Re: Load and Display a JPG Image
- Posted by ssallen Mar 03, 2013
- 1870 views
I have used CXImage in the past and it makes it pretty easy if all you want to do is draw a jpg/gif/png/etc in a window:
global function getImageType(sequence xbytes) -- xbytes is first 5 bytes of file integer imgtype imgtype = 0 if xbytes[1] = 'ÿ' then if xbytes[2] = 'Ø' and xbytes[3] = 'ÿ' then imgtype = CXI_FORMAT_JPG end if elsif xbytes[2] = 'P' then if xbytes[3] = 'N' and xbytes[4] = 'G' then imgtype = CXI_FORMAT_PNG end if elsif xbytes[1] = 66 then if xbytes[2] = 77 then imgtype = CXI_FORMAT_BMP end if elsif xbytes[1] = 'G' then if xbytes[2] = 'I' and xbytes[3] = 'F' then imgtype = CXI_FORMAT_GIF end if else imgtype = CXI_FORMAT_UNKNOWN end if return imgtype end function
atom img, fimg, imgtyp sequence abytes fnumb = open("myimg.jpg", "rb") abytes = get_bytes(fnumb, 5) close(fnumb) imgtyp = getImageType(abytes) img = CXI_LoadImage("myimg.jpg", imgtyp, 0) if img < 1 then ret = message_box("Image load error!", "File is busted!", MB_OK) return -1 end if fimg = CXI_MakeBitmap(img, NULL) -- draw the image using whatever api you are using drawBitmap(Window1, fimg, 428, 308) -- win32lib CXI_FreeImage(fimg)
cxImage uses its own internal image format so make sure you use CXI_MakeBitmap - not using it will cause machine level exceptions.
Steve A.