Thursday, 14 June 2007

Using Image Factory under CE/Windows Mobile

Having trouble getting Windows CE/Windows Mobile factory working to load pictures?

The code below will load a JPG/GIF/PNG (or whatever decoders are installed on your device) etc from a file and paint it to the screen:

CoInitializeEx(NULL, 0);
IImagingFactory* pImageFactory;
IImage *pImage = NULL;

HDC dc = GetDC(0); // insert your window handle here, else this is the desktop

HRESULT hr = CoCreateInstance(CLSID_ImagingFactory, NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IImagingFactory),
(LPVOID *)&pImageFactory);

// Load image from file
pImageFactory->CreateImageFromFile(bitmapName, &pImage);

// Get image information
ImageInfo info;
pImage->GetImageInfo(&info);

RECT rc={0,0,info.Width, info.Height};

// draw jpg/gif etc onto temp dc
pImage->Draw(dc, &rc, NULL);

// Clean up
pImage->Release();
pImageFactory->Release();

CoUninitialize();



9 comments:

Anonymous said...

Hi,

I tried including this in Pocket PC2003 but get the following error.

: fatal error C1083: Cannot open include file: 'GdiplusPixelFormats.h': No such file or directory

Unknown said...

Can you tell me where should I place the file "bitmapName" (CreateImageFromFile). In which folder. I am getting error file not found. I have tried placing it in the folder where I my worksapce is but nothing seems t work.

Anonymous said...

Hi,

I tried including this in Pocket PC2003 but get the same error as above.

: fatal error C1083: Cannot open include file: 'GdiplusPixelFormats.h': No such file or directory

GraemeW said...

Windows CE / Pocket PC has no concept of current working directories, so you must specify the whole path, i.e.

From an external disk like

\sd card\mybitmap.bmp

or from the root mounted file system (this maybe ram or flash depending on the OS setup)

\mybitmap.bmp

GraemeW said...

For the includes you should be using (in addition to your afx.h / windows.h and others):


#include "INITGUID.h"
#include "imaging.h"

Anonymous said...

Could you give me a hand on how this could be done in VB.NET or at least the libraries and/or DLLs I should check out. I would really appreaciate it!

Cheers,
Mike

GraemeW said...

Sorry not really sure in VB, isn't there built in handling in VB to support loading and saving of png/jpg etc files using the

System.Drawing.Imaging
or
Dim g As Graphics = Graphics.FromImage(bmp)

Type commands (just from a google), these will invariably be just calling the imaging factory underneath anyhow, or do you want lower access than VB allows?

Cheers
Graeme

Unknown said...

Hi I need a method to acces the image pixel (i,j) with imaginfactory before draw it in the hdc to transfer the pixel to an array.

Actually i drawn it and acces the pixel in the hdc with GetPixel but this method is very slow.

GraemeW said...

You should use CreateDIBSection to create a device independent bitmap, this returns a pointer to the memory buffer that makes up the bitmap, draw to this DC then access the array of pixels directly, much much quicker than get pixel but you will have to work out the bit ordering for the RGB pixels.