Here is an include file which has a routine which allows you to load JPeg,GIF and PNG images using GDIplus.
The beauty of this routine is that you don't need any GDI+ API include files to use it. It is a self contained include file of its own.
The routine can load images for either DDT or SDK coding.
I finally got it working for DDT, but there is still one small problem with it. There is a white line at the top of the image and I have no idea why.
I don't know what DDT does with its own Bitmaps, but I had to go through loops to convert the image to a DDT Bitmap. I had to load the image as a DIB and using pointers run the image through a conversion routine before BitBlt'ing it to a DDt Bitmap.
If anyone can figure out what the problem is (the white line) please email me or post the solution so I can fix the problem.
Here are the routines in the include file
GDIP_GetImageSize F$, W&, H&Reads an image file and returns the size (width and height) in pixels, without loading.
F$ is the filename (full path) of any Image file windows supports (ie. JPeg, PNG, GIF)
W& is a long variable to return the width in pixels
H& is a long variable to return the height in pixels
hBmp& = GDIP_LoadImage(DMode&, F$, W&, H&, QFlag&)Loads an image file (JPeg,GIF or PNG)
DMode& if non-zero (1) makes function return a DDT Bitmap, rather than an API Bitmap
F$ is the filename (full path) of any Image file windows supports (ie. JPeg, PNG, GIF)
W& is the requested width of the image (in pixels)
H& is the requested height of the image (in pixels)
QFlag& if set to non-zero (1) requests best quality possible image
If W& and H& are set to -1 (or even zero) then the routine will use the actual size of the image on file.
The return value is a Bitmap handle.
If DMode& is non-zero (1) then the handle is a DDT Bitmap handle which can only be used with DDT Graphic commands.
If DMode& is zero, then the handle is an API Bitmap handle, which can only be used with the API and not DDT Graphic commands.
The code is based on code written by Dan Ginzel.
His code can be found here:
http://chrisboss.hypermart.net/ubb/Forum36/HTML/000002.htmlHere is the actual code in the EZ include file:
' Public domain
' conversion of code originally by Dan Ginzel
' see: http://chrisboss.hypermart.net/ubb/Forum36/HTML/000002.html
' requires no include files for GDIplus
DECLARE FUNCTION GdiplusStartupX LIB "GDIPLUS.DLL" ALIAS "GdiplusStartup" (BYREF token AS DWORD, BYVAL lpGDI_I AS DWORD, BYVAL lpGDI_O AS DWORD) AS LONG
DECLARE FUNCTION GdipLoadImageFromFileX LIB "GDIPLUS.DLL" ALIAS "GdipLoadImageFromFile" (BYVAL pFilename AS STRING, BYREF pImage AS DWORD) AS LONG
DECLARE FUNCTION GdipGetImageDimensionX LIB "GDIPLUS.DLL" ALIAS "GdipGetImageDimension" (BYVAL pImage AS DWORD, BYREF nWidth AS SINGLE, BYREF nHeight AS SINGLE) AS LONG
DECLARE FUNCTION GdipCreateFromHDCX LIB "GDIPLUS.DLL" ALIAS "GdipCreateFromHDC" ( BYVAL hDC AS LONG, BYREF graphics AS DWORD) AS LONG
DECLARE FUNCTION GdipSetInterpolationModeX LIB "GDIPLUS.DLL" ALIAS "GdipSetInterpolationMode" (BYVAL graphics AS DWORD, BYVAL interpolationMode AS LONG) AS LONG
DECLARE FUNCTION GdipDrawImageRectIX LIB "GDIPLUS.DLL" ALIAS "GdipDrawImageRectI" (BYVAL graphics AS DWORD, BYVAL pImage AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, BYVAL nWidth AS LONG, BYVAL nHeight AS LONG) AS LONG
DECLARE FUNCTION GdipDisposeImageX LIB "GDIPLUS.DLL" ALIAS "GdipDisposeImage" ( BYVAL pImage AS DWORD) AS LONG
DECLARE SUB GdiplusShutdownX LIB "GDIPLUS.DLL" ALIAS "GdiplusShutdown" ( BYVAL token AS DWORD)
SUB GDIP_GetImageSize(BYVAL F$, W&,H&)
LOCAL GDIToken AS LONG, Picture AS STRING, Result AS LONG, pImage AS DWORD
LOCAL hDC AS LONG, pGraphics AS LONG, QMode AS LONG, RV$, PWidth AS SINGLE, PHeight AS SINGLE
LOCAL GDI_S() AS DWORD
RV$=""
DIM GDI_S(1 TO 4) ' use instead of GdiplusStartupInput structure
GDI_S(1)=1 ' version
F$=UCODE$(TRIM$(F$))
W&=0
H&=0
IF GDIplusStartupX(GDItoken, BYVAL VARPTR(GDI_S(1)), BYVAL 0)=0 THEN
Result = GDIPLoadImageFromFileX(F$, pImage)
GDIPGetImageDimensionX pImage, PWidth, PHeight
W&=PWidth
H&=PHeight
IF pImage<>0 THEN GDIPDisposeImageX(pImage)
GDIplusShutdownX GDItoken
END IF
END SUB
FUNCTION GDIP_LoadImage(BYVAL DMode&, BYVAL F$, BYVAL W&, BYVAL H&, BYVAL QFlag&) AS DWORD
LOCAL GDIToken AS LONG, Picture AS DWORD, Result AS LONG, pImage AS DWORD
LOCAL hDC AS DWORD, pGraphics AS LONG, QMode AS LONG, RV AS DWORD, PWidth AS SINGLE, PHeight AS SINGLE, hOldBmp AS DWORD
LOCAL GDI_S() AS DWORD, hDCD AS DWORD
LOCAL BM AS BITMAPINFO, PA AS DWORD, BP AS BYTE PTR, LX&,LY&, Pad&, BRow&
REGISTER BX&, BY&
RV=0
DIM GDI_S(1 TO 4) ' use instead of GdiplusStartupInput structure
GDI_S(1)=1 ' version
F$=UCODE$(TRIM$(F$))
Picture=0
IF GDIplusStartupX(GDItoken, BYVAL VARPTR(GDI_S(1)), BYVAL 0)=0 THEN
Result = GDIPLoadImageFromFileX(F$, pImage)
GDIPGetImageDimensionX pImage, PWidth, PHeight
IF W&>0 THEN PWidth=W&
IF H&>0 THEN PHeight=H&
hDCD = GetDC(%HWND_DESKTOP)
hDC=CreateCompatibleDC(hDCD)
GOSUB MakeDIB
ReleaseDC %HWND_DESKTOP, hDCD
IF hDC<>0 THEN
hOldBmp=SelectObject(hDC,Picture)
IF QFlag& THEN
QMode=2
ELSE
QMode=1
END IF
Result = GDIPCreateFromHDCX(hDC, pGraphics)
Result = GDIPSetInterpolationModeX(pGraphics, QMode)
Result = GDIPDrawImageRectIX(pGraphics, pImage, 0, 0, PWidth, PHeight)
END IF
IF pImage<>0 THEN GDIPDisposeImageX(pImage)
GDIplusShutdownX GDItoken
IF hDC<>0 THEN
IF DMode&<>0 THEN
GOSUB SwapRedBlue
GOSUB ConvertToDDTGraphic
DeleteDC hDC
ELSE
SelectObject hDC, hOldBmp
DeleteDC hDC
END IF
END IF
RV=Picture
END IF
FUNCTION=RV
EXIT FUNCTION
MakeDDB:
Picture=CreateCompatibleBitmap(hDCD,INT(PWidth),INT(PHeight))
RETURN
MakeDIB:
BM.bmiHeader.biSize=SIZEOF(BM.bmiHeader)
BM.bmiHeader.biWidth=INT(PWidth)
BM.bmiHeader.biHeight=-INT(PHeight) ' pass a negative value for a topdown Bitmap
BM.bmiHeader.biPlanes=1
BM.bmiHeader.biBitCount=24
BM.bmiHeader.biCompression=%BI_RGB
Picture=CreateDIBSection (hDCD, BM, %DIB_RGB_COLORS, VARPTR(PA), %NULL, %NULL)
RETURN
SwapRedBlue:
GDIFlush
LX&=INT(PWidth)
BRow&=LX&*3
Pad&=BRow& MOD 4
IF Pad&<>0 THEN Pad&=4-Pad&
LX&=LX&-1
LY&=INT(PHeight)-1
BP=PA
FOR BY&=0 TO LY&
FOR BX&=0 TO LX&
@BP[0]=255-@BP[0]
@BP[1]=255-@BP[1]
@BP[2]=255-@BP[2]
SWAP @BP[0],@BP[2]
BP=BP+3
NEXT BX&
BP=BP+Pad&
NEXT BY&
RETURN
ConvertToDDTGraphic:
LOCAL hBmp AS DWORD, hGDC AS DWORD, MapMode&
GRAPHIC BITMAP NEW INT(PWidth),INT(PHeight) TO hBmp
GRAPHIC ATTACH hBmp,0
GRAPHIC SET OVERLAP 1
GRAPHIC SCALE PIXELS
GRAPHIC GET DC TO hGDC
bitBlt hGDC,0,0,INT(PWidth),INT(PHeight), hDC, 0,0,%SRCCOPY
GRAPHIC DETACH
SelectObject hDC, hOldBmp
DeleteObject Picture
Picture=hBmp
RETURN
END FUNCTION