/// <summary> /// Draws another bitmap into this image using straight-up pixel data. /// Not as fast as the Graphics.DrawImage(); /// </summary> /// <param name="img">The source image.</param> /// <param name="x">x location in pixels.</param> /// <param name="y">y location in pixels.</param> public void DrawImage(Bitmap img, int x, int y) { FastBitmap fastSource = new FastBitmap(img); fastSource.LockImage(); for (int y0 = y; y0 < _image.Height; ++y0) { if (y0 == Height) continue; for (int x0 = x; x0 < _image.Width; ++x0) { if (x0 == Width) continue; SetPixel(x0, y0, fastSource.GetPixel(x0 - x, y0 - y)); } } fastSource.UnlockImage(); }
private void replaceColorItem_Click(object sender, EventArgs e) { FastBitmap bmap = new FastBitmap(_edit_layer); bmap.LockImage(); bmap.ReplaceColor(bmap.GetPixel(_mouse.X, _mouse.Y), DrawColor); bmap.UnlockImage(); _start_anchor = Point.Empty; _end_anchor.X = _image.Width - 1; _end_anchor.Y = _image.Height - 1; PushHistory(); if (ImageChanged != null) ImageChanged(this, EventArgs.Empty); }
private void selectColorItem_Click(object sender, EventArgs e) { FastBitmap bmap = new FastBitmap(_edit_layer); bmap.LockImage(); DrawColor = bmap.GetPixel(_mouse.X, _mouse.Y); bmap.UnlockImage(); if (ColorChanged != null) ColorChanged(this, EventArgs.Empty); }
/// <summary> /// Sets the drawing image as a copy of the image. /// </summary> public void SetImage(Bitmap image) { if (image == null) return; int _oldWidth = 0, _oldHeight = 0; if (_image != null) { _oldWidth = _image.Width; _oldHeight = _image.Height; _image.Dispose(); _edit_layer.Dispose(); _edit_canvas.Dispose(); } _image = new Bitmap(image); _edit_layer = new Bitmap(_image); _edit_canvas = Graphics.FromImage(_edit_layer); _edit_canvas.InterpolationMode = InterpolationMode.NearestNeighbor; if (blendItem.Checked) _edit_canvas.CompositingMode = CompositingMode.SourceOver; else _edit_canvas.CompositingMode = CompositingMode.SourceCopy; // here I construct a new dotted bg image. But only if we need to. if (_image.Width != _oldWidth || _image.Height != _oldHeight) { _bg?.Dispose(); _bg = new Bitmap(_image.Width, _image.Height, PixelFormat.Format32bppPArgb); FastBitmap bmap = new FastBitmap(_bg); bmap.LockImage(); for (int x = 0; x < _image.Width; x++) for (int y = 0; y < _image.Height; y++) if (y % 2 == (x % 2 == 0 ? 0 : 1)) bmap.SetPixel(x, y, Color.LightGray); bmap.UnlockImage(); } ResizeToFit(); }
private void DoTool() { Rectangle rect; switch (Tool) { case ImageTool.Pen: DrawPenLine(); break; case ImageTool.Line: _edit_canvas.DrawLine(_draw_pen, _start_anchor, _mouse); _end_anchor = _end_anchor = _mouse; break; case ImageTool.Rectangle: rect = Line.ToRectangle(new Line(_start_anchor, _mouse)); if (!Outlined) { rect.Width += 1; rect.Height += 1; _edit_canvas.FillRectangle(_draw_brush, rect); } else _edit_canvas.DrawRectangle(_draw_pen, rect); _end_anchor = _mouse; break; case ImageTool.Floodfill: FastBitmap bmap = new FastBitmap(_edit_layer); bmap.LockImage(); rect = bmap.FloodFill(_mouse.X, _mouse.Y, DrawColor); bmap.UnlockImage(); _start_anchor = rect.Location; _end_anchor.X = rect.Right; _end_anchor.Y = rect.Bottom; break; } }
public void ToGrayscale() { FastBitmap bmap = new FastBitmap(image); bmap.LockImage(); bmap.Grayscale(); bmap.UnlockImage(); }
private void DoTool(Graphics g) { g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; int cx = x_off + cur_x, cy = y_off + cur_y; edit_region.Width++; edit_region.Height++; edit_region.X += x_off; edit_region.Y += y_off; g.SetClip(edit_region); switch (_tool) { case Tool.Pen: g.DrawLine(SelPen, x_off + last_x, y_off + last_y, cx, cy); break; case Tool.Line: g.DrawLine(SelPen, x_off + Origin.X, y_off + Origin.Y, cx, cy); break; case Tool.Rect: if (!Outline) { using (SolidBrush brush = new SolidBrush(color)) { g.FillRectangle(brush, edit_region); } } else { edit_region.Width--; edit_region.Height--; if (edit_region.Width == 0 || edit_region.Height == 0) g.DrawLine(SelPen, edit_region.X, edit_region.Y, edit_region.Right, edit_region.Bottom); else g.DrawRectangle(SelPen, edit_region); edit_region.Width++; edit_region.Height++; } break; case Tool.Fill: FastBitmap bmap = new FastBitmap(edit_layer); bmap.LockImage(); edit_region = bmap.FloodFill(cx, cy, color); edit_region.Width++; edit_region.Height++; bmap.UnlockImage(); break; // case 4: ellipse } edit_region.Width--; edit_region.Height--; edit_region.X -= x_off; edit_region.Y -= y_off; g.ResetClip(); g.Dispose(); }
public void ReplacePixels(Color oldCol, Color newCol) { UpdateHistoryBefore(); FastBitmap bmap = new FastBitmap(image); bmap.LockImage(); bmap.ReplaceColor(oldCol, newCol); bmap.UnlockImage(); edit_layer.Dispose(); edit_layer = new Bitmap(image); UpdateHistoryAfter(); }
public void MonoNoise() { FastBitmap bmap = new FastBitmap(image); bmap.LockImage(); Random rand = new Random(); Color c; int num, r, g, b; num = r = g = b = 0; for (int y = 0; y < image.Height; ++y) { for (int x = 0; x < image.Width; ++x) { c = bmap.GetPixel(x, y); num = rand.Next(0, 51); r = Math.Min(c.R + num, 255); g = Math.Min(c.G + num, 255); b = Math.Min(c.B + num, 255); bmap.SetPixel(x, y, Color.FromArgb(c.A, r, g, b)); } } bmap.UnlockImage(); }
public void InvertColors() { FastBitmap bmap = new FastBitmap(image); Color c; bmap.LockImage(); for (int y = 0; y < image.Height; ++y) { for (int x = 0; x < image.Width; ++x) { c = bmap.GetPixel(x, y); bmap.SetPixel(x, y, Color.FromArgb(c.A, 255 - c.R, 255 - c.G, 255 - c.B)); } } bmap.UnlockImage(); }
public void ColorNoise() { FastBitmap bmap = new FastBitmap(image); bmap.LockImage(); Random rand = new Random(); Color col; int a, r, g, b; a = r = g = b = 0; for (int y = 0; y < image.Height; ++y) { for (int x = 0; x < image.Width; ++x) { col = bmap.GetPixel(x, y); a = rand.Next(col.A, Math.Min(col.A + 26, 255)); r = rand.Next(Math.Max(0, col.R - 25), Math.Min(col.R + 26, 255)); g = rand.Next(Math.Max(0, col.G - 25), Math.Min(col.G + 26, 255)); b = rand.Next(Math.Max(0, col.B - 25), Math.Min(col.B + 26, 255)); bmap.SetPixel(x, y, Color.FromArgb(a, r, g, b)); } } bmap.UnlockImage(); }
public void Blur(int h, int v) { FastBitmap fastBmp = new FastBitmap(image); fastBmp.LockImage(); int h2 = (h << 1) + 1; int v2 = (v << 1) + 1; float weight; float[] weights = new float[h2]; for (int i = 0; i < h2; ++i) weights[i] = Gauss(-h + i, 0, h); double weighted; double a, r, g, b; byte ba, br, bg, bb; Color c; // first we do a horizontal pass: for (int row = 0; row < image.Height; ++row) { for (int col = 0; col < image.Width; ++col) { a = r = g = b = 0; weight = 0; for (int i = 0; i < h2; ++i) { int x = col - h + i; if (x < 0) { i += -x; x = 0; } if (x > image.Width - 1) break; c = fastBmp.GetPixel(x, row); weighted = weights[i] / 255 * c.A; r += c.R * weighted; g += c.G * weighted; b += c.B * weighted; a += c.A * weights[i]; weight += weights[i]; } br = (byte)Math.Min(Math.Round(r/weight), 255); bg = (byte)Math.Min(Math.Round(g/weight), 255); bb = (byte)Math.Min(Math.Round(b/weight), 255); ba = (byte)Math.Min(Math.Round(a/weight), 255); fastBmp.SetPixel(col, row, Color.FromArgb(ba, br, bg, bb)); } } if (v2 != h2) weights = new float[v2]; for (int i = 0; i < v2; ++i) weights[i] = Gauss(-v + 1, 0, v); // then we do a vertical pass: for (int col = 0; col < image.Width; ++col) { for (int row = 0; row < image.Height; ++row) { a = r = g = b = 0; weight = 0; for (int i = 0; i < v2; ++i) { int y = row - v + i; if (y < 0) { i += -y; y = 0; } if (y > image.Height - 1) break; c = fastBmp.GetPixel(col, y); weighted = weights[i] / 255 * c.A; r += c.R * weighted; g += c.G * weighted; b += c.B * weighted; a += c.A * weights[i]; weight += weights[i]; } br = (byte)Math.Min(Math.Round(r/weight), 255); bg = (byte)Math.Min(Math.Round(g/weight), 255); bb = (byte)Math.Min(Math.Round(b/weight), 255); ba = (byte)Math.Min(Math.Round(a/weight), 255); fastBmp.SetPixel(col, row, Color.FromArgb(ba, br, bg, bb)); } } fastBmp.UnlockImage(); }