June 19, 2019, 01:50:50 pm

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Loading JPEG from string

Started by Peter, March 07, 2010, 11:27:24 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Peter

I was wondering if I somehow can load images such as jpegs etc (not bitmaps) directly from memory for use with RGNFROMBITMAP and SETBUTTONRGN etc?

I would like to customize the look of an application with jpg/png instead of bmp due to size. Someone who can give me some tips?

sapero

Peter, if your question is still active, there is the solution (requires headers from 15 Nov 2010):

Code Select
$include "olectl.inc"
$include "shlwapi.inc"

' line 113: update the path to your image, after "incbin"

declare GetImageDataSize_1(),int
declare asm_imageData_1

IStream stream
pointer imageData = &asm_imageData_1
int     imagesize = GetImageDataSize_1()
UINT    hImage
LARGE_INTEGER g_liZero ' zeroed


' pick step 1 or 2 by defining (or not) USE_SHCREATEMEMSTREAM condition
$define USE_SHCREATEMEMSTREAM
'---------------------------------------------------------------
' 1. using SHCreateMemStream api from shlwapi.dll (ordinal 12) Requires Windows XP.

$ifdef USE_SHCREATEMEMSTREAM

stream = SHCreateMemStream(imageData, imagesize)
if (!stream)
' ERROR: SHCreateMemStream failed
else
stream->_Seek(g_liZero, STREAM_SEEK_SET, NULL)
'
' OK, stream is now initialized, we can pass it to OleLoadPicture or GdipLoadImageFromStream
'
'TODO: call LoadImageFromStream here
if (!LoadImageFromStream(stream, imagesize, hImage))
MESSAGEBOX 0, "LoadImageFromStream failed", "Error"
else
MESSAGEBOX 0, "Image has been loaded", "Error"
DELETEIMAGE hImage, @IMGSCALABLE
endif

stream->Release()
endif


$else

'---------------------------------------------------------------
' 2. using standard stream - CreateStreamOnHGlobal
if (!CreateStreamOnHGlobal(0, TRUE, &stream))
if (stream->_Write(imageData, imagesize, NULL) < 0)
' ERROR
else
stream->_Seek(g_liZero, STREAM_SEEK_SET, NULL)
'
' OK, stream is now initialized, we can pass it to OleLoadPicture or GdipLoadImageFromStream
'
'TODO: call LoadImageFromStream here
if (!LoadImageFromStream(stream, imagesize, hImage))
MESSAGEBOX 0, "LoadImageFromStream failed", "Error"
else
MESSAGEBOX 0, "Image has been loaded", "Error"
DELETEIMAGE hImage, @IMGSCALABLE
endif
endif
stream->Release()
endif

$endif

'---------------------------------------------------------------
' The function to load image from stream, and convert to a scalable image.
'    For JPG/WMF/ICO/GIF/CUR/BMP images we can do it with OLE functions

sub LoadImageFromStream(IStream stream, int imagesize, UINT hImage byref),BOOL

HRESULT hr = OleLoadPicture(stream, imagesize, FALSE, _IID_IPicture, &hImage)
/* if (hr < 0)
ERROR: unable to load image. Bad image format?
else
image loaded/created. Use @IMGSCALABLE in SHOWIMAGE
DELETEIMAGE hImage, @IMGSCALABLE
endif*/
return hr>=0
endsub



' for BitmapToRegion function
sub GetBitmapFromScalableImage(UINT hImage, UINT hBitmap byref),BOOL
IPicture pic = hImage
return (pic and !pic->get_Handle(&hBitmap))
endsub


' optional, not used here
sub GetScalableImageSize(UINT hImage, int width byref, int height byref),BOOL
UINT hBitmap
BITMAP bmp

if (GetBitmapFromScalableImage(hImage, hBitmap))
GetObject(hBitmap, len(bmp), &bmp)
width = bmp.bmWidth
height = bmp.bmHeight
return TRUE
endif
return FALSE
endsub


end
_asm
segment .data

asm_imageData_1:
incbin "D:\Aurora\projects\OCR\images\old\antifakepic18.jpg"

asm_imageDataEnd_1:

segment .text

GetImageDataSize_1:
mov eax,asm_imageDataEnd_1 - asm_imageData_1
ret

_endasm