Author Topic: Loading JPEG from string  (Read 2180 times)

0 Members and 1 Guest are viewing this topic.

Offline Peter

  • Paid Subscriber
  • Jr. Member
  • *****
  • Posts: 61
Loading JPEG from string
« on: March 07, 2010, 11:27:24 AM »
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?

Offline sapero

  • Senior Developer
  • Hero Member
  • *****
  • Posts: 1291
Re: Loading JPEG from string
« Reply #1 on: November 15, 2010, 07:16:16 AM »
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