BioLib  3.6.2
A GUI-less version of Bio .NET library for editing & annotating various microscopy image formats.
Loading...
Searching...
No Matches
Bio.Graphics.Graphics Class Reference
Inheritance diagram for Bio.Graphics.Graphics:

Public Member Functions

void DrawLine (int x, int y, int x2, int y2)
 
void DrawLine (PointF p, PointF p2)
 
void FillRectangle (Rectangle r, ColorS col)
 
void FillRectangle (RectangleF r, ColorS col)
 
void DrawRectangle (Rectangle r)
 
void DrawRectangle (RectangleF r)
 
void DrawEllipse (Rectangle r)
 
void DrawEllipse (RectangleF r)
 
void FillEllipse (float xx, float yy, int w, int h, ColorS c)
 
void FillEllipse (RectangleF r, ColorS c)
 
void FillEllipse (Rectangle r, ColorS c)
 
void FillPolygon (PointF[] pfs, Rectangle r)
 
void FillPolygon (PointF[] pfs, RectangleF r)
 
bool PointInPolygon (int x, int y)
 
void FillPolygon (PointF[] pfs, Rectangle r, ColorS c)
 
void FillPolygon (PointF[] pfs, RectangleF r, ColorS c)
 
void DrawScanline (int x, int x2, int line, ColorS col)
 
void Dispose ()
 The Dispose() function is used to release the resources used by the object.
 

Static Public Member Functions

static Graphics FromImage (Bitmap b)
 

Public Attributes

Bitmap buf
 
Pen pen
 

Detailed Description

Definition at line 37 of file Graphics.cs.

Member Function Documentation

◆ Dispose()

void Bio.Graphics.Graphics.Dispose ( )

The Dispose() function is used to release the resources used by the object.

Definition at line 396 of file Graphics.cs.

397 {
398 pen.Dispose();
399 }

◆ DrawEllipse() [1/2]

void Bio.Graphics.Graphics.DrawEllipse ( Rectangle r)

For every angle from 0 to 360, draw a point at the angle's cosine and sine, multiplied by the radius, plus the radius, plus the center point

Parameters
RectangleThe rectangle that the ellipse will be drawn in.

Definition at line 203 of file Graphics.cs.

204 {
205 if (r.Width == 1 && r.Height == 1)
206 buf.SetPixel(r.X, r.Y, pen.color);
207 double radiusx = r.Width / 2;
208 double radiusy = r.Height / 2;
209 int x, y;
210 for (double a = 0.0; a < 360.0; a += 0.1)
211 {
212 double angle = a * System.Math.PI / 180;
213 for (int i = 0; i < pen.width; i++)
214 {
215 x = (int)(radiusx * System.Math.Cos(angle) + radiusx + r.X);
216 y = (int)(radiusy * System.Math.Sin(angle) + radiusy + r.Y);
217 buf.SetPixel(x+i, y+i, pen.color);
218 }
219 }
220 }

◆ DrawEllipse() [2/2]

void Bio.Graphics.Graphics.DrawEllipse ( RectangleF r)

If you pass a RectangleF to DrawEllipse, it will convert it to a Rectangle and then call DrawEllipse(Rectangle)

Parameters
RectangleFA rectangle with floating point coordinates.

Definition at line 225 of file Graphics.cs.

226 {
227 DrawEllipse(new Rectangle((int)Math.Ceiling(r.X), (int)Math.Ceiling(r.Y), (int)Math.Ceiling(r.Width), (int)Math.Ceiling(r.Height)));
228 }
void DrawEllipse(Rectangle r)
Definition Graphics.cs:203

◆ DrawLine() [1/2]

void Bio.Graphics.Graphics.DrawLine ( int x,
int y,
int x2,
int y2 )

‍Draw a line from (x,y) to (x2,y2) by drawing a series of ellipses with the same width and height as the pen width

Parameters
xThe x coordinate of the first point.
yThe y coordinate of the first point.
x2The x coordinate of the end point of the line.
y2The y coordinate of the second point.

Definition at line 65 of file Graphics.cs.

66 {
67 //Bresenham's algorithm for line drawing.
68 int w = x2 - x;
69 int h = y2 - y;
70 int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
71 if (w < 0) dx1 = -1; else if (w > 0) dx1 = 1;
72 if (h < 0) dy1 = -1; else if (h > 0) dy1 = 1;
73 if (w < 0) dx2 = -1; else if (w > 0) dx2 = 1;
74 int longest = Math.Abs(w);
75 int shortest = Math.Abs(h);
76 if (!(longest > shortest))
77 {
78 longest = Math.Abs(h);
79 shortest = Math.Abs(w);
80 if (h < 0) dy2 = -1; else if (h > 0) dy2 = 1;
81 dx2 = 0;
82 }
83 int numerator = longest >> 1;
84 for (int i = 0; i <= longest; i++)
85 {
86 FillEllipse(x, y, pen.width, pen.width, pen.color);
87 numerator += shortest;
88 if (!(numerator < longest))
89 {
90 numerator -= longest;
91 x += dx1;
92 y += dy1;
93 }
94 else
95 {
96 x += dx2;
97 y += dy2;
98 }
99 }
100 }
void FillEllipse(float xx, float yy, int w, int h, ColorS c)
Definition Graphics.cs:238

◆ DrawLine() [2/2]

void Bio.Graphics.Graphics.DrawLine ( PointF p,
PointF p2 )

DrawLine(PointF p, PointF p2) is a function that takes two points and draws a line between them

Parameters
PointFA structure that defines a point in a two-dimensional plane.
PointFA structure that defines a point in a two-dimensional plane.

Definition at line 150 of file Graphics.cs.

151 {
152 DrawLine((int)p.X, (int)p.Y, (int)p2.X, (int)p2.Y);
153 }
void DrawLine(int x, int y, int x2, int y2)
Definition Graphics.cs:65

◆ DrawRectangle() [1/2]

void Bio.Graphics.Graphics.DrawRectangle ( Rectangle r)

For each pixel in the rectangle, draw a line of pixels of the same color

Parameters
RectangleThe rectangle to draw

Definition at line 179 of file Graphics.cs.

180 {
181 DrawLine(new PointF(r.X, r.Y), new PointF(r.X + r.Width, r.Y));
182 DrawLine(new PointF(r.X + r.Width, r.Y), new PointF(r.X + r.Width, r.Y + r.Height));
183 DrawLine(new PointF(r.X, r.Y), new PointF(r.X, r.Y + r.Height));
184 DrawLine(new PointF(r.X, r.Y + r.Height), new PointF(r.X + r.Width, r.Y + r.Height));
185 }

◆ DrawRectangle() [2/2]

void Bio.Graphics.Graphics.DrawRectangle ( RectangleF r)

"Draw a rectangle with the top left corner at (x,y) and the bottom right corner at (x+width,y+height)."

The function is called with a RectangleF object, which is a rectangle with floating point coordinates. The function converts the floating point coordinates to integers and then calls the DrawRectangle function that takes integer coordinates

Parameters
RectangleFA rectangle with float coordinates.

Definition at line 194 of file Graphics.cs.

195 {
196 DrawRectangle(new Rectangle((int)Math.Ceiling(r.X), (int)Math.Ceiling(r.Y), (int)Math.Ceiling(r.Width), (int)Math.Ceiling(r.Height)));
197 }
void DrawRectangle(Rectangle r)
Definition Graphics.cs:179

◆ DrawScanline()

void Bio.Graphics.Graphics.DrawScanline ( int x,
int x2,
int line,
ColorS col )

DrawScanline(int x, int x2, int line, ColorS col)

This function draws a scanline from x to x2 on the y line

Parameters
xThe starting x position of the scanline
x2The end of the scanline
lineThe line to draw the scanline on
ColorSA struct that contains the RGB values of a color.

Definition at line 388 of file Graphics.cs.

389 {
390 for (int xx = x; xx < x2; xx++)
391 {
392 buf.SetPixel(xx, line, col);
393 }
394 }

◆ FillEllipse() [1/3]

void Bio.Graphics.Graphics.FillEllipse ( float xx,
float yy,
int w,
int h,
ColorS c )

It draws an ellipse by drawing a bunch of horizontal lines

Parameters
xxx position of the ellipse
yyy position of the ellipse
wwidth of the ellipse
hheight of the ellipse
ColorSA struct that contains the color information.
Returns
the color of the pixel at the specified coordinates.

Definition at line 238 of file Graphics.cs.

239 {
240 if (w <= 1 && w <= 1)
241 {
242 buf.SetPixel((int)xx,(int)yy, c);
243 return;
244 }
245 double radiusx = w / 2;
246 double radiusy = h / 2;
247 int x, y;
248 for (double a = 90; a < 270; a += 0.1)
249 {
250 double angle = a * System.Math.PI / 180;
251 x = (int)(radiusx * System.Math.Cos(angle) + radiusx + xx);
252 y = (int)(radiusy * System.Math.Sin(angle) + radiusy + yy);
253 double angle2 = (a+180) * System.Math.PI / 180;
254 int x2 = (int)(radiusx * System.Math.Cos(angle2) + radiusx + xx);
255 DrawScanline(x,x2,y,c);
256 }
257 }
void DrawScanline(int x, int x2, int line, ColorS col)
Definition Graphics.cs:388

◆ FillEllipse() [2/3]

void Bio.Graphics.Graphics.FillEllipse ( Rectangle r,
ColorS c )

‍Draws a filled ellipse with the specified color

Parameters
RectangleThe rectangle to draw the ellipse in.
ColorSA struct that contains the color information.

Definition at line 270 of file Graphics.cs.

271 {
272 FillEllipse(r.X, r.Y, r.Width, r.Height, c);
273 }

◆ FillEllipse() [3/3]

void Bio.Graphics.Graphics.FillEllipse ( RectangleF r,
ColorS c )

‍This function takes a rectangle and a color and draws an ellipse inside the rectangle

Parameters
RectangleFA rectangle with float values
ColorSA struct that contains a byte for each color channel (R, G, B, A)

Definition at line 262 of file Graphics.cs.

263 {
264 FillEllipse(new Rectangle((int)Math.Ceiling(r.X), (int)Math.Ceiling(r.Y), (int)Math.Ceiling(r.Width), (int)Math.Ceiling(r.Height)), c);
265 }

◆ FillPolygon() [1/4]

void Bio.Graphics.Graphics.FillPolygon ( PointF[] pfs,
Rectangle r )

We draw the polygon onto a new bitmap, then we find a point inside the polygon and flood fill it, then we draw the flood filled bitmap onto the original bitmap

Parameters
pfsThe points of the polygon
RectangleThe rectangle that contains the polygon.

Definition at line 279 of file Graphics.cs.

280 {
281 //We will use the flood fill algorithm to fill the polygon.
282 //First we need to create a new Buffer incase the current Buffer contains filled pixels that could prevent flood fill from filling the whole area.
283 Bitmap bf = buf.CopyInfo();
284 bf.Bytes = new byte[buf.Bytes.Length];
285
286 DrawPolygon(pfs, bf, pen.color);
287
288 filler = new QueueLinearFloodFiller(filler);
289 filler.FillColor = pen.color;
290 filler.Tolerance = new ColorS(0, 0, 0);
291 filler.Bitmap = bf;
292 //Next we need to find a point inside the polygon from where to start the flood fill.
293 //We use the center points x-line till we find a point inside.
294 Point p = new Point(r.X + (r.Width / 2), r.Y + (r.Height / 2));
295 Point? pp = null;
296 polygon = pfs;
297 for (int x = r.X; x < r.Width + r.X; x++)
298 {
299 if (PointInPolygon(x, (int)p.Y))
300 {
301 pp = new Point(x, p.Y);
302 break;
303 }
304 }
305 filler.FloodFill(pp.Value);
306 //Now that we have a filled shape we draw it onto the original bitmap
307 for (int x = 0; x < bf.SizeX; x++)
308 {
309 for (int y = 0; y < bf.SizeY; y++)
310 {
311 if(bf.GetPixel(x,y) == pen.color)
312 {
313 buf.SetPixel(x, y, pen.color);
314 }
315 }
316 }
317 bf.Dispose();
318 }
bool PointInPolygon(int x, int y)
Definition Graphics.cs:336

◆ FillPolygon() [2/4]

void Bio.Graphics.Graphics.FillPolygon ( PointF[] pfs,
Rectangle r,
ColorS c )

‍This function takes an array of points, a rectangle, and a color, and fills the polygon with the color

Parameters
pfsAn array of points that make up the polygon
RectangleThe rectangle that the polygon is in.
ColorSA struct that contains a color.

Definition at line 350 of file Graphics.cs.

351 {
352 pen.color = c;
353 FillPolygon(pfs, r);
354 }
void FillPolygon(PointF[] pfs, Rectangle r)
Definition Graphics.cs:279

◆ FillPolygon() [3/4]

void Bio.Graphics.Graphics.FillPolygon ( PointF[] pfs,
RectangleF r )

‍This function takes a list of points and a rectangle and fills the polygon defined by the points with the rectangle

Parameters
pfsAn array of PointF structures that represent the vertices of the polygon to fill.
RectangleFA rectangle with float coordinates.

Definition at line 324 of file Graphics.cs.

325 {
326 FillPolygon(pfs, new Rectangle((int)r.X, (int)r.Y, (int)r.Width, (int)r.Height));
327 }

◆ FillPolygon() [4/4]

void Bio.Graphics.Graphics.FillPolygon ( PointF[] pfs,
RectangleF r,
ColorS c )

‍This function takes an array of points, a rectangle, and a color, and fills the polygon with the color

Parameters
pfsAn array of points that make up the polygon
RectangleFThe rectangle that the polygon is in.
ColorSA struct that contains a color.

Definition at line 361 of file Graphics.cs.

362 {
363 pen.color = c;
364 FillPolygon(pfs, r);
365 }

◆ FillRectangle() [1/2]

void Bio.Graphics.Graphics.FillRectangle ( Rectangle r,
ColorS col )

For every pixel in the rectangle, set the pixel to the color

Parameters
RectangleThe rectangle to fill
ColorSA struct that contains the RGB values of a color.

Definition at line 158 of file Graphics.cs.

159 {
160 for (int x = r.X; x < r.Width + r.X; x++)
161 {
162 for (int y = r.Y; y < r.Height + r.Y; y++)
163 {
164 buf.SetPixel(x,y, col);
165 }
166 }
167 }

◆ FillRectangle() [2/2]

void Bio.Graphics.Graphics.FillRectangle ( RectangleF r,
ColorS col )

‍This function takes a rectangle and a color and fills the rectangle with the color

Parameters
RectangleFA rectangle with float values
ColorSA struct that contains a byte for each color channel (R, G, B, A)

Definition at line 172 of file Graphics.cs.

173 {
174 FillRectangle(new Rectangle((int)Math.Ceiling(r.X), (int)Math.Ceiling(r.Y), (int)Math.Ceiling(r.Width), (int)Math.Ceiling(r.Height)), col);
175 }
void FillRectangle(Rectangle r, ColorS col)
Definition Graphics.cs:158

◆ FromImage()

static Graphics Bio.Graphics.Graphics.FromImage ( Bitmap b)
static

It creates a new Graphics object, sets the buffer to the bitmap passed in, and sets the pen to a white pen with a width of 1 and a color depth of the bitmap's color depth

Parameters
BitmapThe bitmap to draw on
Returns
A new Graphics object.

Definition at line 51 of file Graphics.cs.

52 {
53 Graphics g = new Graphics();
54 g.buf = b;
55 g.pen = new Pen(new ColorS(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue),1,b.BitsPerPixel);
56 return g;
57 }

◆ PointInPolygon()

bool Bio.Graphics.Graphics.PointInPolygon ( int x,
int y )

If the point is on the left side of the line, then the point is inside the polygon

Parameters
xThe x coordinate of the point to test.
yThe y coordinate of the point to test.
Returns
A boolean value.

Definition at line 336 of file Graphics.cs.

337 {
338 int j = polygon.Length - 1;
339 bool c = false;
340 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;
341 return c;
342 }

Member Data Documentation

◆ buf

Bitmap Bio.Graphics.Graphics.buf

Definition at line 40 of file Graphics.cs.

◆ pen

Pen Bio.Graphics.Graphics.pen

Definition at line 42 of file Graphics.cs.


The documentation for this class was generated from the following file: