C# 我的小画板
我的画板
先看实现图
实现过程
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//**********************金燕电子****************************************
namespace huaban
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
toolStrip1.Enabled = false;
foreColor = Color.Black ;
backColor = Color.White ;
}
Color backColor = new Color();
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(backColor);
toolStrip1.Enabled = true;
//创建一个Bitmap
theImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
editFileName = "新建文件";
//修改窗口标题
this.Text = "MyDraw\t" + editFileName;
ig = Graphics.FromImage(theImage);
ig.Clear(backColor);
}
string editFileName = "";
private Image theImage;
public Graphics ig { get; private set; } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//修改窗口标题
this.Text = "MyDraw\t" + openFileDialog1.FileName;
editFileName = openFileDialog1.FileName;
theImage = Image.FromFile(openFileDialog1.FileName);
Graphics g = this.CreateGraphics();
g.DrawImage(theImage, this.ClientRectangle);
ig = Graphics.FromImage(theImage);
ig.DrawImage(theImage, this.ClientRectangle);
//ToolBar可以使用了
toolStrip1.Enabled = true;
}
} private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "图像(*.bmp)|*.bmp";
saveFileDialog1.FileName = editFileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
theImage.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
this.Text = "MyDraw\t" + saveFileDialog1.FileName;
editFileName = saveFileDialog1.FileName;
}
} private void Form1_Paint(object sender, PaintEventArgs e)
{
//将Image中保存的图像,绘制出来
Graphics g = this.CreateGraphics();
if (theImage != null)
{
g.Clear(Color.White);
g.DrawImage(theImage, this.ClientRectangle);
}
}
Color foreColor = new Color();
private void Form1_MouseDown(object sender, MouseEventArgs e)
{ if (e.Button == MouseButtons.Left)
{
//如果选择文字输入,则打开strInput窗体
if (drawTool == (int)drawTools.String)
{
Frm_Text inputBox = new Frm_Text();
inputBox.StartPosition = FormStartPosition.CenterParent;
if (inputBox.ShowDialog() == DialogResult.OK)
{
Graphics g = this.CreateGraphics();
Font theFont = this.Font;
g.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
}
}
//如果开始绘制,则开始记录鼠标位置
else if ((isDrawing = !isDrawing) == true)
{
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
}
}
}
int drawTool = 0;
enum drawTools
{
None = 0,
Pen = 1,
Line = 2,
Ellipse = 3,
Rectangle = 4,
String = 5,
Rubber =6
}
Point oldPoint = new Point();
Point startPoint = new Point();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g;
g = this.CreateGraphics(); if (isDrawing)
{
switch (drawTool)
{
case (int)drawTools.None:
break;
case (int)drawTools.Pen:
//从上一个点到当前点绘制线段
g.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case (int)drawTools.Line:
//首先恢复此次操作之前的图像,然后再添加Line
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
break;
case (int)drawTools.Ellipse:
//首先恢复此次操作之前的图像,然后再添加Ellipse
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case (int)drawTools.Rectangle:
//首先恢复此次操作之前的图像,然后再添加Rectangle
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case (int)drawTools.String:
break;
case (int)drawTools.Rubber:
//用背景色绘制宽线段
g.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
}
}
}
bool isDrawing = false;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
switch (drawTool)
{
case (int)drawTools.Line:
ig.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
break;
case (int)drawTools.Ellipse:
ig.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case (int)drawTools.Rectangle:
ig.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
}
} private void Frm_Main_Paint(object sender, PaintEventArgs e)
{
//将Image中保存的图像,绘制出来
Graphics g = this.CreateGraphics();
if (theImage != null)
{
g.Clear(Color.White);
g.DrawImage(theImage, this.ClientRectangle);
}
} private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton6.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Pen)
{
drawTool = (int)drawTools.None ;
toolStripButton1.Checked = false ;
}
else
{
drawTool = (int)drawTools.Pen;
toolStripButton1.Checked = true;
}
} private void toolStripButton2_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton6.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Line)
{
drawTool = (int)drawTools.None;
toolStripButton2.Checked = false;
}
else
{
drawTool = (int)drawTools.Line;
toolStripButton2.Checked = true;
}
} private void toolStripButton3_Click(object sender, EventArgs e)
{ toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton6.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Rectangle)
{
drawTool = (int)drawTools.None;
toolStripButton3.Checked = false;
}
else
{
drawTool = (int)drawTools.Rectangle;
toolStripButton3.Checked = true;
}
} private void toolStripButton4_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton6.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Ellipse)
{
drawTool = (int)drawTools.None;
toolStripButton4.Checked = false;
}
else
{
drawTool = (int)drawTools.Ellipse;
toolStripButton4.Checked = true;
}
} private void toolStripButton5_Click(object sender, EventArgs e)
{ toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton6.Checked = false;
if (drawTool == (int)drawTools.String)
{
drawTool = (int)drawTools.None;
toolStripButton5.Checked = false;
}
else
{
drawTool = (int)drawTools.String;
toolStripButton5.Checked = true;
}
} private void toolStripButton6_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Rubber )
{
drawTool = (int)drawTools.None;
toolStripButton6.Checked = false;
}
else
{
drawTool = (int)drawTools.Rubber;
toolStripButton6.Checked = true;
}
} private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
foreColor = colorDialog1.Color ;
} }
}
}
金燕电子
C# 我的小画板的更多相关文章
- canvas小画板--(1)平滑曲线
功能需求 项目需求:需要实现一个可以自由书写的小画板 简单实现 对于熟悉canvas的同学来说,这个需求很简单,短短几十行代码就能实现: <!doctype html> <html& ...
- canvas小画板——(2)荧光笔效果
我们在上一篇文章中讲了如何绘制平滑曲线 canvas小画板——(1)平滑曲线. 透明度实现荧光笔 现在我们需要加另外一种画笔效果,带透明度的荧光笔.那可能会觉得绘制画笔的时候加上透明度就可以了.我们来 ...
- [原创]基于html5新标签canvas写的一个小画板
最近刚学了canvas,写个小应用练习下 源代码 <!DOCTYPE> <html> <head> <meta http-equiv="Conten ...
- iOS小画板画线总结
一:基本画线: 使用贝赛尔曲线画: //创建路径 UIBezierPath* aPath = [UIBezierPath bezierPath]; //设置线宽 aPath.lineWidth = 5 ...
- 用canvas画一个的小画板(PC端移动端都能用)
前言 本篇的内容主要包括: canvas标签简介 画板的功能简介 画板的JS部分(包括:1.获取画布 2.使画板全屏幕显示且自适应 3.如何绘制直线 4.绘画时的三种状态(鼠标点击.移动.离开)5.画 ...
- canvas小画板——(3)笔锋效果
画线准备 准备一个canvas <canvas id="canvasId" width="1000" height="800"> ...
- 开源 iOS 项目分类索引大全 - 待整理
开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...
- 史上最全的常用iOS的第三方框架
文章来源:http://blog.csdn.net/sky_2016/article/details/45502921 图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片 ...
- 常用iOS的第三方框架
图像:1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等 ...
随机推荐
- .Net Core/Framework之Nginx反向代理后获取客户端IP等数据探索
公司项目最近出现获取访问域名.端口.IP错误现象,通过排查发现, 之前项目一直通过Nginx自定义Headers信息来获取,但最近运维人员失误操作造成自定义Header信息丢失,造成项目拿不到对应的数 ...
- Docker 空间大小设置 - 十
一.容器启动 默认存储大小: 1.一种在启动项 docker.service 中配置. 2.在启动项配置调用的 docker-storage 配置文件中配置: 二.Docker 容器默认启动文件: / ...
- android 内存回收及怎样避免内存泄露
http://blog.vunso.com/201307/android%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6%E5%8F%8A% ...
- 单片机AT和STC区别
http://www.21ic.com/jichuzhishi/datasheet/AT89C51/data/192017.html
- Spring重温(三)--Spring依赖注入(DI)
前言:在Spring框架中,DI(依赖注入)是用来定义对象彼此间的依赖,主要有set方法注入和构造器注入两种方式.另外,当一个类包含多个构造函数带的参数相同,它总是会造成构造函数注入参数类型歧义的问题 ...
- telegram即时通信软件和outline ---- by 余弦 by倾旋
第一次遇到它 是在余弦的圈子里发现的tele推送,他当时关键部分说的极其少,就三个字,你懂的..看完以后,我想哭.我真的不懂.. 我是如何突破这个术语的 近期,随着信息源的增多.(收集了大量的可靠公众 ...
- [Docker]Docker拉取,上传镜像到Harbor仓库
需求 因为项目的需求,需要制作一个基于tomcat的镜像.那么前提就是,需要有tomcat的基础镜像. 怎么做 我的思路跑偏了,本来以为是需要将tomcat下载下来,然后通过docker命令,让它成为 ...
- 红迅JSAAS敏捷开发平台
红迅JSAAS敏捷开发平台: http://www.redxun.cn/
- python用类装饰函数的一个有趣实现
class RunningLog: def __init__(self,func): self._func=func self._func_name = func.__name__ def __cal ...
- ECC加密算法入门介绍 --- 看雪
标 题:ECC加密算法入门介绍 作 者:zmworm 时 间:2003/05/04 08:32pm 链 接:http://bbs.pediy.com ECC加密算法入门介绍 作者 :ZMWorm[C ...