• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Draw a graphic then save it

If I draw on a control using

Graphics g = panel1.CreateGraphics();
g.DrawEllipse(...);
etc...

how can I copy what I have drawn to another control such as panel2?

with thanks in advance

Dan
0
seabear
Asked:
seabear
  • 2
  • 2
1 Solution
 
brckoslavCommented:
Best solution

Create Bitmap b of size Control.ClientRectangle.

Graphics g = Graphics.FromImage(b);
g.DrawElipse();

next
Graphics g1 = panel1.CreateGraphics();
g1.DrawBitmap(b, ...);

Graphics g2 = panel2.CreateGraphics();
g2.DrawBitmap(b, ...);
 

note: but you should not use panel.CreateGraphics, instead of this override OnPaint for panel and when you want refresh panel just call Invalidate on it
0
 
seabearAuthor Commented:
Hi brckoslav

Some of the drawing I am doing is on the MouseMove event (draw a line following the mouse) and repeatedly drawing on the bitmap and then using the bitmap to refresh the control has me worrying about flicker.

I could use your suggestion but draw twice so that whatever I draw on the control I also draw on the bitmap.

That should work and I can use it but it seems to me that there should be a bitmap associated with the device context that I am drawing on and it would be nice to get hold of that so I do not have to duplicate my drawing.

regards

Dan
0
 
brckoslavCommented:
to the Bitmap draw only static content (like contents of file), rectangle in mouse move draw normal

like:

Graphics g = Graphics.FromImage(b);
g.DrawElipse();

next
Graphics g1 = panel1.CreateGraphics();
g1.DrawBitmap(b, ...);
g1.DrawRectangle(mouseStartPoint.X, mouseStartPoint.Y, mouseEndPoint.X, mouseEndPoint.Y);

you should not draw this rectangle to the bitmap, because in second control is mouse somewhere else

0
 
seabearAuthor Commented:
Hi brckoslav,
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now