public static void FillEllipse(this Graphics2DGL g, Point p, int width, int height, Pixel col) { if (width == 0 || height == 0) { return; } void ScanLine(int sx, int ex, int y) { for (var i = sx; i <= ex; i++) { g.Draw(i, y, col); } } var a2 = width * width; var b2 = height * height; int fa2 = 4 * a2, fb2 = 4 * b2; int sigma; sigma = 2 * b2 + a2 * (1 - 2 * height); for (int x = 0, y = height; b2 *x <= a2 *y; x++) { ScanLine(p.X - x, p.X + x, p.Y - y); ScanLine(p.X - x, p.X + x, p.Y + y); if (sigma >= 0) { sigma += fa2 * (1 - y--); } sigma += b2 * (4 * x + 6); } sigma = 2 * a2 + b2 * (1 - 2 * width); for (int x = width, y = 0; a2 *y <= b2 *x; y++) { ScanLine(p.X - x, p.X + x, p.Y - y); ScanLine(p.X - x, p.X + x, p.Y + y); if (sigma >= 0) { sigma += fb2 * (1 - x--); } sigma += a2 * (4 * y + 6); } }
public static void Draw(this Graphics2DGL g, Point p, Pixel col) { g.Draw(p.X, p.Y, col); }
public static void DrawBitmap(this Graphics2DGL gfx, Bitmap imageToDraw, Rectangle sourceRect, Rectangle destRect, bool useUnsafe = false) { try { var bWidth = sourceRect.Width != -1 ? sourceRect.Width : imageToDraw.Width; var bHeight = sourceRect.Height != -1 ? sourceRect.Height : imageToDraw.Height; int dWidth = 0, dHeight = 0; if (useUnsafe) { var bmp = imageToDraw; unsafe { var rect = new Rectangle(0, 0, bmp.Width, bmp.Height); var bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); var scan0 = (byte *)bmpData.Scan0; var depth = Image.GetPixelFormatSize(bmp.PixelFormat); var length = bmp.Width * bmp.Height * depth / 8; for (var x = sourceRect.X; x < bWidth; x++) { if (destRect.Width != -1) { if (dWidth >= destRect.Width) { break; } } for (var y = sourceRect.Y; y < bHeight; y++) { var i = (y * bmp.Width + x) * depth / 8; var c = Color.Empty; switch (depth) { case 32: { var b = scan0[i]; var g = scan0[i + 1]; var r = scan0[i + 2]; var a = scan0[i + 3]; c = Color.FromArgb(a, r, g, b); break; } case 24: { var b = scan0[i]; var g = scan0[i + 1]; var r = scan0[i + 2]; c = Color.FromArgb(r, g, b); break; } case 8: { var b = scan0[i]; c = Color.FromArgb(b, b, b); break; } } var x0 = x - sourceRect.X; var y0 = y - sourceRect.Y; x0 += destRect.X; y0 += destRect.Y; if (destRect.Height > -1) { if (dHeight >= destRect.Height) { break; } } gfx.Draw(x0, y0, new Pixel(c)); if (destRect.Height != -1) { dHeight++; } } if (destRect.Width != -1) { dWidth++; } } bmp.UnlockBits(bmpData); } } else { for (var x = sourceRect.X; x < bWidth; x++) { if (destRect.Width != -1) { if (dWidth >= destRect.Width) { break; } } for (var y = sourceRect.Y; y < bHeight; y++) { var x0 = x - sourceRect.X; var y0 = y - sourceRect.Y; x0 += destRect.X; y0 += destRect.Y; if (destRect.Height != -1) { if (dHeight >= destRect.Height) { break; } } var pixel = imageToDraw.GetPixel(x, y); gfx.Draw(x0, y0, new Pixel(pixel.R, pixel.G, pixel.B)); if (destRect.Height != -1) { dHeight++; } } if (destRect.Width != -1) { dWidth++; } } } } catch (Exception ex) { throw new Graphics2DGLException(gfx, "GraphicsExt::DrawBitmap", ex); } }