在图片上进行图形绘制,包括矩形框、圆、箭头、文字,可动态编辑绘制的内容,修改颜色和调整大小,此功能主要用于即时通讯程序截图编辑功能,支持导出图片文件。这里上传是源码,给有需要的人,避免重复劳动,大家可在此基础上进行深化开发,文章结尾有下载地址。
效果如下:
图1 基本绘制功能
图2 支持旋转和鼠标划过效果
部分绘制代码:
/// 绘制一头大一头小的箭头
public class DOArrow : DrawableObject
{
public DOArrow()
{
Name = "DOArrow";
}
public override void Draw(Graphics g, bool isPreview = false)
{
if (isPreview || Selected)
{
base.DrawBorder(g);
}
// 计算矩形框对角线长度
double len = Math.Sqrt(Math.Pow(Bounds.Width, 2) + Math.Pow(Bounds.Height, 2));
AdjustableArrowCap lineCap = new AdjustableArrowCap(9, 9, true);
lineCap.Filled = true;
lineCap.MiddleInset = 3.1f;
Pen pen = new Pen(Color, Size);
pen.CustomStartCap = new AdjustableArrowCap(5, (int)len / 3 - 5, true);
pen.CustomEndCap = lineCap;
Point starPoint = Point.Empty; // 起点坐标
Point endPoint = Point.Empty; // 终点坐标
switch (DrawDirection)
{
case DrawDirection.NWSE:
starPoint = new Point(Bounds.X, Bounds.Y);
endPoint = new Point(Bounds.Right, Bounds.Bottom);
break;
case DrawDirection.NESW:
starPoint = new Point(Bounds.Right, Bounds.Y);
endPoint = new Point(Bounds.X, Bounds.Bottom);
break;
case DrawDirection.SWNE:
starPoint = new Point(Bounds.X, Bounds.Bottom);
endPoint = new Point(Bounds.Right, Bounds.Y);
break;
case DrawDirection.SENW:
starPoint = new Point(Bounds.Right, Bounds.Bottom);
endPoint = new Point(Bounds.X, Bounds.Y);
break;
}
g.DrawLine(pen, starPoint, endPoint);
base.DrawControlPoints(g);
}
}
代码比较简单,基本都有注释,很容易看懂,源码下载地址为: