C#自定义画刷原理解析
作者:林子xxx 时间:2021-06-19 19:17:32
windows系统中的画板工具,有好几种画刷,C#中并没有直接对应可使用的类,只能自己研究。
1.画刷原理
根据本人对PS的相关功能细心分析,发现各种画刷其实就是一幅图片的移位重叠显示。通常这幅画刷图是半透明的,只有其中一些区域有颜色。
上图中的画刷,把间隔设大之后可以明显看到原图的模样。
这是基于位移的画刷,另外有基于时间的,比如喷枪工具。
2.代码实现
1). 直线算法
为什么要直线算法?因为我们移动鼠标,触发MouseMove事件,记录鼠标前一坐标点与当前点,如果两点是是相邻的,当然不需要再做多余的算法,当如果两点是不相邻的,我们就需要计算两点之间所有的点。否则无法有效地进行固定间隔绘制画刷图。
/// <summary>
/// 顺序获取两点间直线上的所有点
/// </summary>
/// <param name="pStart">开始点</param>
/// <param name="pEnd">结束点</param>
/// <returns>两点间直线上的所有点</returns>
private List<Point> getPoint2Point(Point pStart, Point pEnd)
{
List<Point> linePoint = new List<Point>();
if (pStart.X == pEnd.X && pStart.Y == pEnd.Y)
{
linePoint.Add(pStart);
return linePoint;
}
DDALine(pStart.X, pStart.Y, pEnd.X, pEnd.Y, ref linePoint);
return linePoint;
}
//DDA直线画法
private void DDALine(int x0, int y0, int x1, int y1, ref List<Point> ptl)
{
int dx,dy,eps1,k;
float x,y,xIncre,yIncre;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
if(Math.Abs(dx)>Math.Abs(dy))
eps1=Math.Abs(dx);
else
eps1=Math.Abs(dy);
xIncre=(float)dx/(float)eps1;
yIncre=(float)dy/(float)eps1;
for(k=0;k<=eps1;k++)
{
ptl.Add( new Point((int)(x + 0.5), (int)(y + 0.5)) );
x+=xIncre;
y+=yIncre;
}
}
2).鼠标事件
分别为鼠标按下、移动、放开事件
bool bIsDraw = false; //主图画线
Point startPoint_Draw = new Point();//划线点变量
List<Point> pts = new List<Point>();//画点保存
private void pictureBox_main_MouseMove(object sender, MouseEventArgs e)
{
PictureBox pb = sender as PictureBox;
ssl_point.Text = e.Location.ToString();
pb.Refresh();
if (bIsDraw)
{
Point p = limitPoint(e.Location, pictureBox_main.ClientSize);
if (p == startPoint_Draw) return;
Graphics gs = Graphics.FromImage(pb.Image);
if (pictureBox_main.Image != null )
{
List<Point> pl = getPoint2Point(startPoint_Draw, p);
pl.RemoveAt(0);
pts.AddRange(pl);
if (pts.Count >= peninv)
{
for (int i = penmod; i < pts.Count; i += peninv)
{
gs.DrawImage(blushbmp_curr, pts[i].X - pensize , pts[i].Y - pensize , pensize*2, pensize*2);
}
penmod = pts.Count % peninv;
pts.RemoveRange(0, pts.Count - penmod);
}
}
gs.Dispose();
startPoint_Draw = p;
}
}
private void pictureBox_main_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
if (bIsDraw == false)
{
startPoint_Draw = e.Location;
pts.Clear();
pts.Add(startPoint_Draw);
bIsDraw = true;
}
}
private void pictureBox_main_MouseUp(object sender, MouseEventArgs e)
{
if (bIsDraw == true)
{
bIsDraw = false;
if (pictureBox_main.Image != null )
{
pts.Clear();
}
pictureBox_main.Refresh();
}
}
如果根据位移方向加上图片的角度旋转效果,应该会更加接近PS的效果。
3.效果
我使用的画刷图就是来源于本文上图的PS画刷。
图中5条画刷线分别使用间隔1,10,20,40,80。使用不同的原图,就能得到各种各样的画刷。
来源:https://blog.csdn.net/wangzibigan/article/details/78993975
标签:C#,画刷
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot集成JWT实现身份认证(权鉴)的方法步骤
2023-06-02 12:57:37
![](https://img.aspxhome.com/file/2023/6/92636_0s.jpg)
Java中的Object.getClass()方法解析
2022-05-06 18:54:48
![](https://img.aspxhome.com/file/2023/0/71840_0s.png)
Java中二叉树数据结构的实现示例
2023-08-07 03:10:58
C#开发WinForm清空DataGridView控件绑定的数据
2022-12-01 11:22:08
![](https://img.aspxhome.com/file/2023/7/91327_0s.jpg)
java实现简单的webservice方式
2023-11-25 03:59:58
![](https://img.aspxhome.com/file/2023/9/58989_0s.jpg)
Spring-boot 2.3.x源码基于Gradle编译过程详解
2022-04-07 15:41:36
![](https://img.aspxhome.com/file/2023/2/66032_0s.png)
C# webclient中文乱码问题解决方法
2022-06-02 16:39:43
教你怎么使用hadoop来提取文件中的指定内容
2021-07-08 02:22:50
![](https://img.aspxhome.com/file/2023/7/77077_0s.png)
专属于程序员的浪漫-Java输出动态闪图iloveyou
2023-12-01 16:00:57
![](https://img.aspxhome.com/file/2023/8/84608_0s.png)
Java 切割字符串的几种方式集合
2023-12-19 00:02:04
![](https://img.aspxhome.com/file/2023/9/84329_0s.jpg)
Java如何实现http接口参数和返回值加密
2023-08-23 12:28:37
Java中的递归方法示例介绍
2023-07-20 18:04:11
C#使用linq语句查询数组中以特定字符开头元素的方法
2022-09-22 06:39:07
浅谈Visual Studio 2019 Vue项目的目录结构
2023-12-20 20:01:34
![](https://img.aspxhome.com/file/2023/6/94586_0s.jpg)
mvn中dependencyManagement的使用详解
2021-11-05 10:19:20
两种JAVA实现短网址服务算法
2023-05-08 12:17:30
Java Spring AOP源码解析之事务实现原理
2023-09-10 02:39:46
![](https://img.aspxhome.com/file/2023/5/66945_0s.png)
SpringCloud断路器Hystrix原理及用法解析
2022-03-20 16:47:41
Java 中 synchronized的用法详解(四种用法)
2022-03-11 08:55:05
Springboot与vue实现数据导出方法具体介绍
2023-11-06 21:00:34