Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > saving picturebox bitmap


Reply
 
Thread Tools Display Modes
  #1  
Old 09-03-2000, 09:41 PM
rodf
Guest
 
Posts: n/a
Default saving picturebox bitmap


How can i save the bitmaps created in picturebox?

ripley
Reply With Quote
  #2  
Old 09-04-2000, 05:02 AM
Phil
Guest
 
Posts: n/a
Default Re: saving picturebox bitmap

Hi,
To save the picture property of a picturebox you can simply use the SavePicture statement. To save an image created in a picturebox is more complex, I managed to addapt some code found in the MSDN article Q161299 to do the job. You can find this at:

http://support.microsoft.com/support...S&SD=msdn&FR=0

There may be an easier way to do this but I couldn't find it so for what it's worth here is the code I use (without any error handling,so you may wish to look at that).

To test this create a project with a default form containing a picturebox Picture1 with SCALEMODE set to PIXELS and a command button command1. Paste this code, create some graphics at design time and hit the command1 hopefully a bitmap "test.bmp" will be created in the "C:Temp" directory.


Option Explicit

Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source

Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type

Private Declare Function CreateCompatibleDC Lib "gdi32" ( _
ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" ( _
ByVal hdc As Long, ByVal nWidth As Long, _
ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" ( _
ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32" ( _
ByVal hDCDest As Long, ByVal XDest As Long, _
ByVal YDest As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal hDCSrc As Long, _
ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) _
As Long
Private Declare Function DeleteDC Lib "gdi32" ( _
ByVal hdc As Long) As Long
Private Declare Function ReleaseDC Lib "user32" ( _
ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetWindowDC Lib "user32" ( _
ByVal hwnd As Long) As Long

Private Type picBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type

Private Declare Function OleCreatePictureIndirect _
Lib "olepro32.dll" (PicDesc As picBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

Private Declare Function StretchBlt Lib "gdi32" _
(ByVal hdc As Long, ByVal x As Long, ByVal y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, _
ByVal ySrc As Long, ByVal nSrcWidth As Long, _
ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Public Function CreateBitmapPicture(ByVal hBmp As Long) As Picture

Dim R As Long
Dim Pic As picBmp
' IPicture requires a reference to "Standard OLE Types."
Dim IPic As IPicture
Dim IID_IDispatch As GUID

' Fill in with 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.
End With

' Create Picture object.
R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

' Return the new Picture object.
Set CreateBitmapPicture = IPic

End Function

Public Function exportBmp(PBox As PictureBox) As Picture
'
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim R As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim WidthPix As Single
Dim HeightPix As Single

WidthPix = PBox.ScaleWidth
HeightPix = PBox.ScaleHeight

hDCSrc = GetWindowDC(PBox.hwnd) ' Get device context for pbox.

' 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, WidthPix, HeightPix)
hBmpPrev = SelectObject(hDCMemory, hBmp)

' Copy the on-screen image into the memory DC.
R = BitBlt(hDCMemory, 0, 0, WidthPix, HeightPix, hDCSrc, _
0, 0, vbSrcCopy)

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

' Release the device context resources back to the system.
R = DeleteDC(hDCMemory)
R = ReleaseDC(PBox.hwnd, hDCSrc)

' Call CreateBitmapPicture to create a picture object from the
' bitmap. Then return the resulting picture object.

Set exportBmp = CreateBitmapPicture(hBmp)

End Function
Private Sub Command1_Click()
'
'save as bitmap option prompts for file name

Dim FileSpec As String

FileSpec = "c: empTest.bmp"
SavePicture exportBmp(Picture1), FileSpec

End Sub

Good Luck Phil

Reply With Quote
  #3  
Old 09-04-2000, 12:25 PM
amram71
Guest
 
Posts: n/a
Default Re: saving picturebox bitmap

Sorry Phil, But i'm pretty positive you can save images Created in a PicBox using absoluely no API, but, like u mentioned, the SavePicture statement.
There are 2 properties of a PBOx that you can use.
Picture Property, to get the Picture in the Box.
Image Property, to get any thing in the box.
So yo ucould do SavePicture Picture1.Image

Correct me if I'm Wrong

Reply With Quote
  #4  
Old 09-05-2000, 04:15 AM
Phil
Guest
 
Posts: n/a
Default Re: saving picturebox bitmap

Hi,

Thanks, you live and learn!
The mistake comes from the fact that in my original code I had to save graphic "controls" such as line and shape and other pictureboxes within the container picturebox, created at design time. The only way I could find to do this was with the above.
We sometimes spend so long looking for complex solutions we overlook the straight forward, thanks for pointing it out.

Phil

Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->