Hello,
Here is a function to save a bitmap identified with a HBITMAP handle to a file. The main application loads a bitmap named Source.bmp and saves it as Destination.bmp
There are some GOTO statements in the function. I know that this is not encouraged in procedural programming but the code is not complicated and no spaghetti here. Under the hood, the assembly code emitted by a compiler contains a lot of jmp instructions equivalent to GOTO.
Here is a function to save a bitmap identified with a HBITMAP handle to a file. The main application loads a bitmap named Source.bmp and saves it as Destination.bmp
There are some GOTO statements in the function. I know that this is not encouraged in procedural programming but the code is not complicated and no spaghetti here. Under the hood, the assembly code emitted by a compiler contains a lot of jmp instructions equivalent to GOTO.
Code Select
FUNCTION SaveBmpFromHandle(hBitmap AS HBITMAP, FileName AS STRING) AS INTEGER
LOCAL BmpSize AS INTEGER
LOCAL pd AS PICTDESC
LOCAL pPicture AS LPPICTURE
LOCAL pStream AS LPSTREAM
LOCAL hGlobal AS HGLOBAL
LOCAL hr AS HRESULT
LOCAL pMem AS void PTR
pd.cbSizeofstruct = SIZEOF(PICTDESC)
pd.picType = PICTYPE_BITMAP
pd.bmp.hbitmap = hBitmap
pd.bmp.hpal = 0
hr = OleCreatePictureIndirect(&pd, &IID_IPicture, FALSE, (void**)(&pPicture))
IF hr<>0 THEN EXIT FUNCTION
CreateStreamOnHGlobal(0, TRUE, &pStream)
IF hr<>0 THEN GOTO relPict
hr = pPicture->lpVtbl->SaveAsFile(pPicture, pStream, TRUE, &BmpSize);
IF hr<>0 THEN GOTO relStream
hr = GetHGlobalFromStream(pStream, &hGlobal)
IF hr<>0 THEN GOTO relStream
pMem = GlobalLock(hGlobal)
IF pMem= 0 THEN GOTO relStream
OPEN FileName FOR BINARY NEW OUTPUT AS hFile
PUT$ hFile, pMem, BmpSize
CLOSE hFile
relStream:
hr = pStream->lpVtbl->Release(pStream)
relPict:
hr = pPicture->lpVtbl->Release(pPicture)
FUNCTION = BmpSize
END FUNCTION
' Main application
DIM AS HBITMAP hBmp
DIM AS INTEGER ImageSize
hBmp = BCX_LOADBMP("Source.bmp", 0)
ImageSize = SaveBmpFromHandle(hBmp, "Destination.bmp")
PRINT "Destination bitmap size = ", ImageSize