Date: 02-16-2022
Return to Index
created by gbSnippets
#Compile Exe
%Unicode = 1
#Include "Win32API.inc"
#Include "gdiplus.inc"
$MyImage = "floor.png"
#Resource RcData, myimage, $MyImage
Function PBMain() As Long
LoadMyResource "myimage"
End Function
Sub LoadMyResource(imgName$)
Local wImg, hImg As Long
Local pBitmap,pGraphics,token, hBMP, hDC As Dword
Local hInst, hRes, x, pres,hglobal, pbuffer As Dword
Local StartupInput As GdiplusStartupInput, pStream As IStream
'initialize
StartupInput.GdiplusVersion = 1
GdiplusStartup(token, StartupInput, ByVal %NULL)
'JK ------------------ access JPG/GIF/PNG resource as stream, then create GDI+ bitmap
hInst = GetModuleHandle(ByVal %NULL)
hRes = FindResource(hinst, (imgName$), ByVal %RT_RCDATA) 'find resource
x = SizeofResource(hInst, hRes)
pres = LockResource(LoadResource(hInst, hRes)) 'lock it
hGlobal = GlobalAlloc(%GMEM_MOVEABLE, x) 'allocate memory to hold the image
pBuffer = GlobalLock(hGlobal) 'lock memory
CopyMemory pBuffer, pRes, x 'copy the image from resource to global memory
CreateStreamOnHGlobal(hGlobal, %FALSE, pStream) 'Create a stream in global memory
GdipLoadImageFromStream pStream, pBitmap 'pBitmap is GDI+ handle to the image
pStream = Nothing
'-------------------
GdipGetImageWidth(pBitmap,wImg) 'get width
GdipGetImageHeight(pBitmap,hImg) 'get height
'create memory bitmap for holding the image
Graphic Bitmap New wImg,hImg To hBMP
Graphic Attach hBMP,0
Graphic Get DC To hDC 'hDC is for memory bitmap
'use the hDC
GdipCreateFromHDC(hDC, pGraphics) 'create graphic object
GdipDrawImageRect(pGraphics, pBitmap, 0,0,wImg,hImg) 'draw image at 0,0
'shutdown GDI
If pBitmap Then GdipDisposeImage(pBitmap) 'cleanup
If pGraphics Then GdipDeleteGraphics(pGraphics) 'cleanup
GDIPlusShutDown token
GlobalUnLock pBuffer
GlobalFree hGlobal
'save the image
Graphic Save PathName$(Name,$MyImage) + ".bmp"
End Sub
http://www.garybeene.com/sw/gbsnippets.htm