Results 1 to 4 of 4

Thread: Save drawing from PictureBox

  1. #1

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Resolved Save drawing from PictureBox

    Hey there,

    I'm trying to save the lines I drew in a PictureBox as a bitmap, but it just returns blank images...

    This is the code I have now:
    vb Code:
    1. 'Draw a grid on PictureBox named draw
    2. For x As Integer = 0 To bWidth
    3.     For y As Integer = 0 To bHeight
    4.         draw.CreateGraphics.DrawLine(Pens.Black, New Point(xStart + x * bScale, yStart), New Point(xStart + x * bScale, yStart + ySize))
    5.         draw.CreateGraphics.DrawLine(Pens.Black, New Point(xStart, yStart + y * bScale), New Point(xStart + xSize, yStart + y * bScale))
    6.     Next
    7. Next
    8.  
    9. 'Save as BMP-file
    10. Dim bmp As New Bitmap(draw.Width, draw.Height)
    11. draw.DrawToBitmap(bmp, New Rectangle(0, 0, draw.Width, draw.Height))
    12. bmp.Save("D:\output.png", Imaging.ImageFormat.Png)
    13. bmp.Dispose()
    The file output.png in now a blank image, the size of the PictureBox. If I try to save the entire form with Me.DrawToBitmap(), I get the form itself but the PictureBox is blank, while the grid is visible on screen.

    WHAT DO?!

    Thanks for your help,
    Alexander.
    Last edited by arsmakman; Jun 4th, 2011 at 03:50 AM. Reason: Issue resolved
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Save drawing from PictureBox

    Follow the CodeBank link in my signature and check out my Simple Drawing thread. It shows you how to draw on a PictureBox and then transfer that drawing to the Image in a the PictureBox. You can then just Save that Image.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: Save drawing from PictureBox

    That got it working! The *working* code I have now is:

    vb Code:
    1. 'Draw grid
    2. Using g As Graphics = Graphics.FromImage(draw.Image)
    3.     For x As Integer = 0 To bWidth
    4.         For y As Integer = 0 To bHeight
    5.             g.DrawLine(Pens.Black, New Point(xStart + x * bScale, yStart), New Point(xStart + x * bScale, yStart + ySize))
    6.             g.DrawLine(Pens.Black, New Point(xStart, yStart + y * bScale), New Point(xStart + xSize, yStart + y * bScale))
    7.         Next
    8.     Next
    9. End Using
    10.  
    11. 'Save as PNG-file
    12. Dim bmp As New Bitmap(draw.Width, draw.Height)
    13. draw.DrawToBitmap(bmp, New Rectangle(0, 0, draw.Width, draw.Height))
    14. bmp.Save("D:\output.png", Imaging.ImageFormat.Png)
    15. bmp.Dispose()

    Thanks for your help!
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  4. #4
    New Member
    Join Date
    Jul 2011
    Posts
    1

    Re: Save drawing from PictureBox

    Can you mail me the full code. i am new with vb.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width