Advertisement
Guest User

Untitled

a guest
Sep 8th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. //This is where the drawing happens - RAM's usage is going up here very slowly
  2. private void Form1_KeyPress(object sender, KeyPressEventArgs
  3. {        
  4.             Pen pen = new Pen(Brushes.Red);
  5.  
  6.             using (Graphics g = Graphics.FromImage(panel.BackgroundImage))
  7.             {
  8.                 g.DrawEllipse(pen, x, y, 50, 50);
  9.         x += 20;
  10.         y += 20;
  11.             }
  12.  
  13.             panel.Refresh();
  14.             pen.Dispose();
  15. }
  16.  
  17. //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
  18. private void panel_Click(object sender, EventArgs e)
  19. {
  20.             var mouse = e as MouseEventArgs;
  21.  
  22.             Bitmap bitmap = new Bitmap(panel.Width, panel.Height);
  23.             panel.DrawToBitmap(bitmap, new Rectangle(0, 0, panel.Width, panel.Height));
  24.             panel.BackgroundImage.Dispose();
  25.  
  26.             FloodFill(bitmap, mouse.X, mouse.Y, Color.Red);
  27.      
  28.             panel.BackgroundImage = bitmap;
  29. }
  30.  
  31. //And this is flood fill (probably irrelevant)
  32. void FloodFill(Bitmap bitmap, int x, int y, Color color)
  33. {
  34.             BitmapData data = bitmap.LockBits(
  35.                 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
  36.                 ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  37.             int[] bits = new int[data.Stride / 4 * data.Height];
  38.             Marshal.Copy(data.Scan0, bits, 0, bits.Length);
  39.  
  40.             LinkedList<Point> check = new LinkedList<Point>();
  41.             int floodTo = color.ToArgb();
  42.             int floodFrom = bits[x + y * data.Stride / 4];
  43.             bits[x + y * data.Stride / 4] = floodTo;
  44.  
  45.             if (floodFrom != floodTo)
  46.             {
  47.                 check.AddLast(new Point(x, y));
  48.                 while (check.Count > 0)
  49.                 {
  50.                     Point cur = check.First.Value;
  51.                     check.RemoveFirst();
  52.  
  53.                     foreach (Point off in new Point[] {
  54.                 new Point(0, -1), new Point(0, 1),
  55.                 new Point(-1, 0), new Point(1, 0)})
  56.                     {
  57.                         Point next = new Point(cur.X + off.X, cur.Y + off.Y);
  58.                         if (next.X >= 0 && next.Y >= 0 &&
  59.                             next.X < data.Width &&
  60.                             next.Y < data.Height)
  61.                         {
  62.                             if (bits[next.X + next.Y * data.Stride / 4] == floodFrom)
  63.                             {
  64.                                 check.AddLast(next);
  65.                                 bits[next.X + next.Y * data.Stride / 4] = floodTo;
  66.                             }
  67.                         }
  68.                     }
  69.                 }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement