Graphics on form not being saved to file

futuremoose
futuremoose used Ask the Experts™
on
Hello,

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.
sshot.jpg
Form1.vb
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Try this out instead:
Public Class Form1

    Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim fill As Boolean = True
        For i = 1 To 8
            fill = Not fill
            For j = 1 To 8
                Add_single_cell(e.Graphics, New Point(i * 30, j * 30), New Size(30, 30), fill)
                fill = Not fill
            Next
        Next
    End Sub

    Private Sub Add_single_cell(ByVal G As Graphics, ByVal loc As Point, ByVal size As Size, ByVal fill As Boolean)
        Dim cell_rect As New Rectangle(loc, size)
        If fill Then
            G.FillRectangle(Brushes.Black, cell_rect)
        Else
            G.FillRectangle(Brushes.White, cell_rect)
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Call Save_image_to_file(New Size(Me.Width, Me.Height))
    End Sub

    Private Sub Save_image_to_file(ByVal size As Size)
        Dim bmp As System.Drawing.Bitmap
        bmp = New System.Drawing.Bitmap(size.Width, size.Height)
        Me.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
        bmp.Save("C:\Users\Mike\Pictures\myImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

End Class

Open in new window

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial