Graphics
#39991522
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Нужно на рисунке нарисовать линии с возможностью стереть и очистить.
Подскажите кто как бы это сделал.
Первый раз столкнулся с Graphics. Сижу ковыряюсь. Но что то путного ни чего не выходит.
В спойлере последняя не рабочая версия.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156.
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using Logics.Models;
namespace Logics
{
public class ServiceGraphic<T> where T : PictureBox
{
private T workspace;
private Point pointStart;
private Point pointEnd;
private Graphics imageG;
private Graphics pointsG;
private Graphics bothG;
private Pen pen;
public ServiceGraphic(T workspace)
{
SetPointStart(0, 0);
SetPointEnd(0, 0);
SetPen(Color.Black, 1f);
SetWorkspace(workspace);
SetWorkspaceColor(workspace.BackColor);
}
// points
public void SetPointStart(Point point)
{
pointStart = point;
}
public void SetPointStart(int x, int y)
{
SetPointStart(new Point(x, y));
}
public void SetPointEnd(Point point)
{
pointEnd = point;
}
public void SetPointEnd(int x, int y)
{
SetPointEnd(new Point(x, y));
}
// workspace
public void SetWorkspace(T workspace)
{
this.workspace = workspace;
var image1 = workspace.Image.Clone() as Image;
var image2 = workspace.Image.Clone() as Image;
imageG = Graphics.FromImage(image1);
pointsG = Graphics.FromImage(image2);
bothG = Graphics.FromImage(workspace.Image);
// graphics for only image
imageG.InterpolationMode = InterpolationMode.HighQualityBicubic;
imageG.SmoothingMode = SmoothingMode.HighQuality;
imageG.PixelOffsetMode = PixelOffsetMode.HighQuality;
imageG.CompositingQuality = CompositingQuality.HighQuality;
// graphics for only points
pointsG.InterpolationMode = InterpolationMode.HighQualityBicubic;
pointsG.SmoothingMode = SmoothingMode.HighQuality;
pointsG.PixelOffsetMode = PixelOffsetMode.HighQuality;
pointsG.CompositingQuality = CompositingQuality.HighQuality;
// graphics for both, image & points
bothG.InterpolationMode = InterpolationMode.HighQualityBicubic;
bothG.SmoothingMode = SmoothingMode.HighQuality;
bothG.PixelOffsetMode = PixelOffsetMode.HighQuality;
bothG.CompositingQuality = CompositingQuality.HighQuality;
workspace.Invalidate();
}
public void SetWorkspaceColor(Color color)
{
workspace.BackColor = color;
SetWorkspace(workspace);
}
public void WorkspaceClear()
{
// clear points & clear both, then restore both from image
pointsG?.Clear(workspace.BackColor);
bothG?.Clear(workspace.BackColor);
bothG = Graphics.FromHdc(imageG.GetHdc());
workspace.Invalidate();
}
public void WorkspaceEraser()
{
WorkspaceEraser(0f);
}
public void WorkspaceEraser(float eraserSize)
{
pen = new Pen(workspace.BackColor, eraserSize);
}
// pen
public void SetPen(Color color)
{
pen = new Pen(color, pen.Width);
}
public void SetPen(float width)
{
pen = new Pen(pen.Color, width);
}
public void SetPen(Color color, float width)
{
pen = new Pen(color, width);
}
// draw point
public void DrawPoint()
{
DrawPoint(pointStart, pen);
}
public void DrawPoint(Point pointStart)
{
DrawPoint(pointStart, pen);
}
public void DrawPoint(Point pointStart, Pen pen)
{
this.pen = pen;
this.pointStart = pointStart;
if (pointsG.DpiX > 0 & pointsG.DpiY > 0)
{
pointsG.FillRectangle(pen.Brush, pointStart.X, pointStart.Y, pen.Width, pen.Width);
bothG.FillRectangle(pen.Brush, pointStart.X, pointStart.Y, pen.Width, pen.Width);
}
this.pointStart = pointEnd;
workspace.Invalidate();
}
public IEnumerable<Pixel> GetPoints()
{
var bitmap = workspace.Image as Bitmap;
for (int width = 0; width < workspace.Image.Width ; width++)
{
for (int height = 0; height < workspace.Height; height++)
{
var pixel = bitmap.GetPixel(width, height);
if (pixel.ToArgb() == pen.Color.ToArgb())
{
yield return new Pixel(new Point(width, height), pixel);
}
}
}
}
}
}
|
|