Picture Box(1) 修正版
在前面一篇文章中,我介绍了一个Picture Box控件,看这里!
最近我在一个小项目中,使用了这个控件,当然,是做过更改后的,主要修正了一个Bug,然后增加了三个函数,用于在图片上面画一些矩形框和标记。
主要的更改是对Copy功能的更改,放大功能没有做更改。解决方案中增加了一个Winform项目示例。
修正版的源代码下载地址: 源代码 Source Code
现在项目示例截图如下:
图中,蓝色框是增加的第一个功能。
绿色虚线框是第二个功能。
图像中的lable是第三个功能。
位置大小信息是增加的一个数据访问。
增加的三个函数分别如下:
1. 在画布上面增加“已存在”矩形框
/// <summary> /// the existence rectangle list /// </summary> public List<Rectangle> existRectangle; private void DrawExistRectangle(Graphics g) { Pen drawRectanglesPen = new Pen(Color.Blue, 2.0F); foreach (Rectangle rect in existRectangle) { g.DrawRectangle(drawRectanglesPen, rect); } drawRectanglesPen.Dispose(); }
2.画一个需要标记的矩形
// a 'star' rectangle, means that rectangle marked. private Rectangle starRectangle; /// <summary> /// a marked rectangle /// </summary> public Rectangle StarRectangle { get { return starRectangle; } set { starRectangle = value; } } private void DrawMarkedRectangle(Graphics g) { Pen drawMarkedRectPen = new Pen(Color.Green, 2.0F); drawMarkedRectPen.DashStyle = DashStyle.Dash; g.DrawRectangle(drawMarkedRectPen, starRectangle); drawMarkedRectPen.Dispose(); }
3.画标签(带位置参数)
在这个实现中,我新建了一个类LableType
/// <summary> /// LableType indicate a lable with a location information /// </summary> public class LableType { private string lableString; /// <summary> /// lable content /// </summary> public string LableString { get { return lableString; } set { lableString = value; } } private Point locationInfo; /// <summary> /// lable's location information /// </summary> public Point LocationInfo { get { return locationInfo; } set { locationInfo = value; } } /// <summary> /// LableType indicate a lable with a location information /// </summary> /// <param name="lable">lable content</param> /// <param name="point">lable location</param> public LableType(string lable, Point point) { this.lableString = lable; this.locationInfo = point; } }
/// <summary> /// the lable list need to draw /// </summary> public List<LableType> lableList; private void DrawLableList(Graphics g) { foreach (LableType lt in lableList) { g.DrawString(lt.LableString, new Font("System", 18F, GraphicsUnit.Pixel), new SolidBrush(Color.DarkBlue), lt.LocationInfo.X, lt.LocationInfo.Y); } }
4.修正Bug:
先前代码中,我发现的一个Bug,已修正
代码如下:
在DragOptions.Copy 项中,先前没有dragWindowSave = dragWindow; 如果默认选择DragOptions的属性为Copy(在属性栏中选择),
则在实际运行中会出现NullException,后来发现是作者没有将dragWindow赋值给dragWindowSave.
switch (_dragOptions) { case DragOptions.Prompt : dragWindowSave = dragWindow; cmnuPrompt.Show(this, dragEnd); break; case DragOptions.Copy : dragWindowSave = dragWindow; Copy(); break; case DragOptions.Zoom : Zoom(); break; }
5.增加DragWindow
增加这样一个拖拉框的属性,可供外部代码访问
// Holds the size and location of the drag window private Rectangle dragWindow; public Rectangle DragWindow { get { return dragWindow; } set { dragWindow = value; } }