Windows应用程序(C#)画板画图

2025-04-10 08:15:28

使用VS开发Windows画板应用程序,来实现画图的功能,使用C#语言开发。效果如图。

Windows应用程序(C#)画板画图

工具/原料

已安装VS2010或其它版本的VS编辑器软件的电脑一台

程序目录

1、新建两个Form,一个叫Form1为程序主窗体用来绘图,一个select_font,Resources文件夹下存放图标图片。图片123.bmp存放在bin/Debug下面,颜色为青绿色,它是写出的文字颜色纹理。

Windows应用程序(C#)画板画图
Windows应用程序(C#)画板画图
Windows应用程序(C#)画板画图

窗体设计

1、Form1窗体,画图窗体界面。

Windows应用程序(C#)画板画图

2、select_font窗体,用来调用计算机字体,会在画图面板写字时调用出来。

Windows应用程序(C#)画板画图

3、调用select_font窗体,效果如图。

Windows应用程序(C#)画板画图

select_font窗体代码

1、引用:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

2、全局变量:public int fontsize;public string fontname;

3、Load中代码:private void select_font_Load(object sender, EventArgs e像粜杵泳){ Graphics g = this.CreateGraphics(); for (int i = 0; i <= System.Drawing.FontFamily.GetFamilies(g).Length - 1; i++) { this.comboBox1.Items.Add(System.Drawing.FontFamily.GetFamilies(g)[i].Name.ToString()); for (int j = 7; j <= 72; j += 2) { this.comboBox2.Items.Add(j.ToString()); } this.comboBox1.SelectedItem = "隶书"; this.comboBox2.SelectedItem= "11"; } }

4、下拉列表代码:private void comboBox1_SelectedIndexChanged(object sender, Eve荏鱿胫协ntArgs e) { fontname = this.comboBox1.SelectedItem.ToString(); } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { fontsize = Convert.ToInt16(this.comboBox2.SelectedItem.ToString()); }

Form1主窗体代码

1、引用:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;

2、调用select_font和全局变量:public Bitmap pbmap;Graphics g;Pen p;Color color;string drawlx;int zx, x, y, xx, yy, xxx, yyy;float x1, x2, y1, y2;select_font sfont = new select_font();

3、Load中代码:private void Form1_Load(object sender, EventArgs e) { this.toolStripStatusLabel2.Text = System.DateTime.Now.ToString(); }

4、所有鼠标点击代码;private void label2_Click(object sender, EventArgs e) { color = t茑霁酌绡his.label2.BackColor; }private void 新建ToolStripMenuItem_Click(object sender, EventArgs e) { g = this.pictureBox1.CreateGraphics(); pbmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); } private void label7_Click(object sender, EventArgs e) { color = this.label7.BackColor; } private void label8_Click(object sender, EventArgs e) { color = this.label8.BackColor; } private void lineShape2_Click(object sender, EventArgs e) { zx = 3; } private void lineShape3_Click(object sender, EventArgs e) { zx = 5; }private void button8_Click(object sender, EventArgs e) { drawlx = "line"; } private void 清空ToolStripMenuItem_Click(object sender, EventArgs e) { //g = this.pictureBox1.CreateGraphics(); //Bitmap image = new Bitmap("123.bmp"); //TextureBrush brush = new TextureBrush(image); this.pictureBox1.Image = null; } private void button15_Click(object sender, EventArgs e) { drawlx = "ellipse"; /* this.panel3.Controls.Clear(); this.label10.Visible = true; this.label10.Left = 0; this.label10.Top = 20; this.panel3.Controls.Add(this.label10);*/ } private void lineShape1_Click(object sender, EventArgs e) { zx = 1; } private void button13_Click(object sender, EventArgs e) { drawlx = "polygon"; } private void label9_Click(object sender, EventArgs e) { color = this.label9.BackColor; } private void label5_Click(object sender, EventArgs e) { color = this.label5.BackColor; } private void label4_Click(object sender, EventArgs e) { color = this.label4.BackColor; } private void label6_Click(object sender, EventArgs e) { color = this.label6.BackColor; } private void label3_Click(object sender, EventArgs e) { color = this.label3.BackColor; } private void button16_Click(object sender, EventArgs e) { drawlx = "rectangle"; } private void button14_Click(object sender, EventArgs e) { drawlx = "circle"; } private void button9_Click(object sender, EventArgs e) { this.textBox1.Visible = true; sfont.Show(); } private void button19_Click(object sender, EventArgs e) { Bitmap image = new Bitmap("123.bmp"); TextureBrush brush = new TextureBrush(image);//TextureBrush 纹理刷 Font font = new Font(sfont.fontname, sfont.fontsize); g.DrawString(this.textBox1.Text, font, brush, new Point(100, 100)); sfont.Hide(); } private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) { saveFileDialog1.InitialDirectory = "c:"; saveFileDialog1.Filter = "JPEG图片|*.jpg|Png图像(*.png)|*.png|所有格式(*.*)|*.*"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string folderP = saveFileDialog1.FileName; //Image img = pbmap; this.pictureBox1.Image.Save(folderP); //img.Save(folderP); } } private void button7_Click(object sender, EventArgs e) { drawlx = "Curve"; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { xx = e.X; yy = e.Y; } private void 关闭ToolStripMenuItem1_Click(object sender, EventArgs e) { this.Close(); }

5、鼠标MouseDown和MouseUp事件代码:private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { x1 = e.X; y1 = e.Y; x = e.X; y = e.Y; } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { xxx = e.X; yyy = e.Y; x2 = e.X; y2 = e.Y; Graphics g = Graphics.FromImage(pbmap); if (drawlx == "line") { p = new Pen(color, zx); g.DrawLine(p, x1, y1, x2, y2); } if (drawlx == "ellipse") { p = new Pen(color, zx); g.DrawEllipse(p, x1, y1, x2, y2); } if (drawlx == "polygon") { p = new Pen(color, zx); g.DrawPolygon(p, new Point[]{ new Point(x,y), new Point(x+10,y+25), new Point(x+35,y+25), new Point(x+10,y+35), new Point(x+20,y+55), new Point(x,y+40), new Point(x-20,y+55), new Point(x-10,y+35), new Point(x-35,y+25), new Point(x-10,y+25), new Point(x,y)}); } if (drawlx == "rectangle") { p = new Pen(color, zx); g.DrawRectangle(p, new Rectangle(x, y, x, y)); } if (drawlx == "circle") { Point centerPoint = new Point(x, y); int r = 60; GraphicsPath path = new GraphicsPath(); path.AddEllipse(centerPoint.X - r, centerPoint.Y - r, 2 * r, 2 * r); PathGradientBrush brush = new PathGradientBrush(path); brush.CenterPoint = centerPoint; brush.CenterColor = Color.White; brush.SurroundColors = new Color[] { color }; g.FillEllipse(brush, centerPoint.X - r, centerPoint.Y - r, 2 * r, 2 * r); } if (drawlx == "Curve") { // Graphics g ; Pen p = new Pen(color, zx); g.DrawCurve(p, new Point[]{ new Point(x,y), new Point(x+100,y-120), new Point(x+200,y+100), new Point(x+250,y-200), new Point(xxx,yyy)}, 0.8f); } this.pictureBox1.Image = pbmap; }

控件类型(补充)

1、select_font窗体:comboBox1、comboBox2、radioButton1、radioButton2、radioButton3

Windows应用程序(C#)画板画图

2、Form1主绘图窗体:菜单:ToolStripMenuItem绘图工具、确认字体按钮:都是button按钮线条粗细:lineShape颜色面板:label文字输入框:textBox

Windows应用程序(C#)画板画图
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢