/// <summary> /// 画线 /// </summary> /// <param name="graphics"></param> private void DrawLine(Graphics graphics) { try { graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //字体 System.Drawing.Font font = new Font(FontFamily.GenericSansSerif, 20f); this.ReDraw(); Domain.GraphicsPointData graphicsPointData = new Domain.GraphicsPointData() { Tool = DrawTool.Line, X = this.startX, Y = this.startY, EndX = this.endX, EndY = this.endY, Color = brushColor, BrushWidth = 2, }; this.graphicsPath.Add(graphicsPointData); //起止坐标点 PointF point1 = new PointF(this.startX, this.startY); PointF point2 = new PointF(this.endX, this.endY); //笔刷 { using (Pen pen = new Pen(this.brushColor, 2)) { graphics.DrawLine(pen, point1, point2); } } this.pboxContainer.Image = image; } catch (OutOfMemoryException ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 橡皮擦 /// </summary> private void Eraser(float x, float y, float w, float h) { if (graphics != null) { Domain.GraphicsPointData graphicsPointData = new Domain.GraphicsPointData() { Tool = DrawTool.Eraser, X = x, Y = y, EndX = x + w, EndY = y + h, Color = brushColor, BrushWidth = 2, Width = 15, Height = 15, }; this.graphicsPath.Add(graphicsPointData); graphics.CompositingMode = CompositingMode.SourceCopy; SolidBrush solidBrush = new SolidBrush(Color.Transparent); graphics.FillRectangle(solidBrush, new RectangleF(x, y, w, h)); this.pboxContainer.Image = image; } }
/// <summary> /// 重绘 /// </summary> private void ReDraw() { foreach (var item in graphicsPath) { using (Pen pen = new Pen(item.Color, item.BrushWidth)) { switch (item.Tool) { //线条 Domain.GraphicsPointData case DrawTool.Line: Domain.GraphicsPointData graphicsPointData = (Domain.GraphicsPointData)item; graphics.DrawLine(pen, graphicsPointData.X, graphicsPointData.Y, graphicsPointData.EndX, graphicsPointData.EndY); break; //椭圆 case DrawTool.Eclips: graphics.DrawEllipse(pen, item.X, item.Y, item.Width, item.Height); break; //矩形 case DrawTool.Rectangle: graphics.DrawRectangle(pen, item.X, item.Y, item.Width, item.Height); break; //橡皮擦 case DrawTool.Eraser: graphics.CompositingMode = CompositingMode.SourceCopy; SolidBrush solidBrush = new SolidBrush(Color.Transparent); graphics.FillRectangle(solidBrush, new RectangleF(item.X, item.Y, item.Width, item.Height)); this.pboxContainer.Image = image; break; } pen.Dispose(); } } }