By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

Save Picturebox

P: n/a
Dear all..
I have a picturebox filled on a form. The picturebox has some
graphics displayed on it.I was trying to save the picturebox, but
continuesly failed to do so. I am clueless of why it is happening. I
tried to save the image of the form & also the picturebox, but always,
i see a blank file saved. Is there a way to save whatever is displayed
on the picturebox, to a file??
Thanks for any suggestions and help.

Regards,
Pavan
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 17 Jun 2004 21:45:25 -0700, pa*********@yahoo.com (Pavan Arise)
wrote:
Dear all..
I have a picturebox filled on a form. The picturebox has some
graphics displayed on it.I was trying to save the picturebox, but
continuesly failed to do so. I am clueless of why it is happening. I
tried to save the image of the form & also the picturebox, but always,
i see a blank file saved. Is there a way to save whatever is displayed
on the picturebox, to a file??
Thanks for any suggestions and help.


Set the Picturebox's AutoRedraw = True

SavePicture Picture1.Image, FileName
Jul 17 '05 #2

P: n/a
I am still not able to save the picturebox/ I am hereby attaching the
code here.
This is the code which I have in the sub:
=================
In Declarations Section
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As
Long, ByVal hWndNewParent As Long) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long,
ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight
As Long, ByVal bRepaint As Long) As Long

' Following code in Sub/
------
Dim Dum As New FrmRotSyncPlt

Load FrmRotSyncPlt ' FrmRotSyncPlt is the form where the whole
graphics is printed & also has the picturebox

Call SetParent(FrmRotSyncPlt.hwnd, Dum.hwnd)

Call MoveWindow(FrmRotSyncPlt.hwnd, 0, 0, 800, 600, True)
' I alreday have the SetParent & MoveWindow declared in the
declarations section (As previously given by Seth)
Dum.Show
SavePicture Dum.Image, "c:/trial.bmp"
Debug.Print "done"

End sub
============
I also tried it with copying the whole form into a picturebox on Dum
, as suggested by Seth above, but without any sucess. The saved
trial.Bmp is always blank. Thanks for any suggestions & help.
Jul 17 '05 #3

P: n/a
wait a second ...

From what you post ... on one hand you Dim a variable 'Dum' as a form type,
then proceed to not properly initialize it to a form. You then do APIs to
FrmRotSyncPlt, setting parent to the non-existent form, then attempt to save
a picture from this same non-existent (non-visible, non-referenced) form
using the image property of a picturebox.

What makes you think that following a SetParent call, that a picturebox
contained on FrmRotSyncPlt will somehow become the picturebox on the other
form?

1) saving the Image property of a form only saves -- quite literally -- an
image the form itself (no contained controls). If you were to draw onto the
form canvas you would 'see' the saved form picture, but any controls on the
form would not be visible.

2) if you have a valid picture in a picturebox, execute:

picture1.picture = picture1.image
savepicture picture1.picture, "c:\temp.bmp"

Once you 'setparent' a form all bets are off as to the success of any given
activity. Using SetParent to reparent one form to another is at best a hack,
and unsupported.

Better explain things more clearly.

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Pavan Arise" <pa*********@yahoo.com> wrote in message
news:eb**************************@posting.google.c om...
: I am still not able to save the picturebox/ I am hereby attaching the
: code here.
: This is the code which I have in the sub:
: =================
: In Declarations Section
: Private Declare Function SetParent Lib "user32" (ByVal hWndChild As
: Long, ByVal hWndNewParent As Long) As Long
: Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long,
: ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight
: As Long, ByVal bRepaint As Long) As Long
:
: ' Following code in Sub/
: ------
: Dim Dum As New FrmRotSyncPlt
:
: Load FrmRotSyncPlt ' FrmRotSyncPlt is the form where the whole
: graphics is printed & also has the picturebox
:
: Call SetParent(FrmRotSyncPlt.hwnd, Dum.hwnd)
:
: Call MoveWindow(FrmRotSyncPlt.hwnd, 0, 0, 800, 600, True)
: ' I alreday have the SetParent & MoveWindow declared in the
: declarations section (As previously given by Seth)
: Dum.Show
: SavePicture Dum.Image, "c:/trial.bmp"
: Debug.Print "done"
:
: End sub
: ============
: I also tried it with copying the whole form into a picturebox on Dum
: , as suggested by Seth above, but without any sucess. The saved
: trial.Bmp is always blank. Thanks for any suggestions & help.

Jul 17 '05 #4

P: n/a
Dear Randy Birch..thanks for the suggestion, I think I have messed up
the code completely. What I was trying to do was to save a form(it has
some plot drawn on it) along with a picturebox on it. As you said,
when I tried with Picturesave, I am not able to save the controls. My
try with the setparent is not yeilding any results either. I am now
wondering(and trying) if Bitblt would serve the purpose of saving the
form along with its control content.Can you please help me.
Thanks in advance for any suggestions/
Jul 17 '05 #5

P: n/a
If you use this ... http://vbnet.mvps.org/code/bitmap/printscreenole.htm ...
and in the GetOLEScreenSnapshot routine substitute:

'CaptureWindow
WidthSrc = me.Width \ Screen.TwipsPerPixelX
HeightSrc = me.Height \ Screen.TwipsPerPixelY

'Get a handle to the desktop window and get the proper device context
hWndSrc = Me.hwnd

.... for ...

'CaptureWindow
WidthSrc = Screen.Width \ Screen.TwipsPerPixelX
HeightSrc = Screen.Height \ Screen.TwipsPerPixelY

'Get a handle to the desktop window and get the proper device context
hWndSrc = GetDesktopWindow()
.... you'll get a pix of the form in Picture1 - including its controls and
control contents - which you can then save using the code I provided
earlier:

picture1.picture = picture1.image
savepicture picture1.picture, "c:\temp.bmp"

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Pavan Arise" <pa*********@yahoo.com> wrote in message
news:eb**************************@posting.google.c om...
: Dear Randy Birch..thanks for the suggestion, I think I have messed up
: the code completely. What I was trying to do was to save a form(it has
: some plot drawn on it) along with a picturebox on it. As you said,
: when I tried with Picturesave, I am not able to save the controls. My
: try with the setparent is not yeilding any results either. I am now
: wondering(and trying) if Bitblt would serve the purpose of saving the
: form along with its control content.Can you please help me.
: Thanks in advance for any suggestions/

Jul 17 '05 #6

P: n/a
Thank you Randy Birch for the invaluable piece of code. I have the
program working perfectly as I wanted. But there is a small problem
here. I was trying to save just the form content only. By the way it
is now, the title bar (Wher the caption is written) is also getting
saved. I tried to change the form borderstyle setting to 0 (None) &
refreshing the form before calling the GetOLEScreenSnapshot sub, but
for some reason, the form border isn't changing and the form along
with the titlebar is getting saved. There were some other buttons on
the form which were not saved when I had them as visible =false during
runtime. Is it that the borderstyle doesn't change during runtime, or
are there any other changes to be made for having the titlebar not to
be saved with the form. Thank you again and any suggestions are
appreciated.
Jul 17 '05 #7

P: n/a
Try this slightly tweaked routine. The values set for the voffset and
hoffset values work on my system using small fonts. You may need to tweak
the values to correctly clip for large font systems. Basically this uses the
form's scaleheight and scalewidth rather than its width/height to size the
bitmap, then offsets the bitblt'ed image to subtract the borders and
titlebar.

Private Function GetOLEScreenSnapshot() As Picture

Dim hWndSrc As Long
Dim hDCSrc As Long
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim WidthSrc As Long
Dim HeightSrc As Long
Dim Pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID

Dim vOffset As Long
Dim hOffset As Long

'CaptureWindow
WidthSrc = Me.ScaleWidth \ Screen.TwipsPerPixelX
HeightSrc = Me.ScaleHeight \ Screen.TwipsPerPixelY

vOffset = ((Me.Width - Me.ScaleWidth) / 1.75) \ Screen.TwipsPerPixelX
hOffset = ((Me.Height - Me.ScaleHeight) / 1.15) \ Screen.TwipsPerPixelY

'Get a handle to the desktop window and get the proper device context
hWndSrc = Me.hWnd
hDCSrc = GetWindowDC(hWndSrc)

'Create a memory device context for the copy process
hDCMemory = CreateCompatibleDC(hDCSrc)

'Create a bitmap and place it in the memory DC
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)

Print vOffset, hOffset

'Copy the on-screen image into the memory DC
Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _
hDCSrc, vOffset, hOffset, vbSrcCopy)

'Remove the new copy of the the on-screen image
hBmp = SelectObject(hDCMemory, hBmpPrev)

'Release the device context resources back to the system
Call DeleteDC(hDCMemory)
Call ReleaseDC(hWndSrc, hDCSrc)

'Fill in OLE IDispatch Interface ID
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With

'Fill Pic with necessary parts
With Pic
.Size = Len(Pic) 'Length of structure
.Type = vbPicTypeBitmap 'Type of Picture (bitmap)
.hBmp = hBmp 'Handle to bitmap
.hPal = 0& 'Handle to palette (may be null)
End With

'Create OLE Picture object
Call OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

'Return the new Picture object
Set GetOLEScreenSnapshot = IPic

End Function

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Pavan Arise" <pa*********@yahoo.com> wrote in message
news:eb*************************@posting.google.co m...
: Thank you Randy Birch for the invaluable piece of code. I have the
: program working perfectly as I wanted. But there is a small problem
: here. I was trying to save just the form content only. By the way it
: is now, the title bar (Wher the caption is written) is also getting
: saved. I tried to change the form borderstyle setting to 0 (None) &
: refreshing the form before calling the GetOLEScreenSnapshot sub, but
: for some reason, the form border isn't changing and the form along
: with the titlebar is getting saved. There were some other buttons on
: the form which were not saved when I had them as visible =false during
: runtime. Is it that the borderstyle doesn't change during runtime, or
: are there any other changes to be made for having the titlebar not to
: be saved with the form. Thank you again and any suggestions are
: appreciated.

Jul 17 '05 #8

P: n/a
Dear Randy Birch,
The changed routine that you have provided has solved my purpose
exactly to the core. I am very much thankful to you for helping me
out. I was trying to crack it for more than a week continuously & you
have made it for me exactly as I wanted. I thank you very much again/

Regards,
Pavan.
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.