锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 英语翻译 / C#绘制画刷工具
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。

C#绘制画刷工具

C#绘制画刷工具工具

Introduction 介绍

Paint Brush tool is similar to the pencil (free hand) with more features. It can be used in different colors, the shape and size of the Paint Brush can also be changed. C#绘制画刷工具类似于有很多功能的铅笔(自由变换)。它可以改变不同的颜色来使用。,画笔的形状和大小也可以改变。

Project Idea 项目的想法

项目想法

Global Variable 全局变量

 private int x1, y1, x2, y2;
private bool flag = false;
private bool colorFlag = false;
private Label lbl;
private ArrayList sequenceStore = new ArrayList();

Steps 步骤

In mouse down: 在鼠标按下事件里:

 private void panel1_MouseDown(object sender, MouseEventArgs e)
{
try
{
x1 = e.X;
y1 = e.Y;
}
catch (Exception)
{
throw;
}
}

In mouse move: 鼠标移动:

 x2 = e.X;
y2 = e.Y;
Graphics g = Graphics.FromHwnd(panel1.Handle);

//default pen
Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex);
if (colorFlag == true)
{
p = new Pen(lbl.BackColor, (float)comboBox1.SelectedIndex);
}
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
g.DrawLine(p, p1, p2);
LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2);
sequenceStore.Add(linestrgrph);
x1 = x2;
y1 = y2;

Problem 问题

If we run the previous code, the free hand will work but if we stop while drawing, we will note that pen is still drawing the mouse up. So we will make a flag and test through it where flag will be true if mouse is down and set flag to be false while mouse is up.

如果我们运行前面的代码,画笔就会自由工作,但如果我们在绘制时停止,我们将注意松开鼠标,笔仍然在画。所以我们将做一个标志,通过测试按下鼠标时,标志是对的,松开鼠标时,标志就会显示错误。

 private void panel1_MouseDown(object sender, MouseEventArgs e)
{
      try
      {
         x1 = e.X;
         y1 = e.Y;
         flag = true;
      }
      catch (Exception)
      {
         throw;
      }
}



 private void panel1_MouseMove(object sender, MouseEventArgs e)
 {
 try
 {
 if (flag == true)
 {
 x2 = e.X;
 y2 = e.Y;
 Graphics g = Graphics.FromHwnd(panel1.Handle);
 //default pen
 Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex);
 if (colorFlag == true)
   {
 p = new Pen(lbl.BackColor, (float)comboBox1.SelectedIndex);
  }
 Point p1 = new Point(x1, y1);
 Point p2 = new Point(x2, y2);
 g.DrawLine(p, p1, p2);
 LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2);
 sequenceStore.Add(linestrgrph);
 x1 = x2;
 y1 = y2;       
  }
   }
 catch (Exception)
  {
  throw;
  }
 }



 private void panel1_MouseMove(object sender, MouseEventArgs e)
{
try
{
if (flag == true)
{
x2 = e.X;
y2 = e.Y;
Graphics g = Graphics.FromHwnd(panel1.Handle); //default pen Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex); if (colorFlag == true) { p = new Pen(lbl.BackColor, (float)comboBox1.SelectedIndex); } Point p1 = new Point(x1, y1); Point p2 = new Point(x2, y2); g.DrawLine(p, p1, p2); LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2); sequenceStore.Add(linestrgrph); x1 = x2; y1 = y2; } } catch (Exception) { throw; } }

Change Color of Pencil 改变铅笔的颜色

Steps 步骤

In event of Label: make all labels in one event: 标签事件:让所有标签在一个事件:

private void label1_Click(object sender, EventArgs e)
{
lbl = (Label)sender;
colorFlag = true;
}

Note: I add a flag named colorflag because I give a default color to pen when the app runs first. I depend on getting color if user presses click on label, so if I do not make this flag and user starts drawing without choosing color, an exception will occur. I avoid this by giving a default color and making something like a sensor to know if user clicks on label, and if this done I will switch to taking the user's color.

注意:我添加一个名为colorflag的标志,因为在应用程序第一次运行时,我给画笔添加一个默认的颜色。我依赖于获得颜色如果用户按点击标签,所以如果我不做这个标志并且用户没有选择颜色就开始绘制时,将会出现异常。我通过给出一个默认的颜色,如果用户点击标签使类似的传感器知道避免这种情况,如果完成这件事,我会切换到采用用户的颜色。

Change Size of Pencil 改变铅笔的大小

public Form1()
{
InitializeComponent(); for (int i = 6; i <= 30; i++) { comboBox1.Items.Add(i); } comboBox1.Text = comboBox1.Items[0].ToString(); }

And as we do in mouse Move, we change the constructor of pen to receive its value from combox1. 当我们移动鼠标,我们改变笔的构造函数,组合框1收到它的价值。

Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex);

Surprise!!! 惊喜! ! !

After we ended from paint brush if we minimize the form to taskbar we will note that the form does not contain any graphics and is like a blank page. 我们从C#绘制画刷工具结束后,如果我们让任务栏最小化,我们将注意不包含任何图形的形式,就像一个空白页。

Solution 解决方案

We will store each point p1, p2 and pen and for each line we do, we will store it in Arraylist then we will retrieve data stored in Arraylist in event Paint.

我们将存储每个点p1,p2,笔和我们所做的每一线,我们会将其存储在数据列表中,然后我们将检索在绘制事件中数组列表中的存储数据。

Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
g.DrawLine(p, p1, p2);
LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2);
sequenceStore.Add(linestrgrph);

And we will retrieve the values we store: 我们将检索存储值:

private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = Graphics.FromHwnd(panel1.Handle);
foreach (LineStoreGraph lineSG in sequenceStore)
{
g.DrawLine(lineSG.p, lineSG.x1, lineSG.y1, lineSG.x2, lineSG.y2);
}
}

Save Your Image: 保存图片:

 private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "All supported image types|" +
"*.bmp;*.jpg;*.jpeg;*.gif|Bitmaps (*.bmp)|*.bmp|JPEG Images" +
" (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif";
saveFileDialog1.FilterIndex = 1;
Bitmap mybitmap = new Bitmap(panel1.Width, panel1.Height);
for (int i = 0; i < mybitmap.Width; i++)
{
for (int j = 0; j < mybitmap.Height; j++)
{
mybitmap.SetPixel(i, j, Color.White);
}
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Graphics g = Graphics.FromImage(mybitmap);
g.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
LineStoreGraph lineSG;
Pen p = new Pen(Color.Red, 1);
p.StartCap = System.Drawing.Drawing2D.LineCap.Round;
p.EndCap = System.Drawing.Drawing2D.LineCap.Round;
for (int i = 0; i < sequenceStore.Count; i++)
{
lineSG = (LineStoreGraph)sequenceStore[i];
g.DrawLine(lineSG.p, lineSG.x1, lineSG.y1,
lineSG.x2, lineSG.y2);
}
mybitmap.Save(saveFileDialog1.FileName);
}
}
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内