C#图片处理之:在图片上画框(圈)
有时候给别人演示图片,例如培训某个软件使用的时候,往往需要在画面上画个框或者圈,提醒用户注意这是个重点。其实这个功能并不难,只需要知道Pen对象的用法就可以了。我们对框或者圈样式的控制都可以通过对Pen的控制来实现。
/**//// <summary>
/// 在图片上画框
/// </summary>
/// <param name="bmp">原始图</param>
/// <param name="p0">起始点</param>
/// <param name="p1">终止点</param>
/// <param name="RectColor">矩形框颜色</param>
/// <param name="LineWidth">矩形框边界</param>
/// <returns></returns>
public static Bitmap DrawRectangleInPicture(Bitmap bmp, Point p0, Point p1, Color RectColor, int LineWidth,DashStyle ds)
{
if (bmp == null) return null;
Graphics g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(RectColor);
Pen pen = new Pen(brush, LineWidth);
pen.DashStyle = ds;
g.DrawRectangle(pen, new Rectangle(p0.X, p0.Y, Math.Abs(p0.X - p1.X), Math.Abs(p0.Y - p1.Y)));
g.Dispose();
return bmp;
}
/**//// <summary>
/// 在图片上画椭圆
/// </summary>
/// <param name="bmp"></param>
/// <param name="p0"></param>
/// <param name="RectColor"></param>
/// <param name="LineWidth"></param>
/// <param name="ds"></param>
/// <returns></returns>
public static Bitmap DrawRoundInPicture(Bitmap bmp, Point p0, Point p1, Color RectColor, int LineWidth, DashStyle ds)
{
if (bmp == null) return null;
Graphics g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(RectColor);
Pen pen = new Pen(brush, LineWidth);
pen.DashStyle = ds;
g.DrawEllipse(pen, new Rectangle(p0.X, p0.Y, Math.Abs(p0.X - p1.X), Math.Abs(p0.Y - p1.Y)));
g.Dispose();
return bmp;
}
/// 在图片上画框
/// </summary>
/// <param name="bmp">原始图</param>
/// <param name="p0">起始点</param>
/// <param name="p1">终止点</param>
/// <param name="RectColor">矩形框颜色</param>
/// <param name="LineWidth">矩形框边界</param>
/// <returns></returns>
public static Bitmap DrawRectangleInPicture(Bitmap bmp, Point p0, Point p1, Color RectColor, int LineWidth,DashStyle ds)
{
if (bmp == null) return null;
Graphics g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(RectColor);
Pen pen = new Pen(brush, LineWidth);
pen.DashStyle = ds;
g.DrawRectangle(pen, new Rectangle(p0.X, p0.Y, Math.Abs(p0.X - p1.X), Math.Abs(p0.Y - p1.Y)));
g.Dispose();
return bmp;
}
/**//// <summary>
/// 在图片上画椭圆
/// </summary>
/// <param name="bmp"></param>
/// <param name="p0"></param>
/// <param name="RectColor"></param>
/// <param name="LineWidth"></param>
/// <param name="ds"></param>
/// <returns></returns>
public static Bitmap DrawRoundInPicture(Bitmap bmp, Point p0, Point p1, Color RectColor, int LineWidth, DashStyle ds)
{
if (bmp == null) return null;
Graphics g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(RectColor);
Pen pen = new Pen(brush, LineWidth);
pen.DashStyle = ds;
g.DrawEllipse(pen, new Rectangle(p0.X, p0.Y, Math.Abs(p0.X - p1.X), Math.Abs(p0.Y - p1.Y)));
g.Dispose();
return bmp;
}