|
-
Nov 2nd, 2003, 12:36 AM
#1
Thread Starter
Fanatic Member
replacing picturebox with device context
Hi, I'd like to be able to get rid of a picturebox I'm using on one of my forms.
I know I'll have to use StretchBlt API to replace the paintpicture vb function in my code but how would I go about creating the device context to replace the picturebox itself and making all this work.
I guess I should ask also... is loading an image to a device context much quicker than loading to a picturebox?
Thanks in advance,
what I have so far
VB Code:
Dim MyPic As StdPicture
Set MyPic = LoadPicture(picturePath)
Call Picture1.PaintPicture(MyPic, 0, 0, Picture1.Width, Picture1.Height)
Set MyPic = Nothing
Image1.Picture = Picture1.Image
Set Picture1.Picture = Nothing
-
Nov 2nd, 2003, 12:51 AM
#2
The picture isn't missing
VB Code:
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC 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 dwRop As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd 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 Sub Command1_Click()
Dim nDC As Long, nBitmap As Long
'create the memory dc
nDC = CreateCompatibleDC(GetDC(0))
nBitmap = CreateCompatibleBitmap(GetDC(0), 100, 100)
'select the bitmap into memory
SelectObject nDC, nBitmap
'do stuff to it
BitBlt nDC, 0, 0, 100, 100, GetDC(0), 0, 0, vbSrcCopy
BitBlt Me.hdc, 0, 0, 100, 100, nDC, 0, 0, vbSrcCopy
'and cleanup
DeleteObject nBitmap
DeleteDC nDC
End Sub
-
Nov 2nd, 2003, 01:33 AM
#3
Thread Starter
Fanatic Member
Thanks BuggyProgrammer,
In the end though I need to load that image to an image box.
Like what I had
Image1.Picture = Picture1.Image
The reason I'm not loading directly to the image box is that in reality I may have as many as 500 image files to load into an array of imagebox (created at run time) and loading directly to them loads the entire image file content regardless if the imagebox is only 1 inch wide x 1 inch high! which is about the size of my imagebox controls.
That is just a plain waste of memory.
What I'm trying to achieve is to resize/scale down each image file prior to loading them onto the imagebox.
Using the picturebox does the trick but I'd sure like to get rid of it if I can.
-
Nov 2nd, 2003, 01:46 AM
#4
Thread Starter
Fanatic Member
Also I forgot to mention that these image files would not only be limited to bitmaps but could be jpg as well.
-
Nov 2nd, 2003, 01:08 PM
#5
The picture isn't missing
hmm.....
you can change a hDC into a picture object:
VB Code:
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC 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 dwRop As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Const RC_PALETTE As Long = &H100
Const SIZEPALETTE As Long = 104
Const RASTERCAPS As Long = 38
Private Type PALETTEENTRY
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End Type
Private Type LOGPALETTE
palVersion As Integer
palNumEntries As Integer
palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
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 GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
Private Sub Command1_Click()
Dim nDC As Long, nBitmap As Long
'create the memory dc
nDC = CreateCompatibleDC(GetDC(0))
nBitmap = CreateCompatibleBitmap(GetDC(0), 100, 100)
'select the bitmap into memory
SelectObject nDC, nBitmap
'do stuff to it
BitBlt nDC, 0, 0, 100, 100, GetDC(0), 0, 0, vbSrcCopy
'set it as a picture
Me.Picture = hDCToPicture(nDC, 0, 0, 100, 100)
'and cleanup
DeleteObject nBitmap
DeleteDC nDC
End Sub
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
'Create a picture object from the screen
Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, HasPaletteScrn As Long
Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE
'Create a compatible device context
hDCMemory = CreateCompatibleDC(hDCSrc)
'Create a compatible bitmap
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
'Select the compatible bitmap into our compatible device context
hBmpPrev = SelectObject(hDCMemory, hBmp)
'Raster capabilities?
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
'Does our picture use a palette?
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
'What's the size of that palette?
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
'Set the palette version
LogPal.palVersion = &H300
'Number of palette entries
LogPal.palNumEntries = 256
'Retrieve the system palette entries
R = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
'Create the palette
hPal = CreatePalette(LogPal)
'Select the palette
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
'Realize the palette
R = RealizePalette(hDCMemory)
End If
'Copy the source image to our compatible device context
R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
'Restore the old bitmap
hBmp = SelectObject(hDCMemory, hBmpPrev)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
'Select the palette
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
'Delete our memory DC
R = DeleteDC(hDCMemory)
Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
End Function
Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
'Fill GUID info
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
'Fill picture info
With Pic
.Size = Len(Pic) ' Length of structure
.Type = vbPicTypeBitmap ' Type of Picture (bitmap)
.hBmp = hBmp ' Handle to bitmap
.hPal = hPal ' Handle to palette (may be null)
End With
'Create the picture
R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
'Return the new picture
Set CreateBitmapPicture = IPic
End Function
most of the code was from the apiguide
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|