37 public static Graphics FromImage(Bitmap b)
41 g.pen =
new Pen(
new ColorS(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue), 1, b.BitsPerPixel);
44 public void DrawLine(
int x,
int y,
int x2,
int y2)
49 int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
50 if (w < 0) dx1 = -1;
else if (w > 0) dx1 = 1;
51 if (h < 0) dy1 = -1;
else if (h > 0) dy1 = 1;
52 if (w < 0) dx2 = -1;
else if (w > 0) dx2 = 1;
53 int longest = Math.Abs(w);
54 int shortest = Math.Abs(h);
55 if (!(longest > shortest))
57 longest = Math.Abs(h);
58 shortest = Math.Abs(w);
59 if (h < 0) dy2 = -1;
else if (h > 0) dy2 = 1;
62 int numerator = longest >> 1;
63 for (
int i = 0; i <= longest; i++)
65 FillEllipse(x, y, pen.width, pen.width, pen.color);
66 numerator += shortest;
67 if (!(numerator < longest))
80 private void DrawLine(
int x,
int y,
int x2,
int y2, Bitmap bf, ColorS c)
85 int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
86 if (w < 0) dx1 = -1;
else if (w > 0) dx1 = 1;
87 if (h < 0) dy1 = -1;
else if (h > 0) dy1 = 1;
88 if (w < 0) dx2 = -1;
else if (w > 0) dx2 = 1;
89 int longest = Math.Abs(w);
90 int shortest = Math.Abs(h);
91 if (!(longest > shortest))
93 longest = Math.Abs(h);
94 shortest = Math.Abs(w);
95 if (h < 0) dy2 = -1;
else if (h > 0) dy2 = 1;
98 int numerator = longest >> 1;
99 for (
int i = 0; i <= longest; i++)
101 bf.SetPixel(x, y, c);
102 numerator += shortest;
103 if (!(numerator < longest))
105 numerator -= longest;
116 public void DrawLine(PointF p, PointF p2)
118 DrawLine((
int)p.X, (
int)p.Y, (
int)p2.X, (
int)p2.Y);
120 public void FillRectangle(Rectangle r, ColorS col)
122 for (
int x = r.X; x < r.Width + r.X; x++)
124 for (
int y = r.Y; y < r.Height + r.Y; y++)
126 buf.SetPixel(x, y, col);
130 public void FillRectangle(RectangleF r, ColorS col)
132 FillRectangle(
new Rectangle((
int)Math.Ceiling(r.X), (
int)Math.Ceiling(r.Y), (
int)Math.Ceiling(r.Width), (
int)Math.Ceiling(r.Height)), col);
134 public void DrawRectangle(Rectangle r)
136 for (
int x = r.X; x < r.Width + r.X; x++)
138 for (
int i = 0; i < pen.width; i++)
140 buf.SetPixel(x + i, r.Y, pen.color);
141 buf.SetPixel(x + i, r.Y + r.Height, pen.color);
144 for (
int y = r.Y; y < r.Height + r.Y; y++)
146 for (
int i = 0; i < pen.width; i++)
148 buf.SetPixel(r.X, y + i, pen.color);
149 buf.SetPixel(r.X + r.Width, y + i, pen.color);
153 public void DrawRectangle(RectangleF r)
155 DrawRectangle(
new Rectangle((
int)Math.Ceiling(r.X), (
int)Math.Ceiling(r.Y), (
int)Math.Ceiling(r.Width), (
int)Math.Ceiling(r.Height)));
158 public void DrawEllipse(Rectangle r)
160 if (r.Width == 1 && r.Height == 1)
161 buf.SetPixel(r.X, r.Y, pen.color);
162 double radiusx = r.Width / 2;
163 double radiusy = r.Height / 2;
165 for (
double a = 0.0; a < 360.0; a += 0.1)
167 double angle = a * System.Math.PI / 180;
168 for (
int i = 0; i < pen.width; i++)
170 x = (int)(radiusx * System.Math.Cos(angle) + radiusx + r.X);
171 y = (int)(radiusy * System.Math.Sin(angle) + radiusy + r.Y);
172 buf.SetPixel(x + i, y + i, pen.color);
176 public void DrawEllipse(RectangleF r)
178 DrawEllipse(
new Rectangle((
int)Math.Ceiling(r.X), (
int)Math.Ceiling(r.Y), (
int)Math.Ceiling(r.Width), (
int)Math.Ceiling(r.Height)));
180 public void FillEllipse(
float xx,
float yy,
int w,
int h, ColorS c)
182 if (w <= 1 && w <= 1)
184 buf.SetPixel((
int)xx, (
int)yy, c);
187 double radiusx = w / 2;
188 double radiusy = h / 2;
190 for (
double a = 90; a < 270; a += 0.1)
192 double angle = a * System.Math.PI / 180;
193 x = (int)(radiusx * System.Math.Cos(angle) + radiusx + xx);
194 y = (int)(radiusy * System.Math.Sin(angle) + radiusy + yy);
195 double angle2 = (a + 180) * System.Math.PI / 180;
196 int x2 = (int)(radiusx * System.Math.Cos(angle2) + radiusx + xx);
197 DrawScanline(x, x2, y, c);
200 public void FillEllipse(RectangleF r, ColorS c)
202 FillEllipse(
new Rectangle((
int)Math.Ceiling(r.X), (
int)Math.Ceiling(r.Y), (
int)Math.Ceiling(r.Width), (
int)Math.Ceiling(r.Height)), c);
204 public void FillEllipse(Rectangle r, ColorS c)
206 FillEllipse(r.X, r.Y, r.Width, r.Height, c);
208 public void FillPolygon(PointF[] pfs, Rectangle r)
212 Bitmap bf = buf.CopyInfo();
213 bf.Bytes =
new byte[buf.Bytes.Length];
215 DrawPolygon(pfs, bf, pen.color);
218 filler.FillColor = pen.color;
219 filler.Tolerance =
new ColorS(0, 0, 0);
223 Point p =
new Point(r.X + (r.Width / 2), r.Y + (r.Height / 2));
226 for (
int x = r.X; x < r.Width + r.X; x++)
228 if (PointInPolygon(x, (
int)p.Y))
230 pp =
new Point(x, p.Y);
234 filler.FloodFill(pp.Value);
236 for (
int x = 0; x < bf.SizeX; x++)
238 for (
int y = 0; y < bf.SizeY; y++)
240 if (bf.GetPixel(x, y) == pen.color)
242 buf.SetPixel(x, y, pen.color);
248 public void FillPolygon(PointF[] pfs, RectangleF r)
250 FillPolygon(pfs,
new Rectangle((
int)r.X, (
int)r.Y, (
int)r.Width, (
int)r.Height));
252 private static PointF[] polygon;
253 public bool PointInPolygon(
int x,
int y)
255 int j = polygon.Length - 1;
257 for (
int i = 0; i < polygon.Length; j = i++) c ^= polygon[i].Y > y ^ polygon[j].Y > y && x < (polygon[j].X - polygon[i].X) * (y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) + polygon[i].X;
261 public void FillPolygon(PointF[] pfs, Rectangle r, ColorS c)
266 public void FillPolygon(PointF[] pfs, RectangleF r, ColorS c)
271 private void DrawPolygon(PointF[] pfs, Bitmap bf, ColorS s)
273 for (
int i = 0; i < pfs.Length - 1; i++)
275 DrawLine((
int)pfs[i].X, (
int)pfs[i].Y, (
int)pfs[i + 1].X, (
int)pfs[i + 1].Y, bf, s);
277 DrawLine((
int)pfs[0].X, (
int)pfs[0].Y, (
int)pfs[pfs.Length - 1].X, (
int)pfs[pfs.Length - 1].Y, bf, s);
279 public void DrawScanline(
int x,
int x2,
int line, ColorS col)
281 for (
int xx = x; xx < x2; xx++)
283 buf.SetPixel(xx, line, col);
286 public void Dispose()