Results 1 to 13 of 13

Thread: Save Picturebox (with Lines and label) to bmp

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Question Save Picturebox (with Lines and label) to bmp

    Hi,

    My application has a requirement where I plot a hugh diagram on picturebox based on some data-analysis.
    This diagram is drawn by creating Line, label and command button controls on runtime and placing them on picture box. The contents of picture box are not really picture as I really act on the events recvd from label and command buttons.

    Now my problem is that when I try to save this image to bmp file I don't get all the contents correctly.
    Effectively I'm not able to capture all the contents of PictureBox correctly. I'm using BitBlt to this.

    So pls enlighten me on what is the best way to do this?

    Thanks in advance
    Mandeep

  2. #2
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: Save Picturebox (with Lines and label) to bmp

    Try this:

    SavePicture Picture1.Image, "C:\TestPic.bmp"
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  3. #3
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Save Picturebox (with Lines and label) to bmp

    If you also want to grab some controls sitting on top of the picturebox (say, Pic1), place an invisible picturebox on the form with the autoredraw and autosize properties set to true (call it Pic2) and then get a screenshot of the desktop and place the relevant part (contents of Pic1) on Pic2. Thereafter, save the contents of Pic2 to hard disk.

    In the code below I'm using TwipsX and TwipsY because the dimensions must be in pixels and the example I've copied the code from had them in twips.
    VB Code:
    1. TwipsX = Screen.TwipsPerPixelX
    2.         TwipsY = Screen.TwipsPerPixelY
    3.         ret = GetDC(GetDesktopWindow)
    4.         BitBlt Pic2.hDC, 0, 0, Pic1.Width / TwipsX, Pic1.Height / TwipsY, ret, _
    5.         (Me.Left + Pic1.Left) / TwipsX, (Me.Height - Me.ScaleHeight + Pic1.Top) / TwipsY, SRCCOPY
    6.         SavePicture Me.Pic2.Image, FileName
    Last edited by krtxmrtz; Apr 11th, 2005 at 06:18 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Re: Save Picturebox (with Lines and label) to bmp

    Thanks for "quick" reply guys.

    Well let me elaborate the problem. Assume I'm using pic1 to draw the image or diagram.
    My PIC1 has autoredraw and autosize set to true, because its hugh picture even bigger than screen dimension. So I have placed scrollbars around it for user to view it fully.

    Now when I try to do bitblt this hDC to another PIC2, I don't get full picture.
    Pls see attached JPG file. You can easily see distortion in image caused by other controls present on the form.

    So tell some way by which only the contents of PictureBox can be saved into a image or image file.

    -Mandeep
    Attached Images Attached Images  

  5. #5
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Save Picturebox (with Lines and label) to bmp

    Quote Originally Posted by ceo_nikka
    Now when I try to do bitblt this hDC to another PIC2, I don't get full picture.
    Pls see attached JPG file. You can easily see distortion in image caused by other controls present on the form.
    Maybe I misunderstood you. From the image you've posted I don't see any controls on the picturebox, only textboxes below. Unless the numbers representing IP addresses are labels...?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Re: Save Picturebox (with Lines and label) to bmp

    Those lines are Line control and Text is labels.

    As you can see from the image. By BitBlt I'm unable to capture the full picture. Using hDC, I'm able to capture only the portion visible to user and not all the contents of Picturebox.

    -Mandeep

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Re: Save Picturebox (with Lines and label) to bmp

    In picture the colored region is PictureBox. And as u can see it is obstructed by scrollbars, taskbar, textbox present in the application.

    So how to capture *Just* contents of picturebox and nothing else??

    -Mandeep

  8. #8
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Save Picturebox (with Lines and label) to bmp

    What are the source and destination of this BitBlt of yours?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Re: Save Picturebox (with Lines and label) to bmp

    Both source and destination are picturebox.

    As you suggested earlier, I'm using a Invisible Picturebox (Autoredraw and Autosize to TRUE) as destination.

    To draw I place controls on picbox1 and to save I do bitblt to picbox2.
    From picbox2, I save to file.

  10. #10
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Save Picturebox (with Lines and label) to bmp

    I think there are 2 options.

    1. Without using GetDC you BilBlt from Pic1 to a invisible autoredrawing Pic2 and then save this one to hd. This should get the entire contents of Pic1, also the part that doesn't fit in it, but will probably leave out the controls. That's not what you want, is it?

    2. Using GetDC as in the example I posted above. In this case you should get the picture with the controls but only the visible part. To get the entire picture, all I can think of is:

    - Use an array of invisible picture boxes Pic2() that you can Redim as and when it is needed.
    - With the GetDC and BitBlt lines above capture the visible part to Pic2(0)
    - Keep doing this (place it in a loop) while at the same time automatically scrolling Pic1 by the exact amount.
    - Finally, join all the portions: copy (BitBlt) the contents of Pic2(0), Pic2(1), etc to a large invisible picturebox, each to the appropiate coordinates.

    Of course, I haven't tried this out because it must take a bit of figuring out the correct numbers, that's for sure, but I think it should work.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Re: Save Picturebox (with Lines and label) to bmp

    Ok.
    I think I should give option 2, a try. You are correct it may take a while to test this.

    So before I do that do let me know how to join these various pieces of picture to one???

    -Mandeep

  12. #12
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Save Picturebox (with Lines and label) to bmp

    Quote Originally Posted by ceo_nikka
    So before I do that do let me know how to join these various pieces of picture to one???
    Ok, I'd like to try to write some code but I don't have the time right now.
    In the first pass of the loop you BitBlt to Pic2(0), then you automatically scroll the window horizontally such that the next portion is visible. You copy that on to Pic1(1) and so on. Then you scroll it all the way to the left and then start scrolling it vertically and preoceed likewise.
    The final step would be:
    BitBlt from Pic2(0) to coordinates (0,0) of another picturebox "PicAll".
    BilBlt from Pic2(1) to coordinates (Pic1.Width,0) of PicAll.
    BitBlt from Pic2(2) to coordinates (2*Pic1.Width,)) of PicAll.
    and so on...
    Probably the picture size is not an exact multiple of Pic1.Width so, for the last horizontal scroll, you'll have to capture only a part of the visible image and you'll have to derive the left corrdinate and the width from the scroll value. If you determine these to be, say, x and w, then you'll have to make a final BilBlt:
    BilBlt from Pic(N) to coordinates ((N-1)*Pic1.Width + x,0) and use width w (rather than Pic1.Width as in the previous BitBlt's)

    Then you'll have to do something similar for the vertical scrolls.

    I know this is somewhat confusing, but you should play around until you get it.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  13. #13

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Smile Re: Save Picturebox (with Lines and label) to bmp

    Thanks Boss.

    I understood your logic. I'm giving it a try.
    But still hope that someday VB will provide some simpler method of doing this picture copy business.

    Thanks once again

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