使用C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的繪圖工具
1.目的
使用C#開發(fā)的簡(jiǎn)單繪圖工具,將簽名,簡(jiǎn)單繪圖后的效果以圖片的形式導(dǎo)出。
2.相關(guān)技術(shù)
GDI.....實(shí)在沒啥
3.效果展示
無背景狀態(tài):
導(dǎo)出的圖片效果:
添加背景后:
導(dǎo)出的圖片效果:
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) { //進(jìn)行重繪不然窗口最小化后就沒有 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; //在當(dāng)前位置添加 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: //為表現(xiàn)出實(shí)時(shí)效果需要擦寫痕跡效果(即在form上進(jìn)行繪制只是表象,一切以在image上繪制為準(zhǔn)) 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 }
到此這篇關(guān)于使用C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的繪圖工具的文章就介紹到這了,更多相關(guān)C#繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WinForm之BindingSource基礎(chǔ)操作實(shí)例教程
這篇文章主要介紹了WinForm之BindingSource基礎(chǔ)操作,對(duì)BindingSource組建的用法進(jìn)行較為深入的實(shí)例分析,需要的朋友可以參考下2014-08-08C#通過XML節(jié)點(diǎn)屬性/屬性值讀取寫入XML操作代碼實(shí)例
本文主要介紹C#通過XML節(jié)點(diǎn)屬性、屬性值對(duì)XML的讀取,寫入操作,大家參考使用吧2013-11-11C#實(shí)現(xiàn)對(duì)二維數(shù)組排序的方法
這篇文章主要介紹了C#實(shí)現(xiàn)對(duì)二維數(shù)組排序的方法,實(shí)例分析了C#數(shù)組遍歷與排序的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#實(shí)現(xiàn)軟件開機(jī)自動(dòng)啟動(dòng)的兩種常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)軟件開機(jī)自動(dòng)啟動(dòng)的兩種常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07C#中Decimal類型截取保留N位小數(shù)并且不進(jìn)行四舍五入操作
這篇文章主要介紹了C#中Decimal類型截取保留N位小數(shù)并且不進(jìn)行四舍五入操作,本文給出需求說明和實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06