Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PictureBox Saving Blank BITMAP...this is a tough one! 2

Status
Not open for further replies.

mmbsports

Programmer
Oct 25, 2005
22
US
My problem is the same as mentioned here: thread222-98810 . When the picturebox is saved, it saves a blank image.

I have a form containing a picturebox which has other objects (textboxes, a frame, labels, and lines) contained within it. I want users to move the textboxes to any location within the picturebox (this works perfectly). I want the users to be able to save the contents in the picturebox as an image (it saves, but as a blank image and all objects are left off).

I am using this command: SavePicture picturebox.Image, "c:\myTestPicture.bmp"

What get saves is a blank image...the objects mentioned above do not get saved within the image.

I did a test and I placed code on the form allowing me to draw a line on the picturebox. When I save it using the SavePicture command, the lines drawn at runtime actually get saved within the image...but the objects (textboxes and so on) do not.

I completed a search within this forum, and researched all entries about pictureboxes and bitmaps, and there is no answer to my problem.

Please let me know what I am doing wrong....

Thanks in advance!
 
See thread222-563766.

Note that the code requires a picturebox (Picture1) in addition to other controls. This picturebox is invisible at runtime and used to temperorily draw the image of the control. This invisible picturebox (Picture1) must NOT be confused with your picturebox which contains other controls. These two are separate from each other.
 
Thank you!

Yes, I have seen the post you mentioned, and that recommendation does not work (as you stated in the post) with labels. As I mentioned in my original post, my picturebox contain several objects...and labels are one of them.

I very much appreciate your quick response. I may have to re-think what objects I have contained within my picturebox and try your solution.

If anyone else has further suggestions on how to create the bitmap or picture as I mentioned above, not only will I appreciate it, but according to hundreds of other programmers, they will too as this is a common problem.

Thanks in advance!
 
>I want the users to be able to save the contents in the picturebox as an image

>my picturebox contain several objects...and labels are one of them.

In the referred thread, I mentioned that the function is not capable of saving the contents of Label and Image control. On the other hand you are willing to save the contents of a "Picturebox", so this code will work fine. It will capture the contents of the picture box correctly, even if it contains Label and Images, without any problem. The problem will only occur if you pass an image or label control directly as an argument to the function.

So try the code as below... and it will work for sure.
[tt]
SavePicture GetCtlImage(yourpicturebox), "c:\myTestPicture.bmp"
[/tt]
I hope this code will meet your requirements. Note that, the above function can be modified in such a way so that it may support all controls, but some additional code will be required, for supporting light-weight controls like images and labels.
 
Hypetia,

It worked like a champ. I have another post requesting how to create a BMP, and this post will fit perfectly. I'll make sure to reference it.

Again, your code worked "as is" without changing anyything except for my refence to my picturebox....just like a plug and play! GREAT CODE!!!
 
Glad to see it work for you.

For your interest, here I post the modified version of GetCtlImage function, which supports all light-weight controls except the Line control. It also fixes some potential bugs over-looked in the original code.
___
[tt]
Function GetCtlImage(Ctl As Control) As StdPicture
Dim Cont As Object, SM As Long, DC As Long
Dim L As Long, T As Long, W As Long, H As Long
Set Cont = Ctl.Container
On Error Resume Next
SM = Cont.ScaleMode 'try to 'get' the scalemode of the container
If Err = 438 Then 'container doesn't have scalemode property
'convert to pixels using default scalemode (twips)
L = Ctl.Left \ Screen.TwipsPerPixelX
T = Ctl.Top \ Screen.TwipsPerPixelY
W = Ctl.Width \ Screen.TwipsPerPixelX
H = Ctl.Height \ Screen.TwipsPerPixelY
Else
'convert to pixels using container's own scalemode
L = Cont.ScaleX(Ctl.Left, SM, vbPixels)
T = Cont.ScaleY(Ctl.Top, SM, vbPixels)
W = Cont.ScaleX(Ctl.Width, SM, vbPixels)
H = Cont.ScaleY(Ctl.Height, SM, vbPixels)
End If
DC = GetDC(Cont.hwnd)
With Picture1
.Move 0, 0, .Parent.ScaleX(W, vbPixels, .Parent.ScaleMode), _
.Parent.ScaleY(H, vbPixels, .Parent.ScaleMode)
.Cls
BitBlt .hdc, 0, 0, .ScaleWidth, .ScaleHeight, DC, L, T, vbSrcCopy
End With
ReleaseDC Cont.hwnd, DC
Set GetCtlImage = Picture1.Image
On Error GoTo 0
End Function[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top