Avatar of futuremoose
 asked on

Graphics on form not being saved to file


I have a very simple vb.net example, where I am using GDI+ to draw a simple pattern directly on the form... just a checkerboard of black/white squares. That bit works fine. See the attached screenshot.

However, if I try to save the form as an image file, the pattern I drew is not included (again, see screenshot). The pattern does not get erased off the form, but it is not included in the image file.

If the form is "refreshed", or another window covers up the pattern, then it is erased... that part I understand. But I don't know why the pattern does not show up on the image saved to a file.

I have included the Form1.vb file, which has all the code... to try it just create a new form with 3 buttons, and copy/paste the contents of my Form1.vb into yours.
Visual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Yes, your code works fine.

Actually, I had already found a workaround... by creating a Bitmap (bmp), getting the Graphics using Graphics.FromImage(bmp), drawing what I needed, and then using PictureBox.Image = bmp to display the result works fine. Plus them I can just do a bmp.Save.

But your answer uses the Form1_Paint event, which I should investigate as a possibly more proper approach. Thanks.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy