Click here to Skip to main content
15,795,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently working with c# PictureBox and facing one problem. I have made handsfree drawing in an image inside a picture box. It draws nicely on the picture box.But when I minimizing the form and reload it again then the drawings disappear. Any one can suggest remedy for this problem… Thanks…

What I have tried:

I just applied this code but no change ...
Bitmap bmp = new Bitmap(ImagepictureBox.Image);
Updated 21-Jan-19 22:21pm

When you draw on a PictureBox, you aren't drawing on the underlying bitmap - you are drawing over the top of the bitmap. And unless you do the drawing inside the Paint event for the PictureBox using information you stored while the user was "freehand drawing" it it will not be refreshed when the form is minimized (or indeed when a different app is placed over yours and then removed). If you draw on the picturebox in the Mouse event handlers for example, then the drawing will not be persistent - it will vanish the next time the Paint event occurs.

There are two things you can to do prevent that:
1) Draw onto the bitmap that the PictureBox is displaying. This mean getting the Graphics context for the image, drawing onto it, then releasing the context (and probably Invalidating the PictureBox to force a redraw via a Paint event.
2) Store your mouse movements in a collection, Invalidate the PictureBox, and then draw all the movements from the collection in the PictureBox Paint event handler.

The former is easier to do, but you can't undo changes the user made.
The latter is more complicated, but allows you to undo user mistakes and does not change the bitmap image at all.

Have a look at this: Creating a Simple "scratch card" Control in WinForms[^] it shows a way to draw over an image in a way that doesn't touch the original image at all, but which can be persistent.
Share this answer
As you only posted one line of your code I can only guess, but I think you did not use the
PictureBox OnPaint event handler which gives you the Graphics object to paint on.
See information here: Overriding the OnPaint Method | Microsoft Docs[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900