2using Bitmap = AForge.Bitmap;
3using Point = AForge.Point;
4using PointF = AForge.PointF;
10 public struct Pen : IDisposable
15 public Pen(ColorS col, ushort w,
int bitsPerPixel)
19 this.bitsPerPixel = (byte)bitsPerPixel;
21 public Pen(ColorS col,
int w,
int bitsPerPixel)
25 this.bitsPerPixel = (byte)bitsPerPixel;
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++)
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;
118 DrawLine((
int)p.X, (
int)p.Y, (
int)p2.X, (
int)p2.Y);
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);
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);
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);
155 DrawRectangle(
new Rectangle((
int)Math.Ceiling(r.X), (
int)Math.Ceiling(r.Y), (
int)Math.Ceiling(r.Width), (
int)Math.Ceiling(r.Height)));
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);
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);
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);
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++)
230 pp =
new Point(x, p.Y);
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);
252 private static PointF[] polygon;
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;
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);
281 for (
int xx = x; xx < x2; xx++)
283 buf.SetPixel(xx, line, col);
AForge.RectangleF RectangleF
AForge.Rectangle Rectangle
System.Drawing.Graphics Graphics
The base class that the flood fill algorithms inherit from. Implements the basic flood filler functio...
void DrawRectangle(RectangleF r)
void DrawLine(int x, int y, int x2, int y2)
void FillEllipse(Rectangle r, ColorS c)
bool PointInPolygon(int x, int y)
void DrawEllipse(Rectangle r)
void FillRectangle(RectangleF r, ColorS col)
void FillPolygon(PointF[] pfs, RectangleF r, ColorS c)
void FillPolygon(PointF[] pfs, RectangleF r)
void FillEllipse(float xx, float yy, int w, int h, ColorS c)
void DrawLine(PointF p, PointF p2)
void DrawRectangle(Rectangle r)
void FillRectangle(Rectangle r, ColorS col)
void FillPolygon(PointF[] pfs, Rectangle r)
void DrawEllipse(RectangleF r)
void FillPolygon(PointF[] pfs, Rectangle r, ColorS c)
void DrawScanline(int x, int x2, int line, ColorS col)
void FillEllipse(RectangleF r, ColorS c)
static Graphics FromImage(Bitmap b)
Implements the QueueLinear flood fill algorithm using array-based pixel manipulation.
Pen(ColorS col, ushort w, int bitsPerPixel)
Pen(ColorS col, int w, int bitsPerPixel)