使用C#实现一个简单的绘图工具
作者:lingxiao16888
这篇文章主要为大家详细介绍了如何使用C#开发的简单绘图工具,可以将签名简单绘图后的效果以图片的形式导出,有需要的小伙伴可以跟随小编一起学习一下
1.目的
使用C#开发的简单绘图工具,将签名,简单绘图后的效果以图片的形式导出。
2.相关技术
GDI.....实在没啥
3.效果展示
无背景状态:
导出的图片效果:
添加背景后:
导出的图片效果:
4.代码
public partial class Form1 : Form { public Form1() { InitializeComponent(); g = this.CreateGraphics(); } Graphics g; Graphics imgGraphice; Color curColor = Color.Black; string title; Image backImg; DrawingMode curDrawingTool = DrawingMode.None; int penSize = 0; bool drawingFlag = false; TextBox tb; private void Form1_Load(object sender, EventArgs e) { title = this.Text; toolStripComboBox1.SelectedIndex = 0; } private void opentoolStrip_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = "jpg图片|*.jpg"; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { Image sourceImage = Image.FromFile(ofd.FileName); backImg = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); imgGraphice = Graphics.FromImage(backImg); RectangleF rec = new RectangleF(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); imgGraphice.DrawImage(sourceImage,rec , new RectangleF(0, 0, sourceImage.Width, sourceImage.Height), GraphicsUnit.Pixel); g.Clear(Color.White); g.DrawImage(sourceImage,this.ClientRectangle); this.Text = title + "\t" + ofd.FileName; } } } private void newtoolStrip_Click(object sender, EventArgs e) { //自定义图片 backImg = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); imgGraphice = Graphics.FromImage(backImg); imgGraphice.Clear(Color.White); g.DrawImage(backImg, this.ClientRectangle); this.Text = title + "\t未命名"; } private void savetoolStrip_Click(object sender, EventArgs e) { if (backImg == null) return; using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Filter = "图像文件|*.jpg"; if (sfd.ShowDialog() == DialogResult.OK) { backImg.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); MessageBox.Show("保存至:" + sfd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } private void exittoolStrip_Click(object sender, EventArgs e) { Application.Exit(); } private void colortoolStrip_Click(object sender, EventArgs e) { using (ColorDialog cd = new ColorDialog()) { if (cd.ShowDialog() == DialogResult.OK) { curColor = cd.Color; } } } private void btnPen_Click(object sender, EventArgs e) { curDrawingTool = DrawingMode.Pen; } private void btnLine_Click(object sender, EventArgs e) { curDrawingTool = DrawingMode.Line; } private void btnRec_Click(object sender, EventArgs e) { curDrawingTool = DrawingMode.Rectangle; } private void btnEllipse_Click(object sender, EventArgs e) { curDrawingTool = DrawingMode.Ellipse; } private void btnText_Click(object sender, EventArgs e) { curDrawingTool = DrawingMode.Text; } private void btnRubber_Click(object sender, EventArgs e) { curDrawingTool = DrawingMode.Rubber; } private void Form1_Paint(object sender, PaintEventArgs e) { //进行重绘不然窗口最小化后就没有 if (backImg != null) g.DrawImage(backImg, this.ClientRectangle); } Point oldPoint; private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { oldPoint = e.Location; drawingFlag = true; } switch (curDrawingTool) { case DrawingMode.None: break; case DrawingMode.Pen: break; case DrawingMode.Line: break; case DrawingMode.Rectangle: break; case DrawingMode.Ellipse: break; case DrawingMode.Text: if (imgGraphice == null) return; //在当前位置添加 tb = new TextBox(); tb.Width = 100; tb.Height = 30; tb.BackColor = Color.FromArgb(192, 255, 192); tb.BorderStyle = BorderStyle.Fixed3D; tb.Location = e.Location; tb.KeyDown += Tb_KeyDown; this.Controls.Add(tb); break; case DrawingMode.Rubber: break; default: break; } } private void Tb_KeyDown(object sender, KeyEventArgs e) { if (imgGraphice == null) return; if (e.KeyValue == 13) { g.DrawString(tb.Text, new Font("宋体", 12), new SolidBrush(curColor), oldPoint); imgGraphice.DrawString(tb.Text, new Font("宋体", 12), new SolidBrush(curColor), oldPoint); this.Controls.Remove(tb); } } private void Form1_MouseMove(object sender, MouseEventArgs e) { labCoord.Text = e.Location.ToString(); if (imgGraphice == null || drawingFlag == false) return; switch (curDrawingTool) { case DrawingMode.None: break; case DrawingMode.Pen: g.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location); imgGraphice.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location); oldPoint = e.Location; break; case DrawingMode.Line: //为表现出实时效果需要擦写痕迹效果(即在form上进行绘制只是表象,一切以在image上绘制为准) Form1_Paint(null, null); g.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location); break; case DrawingMode.Rectangle: Form1_Paint(null, null); g.DrawRectangle(new Pen(curColor, penSize),oldPoint.X,oldPoint.Y,e.X-oldPoint.X,e.Y-oldPoint.Y); break; case DrawingMode.Ellipse: Form1_Paint(null, null); g.DrawEllipse(new Pen(curColor, penSize), oldPoint.X, oldPoint.Y, e.X - oldPoint.X, e.Y - oldPoint.Y); break; case DrawingMode.Text: break; case DrawingMode.Rubber: //所谓橡皮擦就是绘制白线 g.DrawLine(new Pen(Color.White, penSize), oldPoint, e.Location); imgGraphice.DrawLine(new Pen(Color.White, penSize), oldPoint, e.Location); oldPoint = e.Location; break; default: break; } } private void Form1_MouseUp(object sender, MouseEventArgs e) { drawingFlag = false; if (imgGraphice == null) return; switch (curDrawingTool) { case DrawingMode.None: break; case DrawingMode.Pen: break; case DrawingMode.Line: //这里绘制Line的最终效果 imgGraphice.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location); break; case DrawingMode.Rectangle: imgGraphice.DrawRectangle(new Pen(curColor, penSize), oldPoint.X, oldPoint.Y, e.X - oldPoint.X, e.Y - oldPoint.Y); break; case DrawingMode.Ellipse: imgGraphice.DrawEllipse(new Pen(curColor, penSize), oldPoint.X, oldPoint.Y, e.X - oldPoint.X, e.Y - oldPoint.Y); break; case DrawingMode.Text: break; case DrawingMode.Rubber: break; default: break; } } private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) { int size; if (int.TryParse(toolStripComboBox1.Text, out size)) { penSize = size; } } } enum DrawingMode { None, Pen, Line, Rectangle, Ellipse, Text, Rubber }
到此这篇关于使用C#实现一个简单的绘图工具的文章就介绍到这了,更多相关C#绘图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!