Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is where the drawing happens - RAM's usage is going up here very slowly
- private void Form1_KeyPress(object sender, KeyPressEventArgs
- {
- Pen pen = new Pen(Brushes.Red);
- using (Graphics g = Graphics.FromImage(panel.BackgroundImage))
- {
- g.DrawEllipse(pen, x, y, 50, 50);
- x += 20;
- y += 20;
- }
- panel.Refresh();
- pen.Dispose();
- }
- //However, this is the flood fill: every time I call that, RAM's usage is going up pretty fast: 1-1.5MB per each call
- private void panel_Click(object sender, EventArgs e)
- {
- var mouse = e as MouseEventArgs;
- Bitmap bitmap = new Bitmap(panel.Width, panel.Height);
- panel.DrawToBitmap(bitmap, new Rectangle(0, 0, panel.Width, panel.Height));
- panel.BackgroundImage.Dispose();
- FloodFill(bitmap, mouse.X, mouse.Y, Color.Red);
- panel.BackgroundImage = bitmap;
- }
- //And this is flood fill (probably irrelevant)
- void FloodFill(Bitmap bitmap, int x, int y, Color color)
- {
- BitmapData data = bitmap.LockBits(
- new Rectangle(0, 0, bitmap.Width, bitmap.Height),
- ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
- int[] bits = new int[data.Stride / 4 * data.Height];
- Marshal.Copy(data.Scan0, bits, 0, bits.Length);
- LinkedList<Point> check = new LinkedList<Point>();
- int floodTo = color.ToArgb();
- int floodFrom = bits[x + y * data.Stride / 4];
- bits[x + y * data.Stride / 4] = floodTo;
- if (floodFrom != floodTo)
- {
- check.AddLast(new Point(x, y));
- while (check.Count > 0)
- {
- Point cur = check.First.Value;
- check.RemoveFirst();
- foreach (Point off in new Point[] {
- new Point(0, -1), new Point(0, 1),
- new Point(-1, 0), new Point(1, 0)})
- {
- Point next = new Point(cur.X + off.X, cur.Y + off.Y);
- if (next.X >= 0 && next.Y >= 0 &&
- next.X < data.Width &&
- next.Y < data.Height)
- {
- if (bits[next.X + next.Y * data.Stride / 4] == floodFrom)
- {
- check.AddLast(next);
- bits[next.X + next.Y * data.Stride / 4] = floodTo;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement