views:

584

answers:

3

I am trying to load high resolution image(like 3264x2448). For that I am using IImageFactory class in C#. IImageFactory is downloaded from the Microsoft site. If I try to load the high resolution image I am getting “out of memory” exception. Have a look at the below sample:

IImage GetIImage(string fileName)
{

Bitmap bitmap = null;

Graphics graphics = null;
IntPtr hdcDestination = IntPtr.Zero;   
try    {
    IImage image = null;
    IImagingFactory imagingFactory = ImagingFactory.GetImaging();
    imagingFactory.CreateImageFromFile(fileName, out image);
    bitmap = new Bitmap(width, height);
    graphics = Graphics.FromImage(bitmap);
    hdcDestination = graphics.GetHdc();
           Rectangle dstRect = new Rectangle(0, 0, width, height);
   image.Draw(_graphicsHDC, ref dstRect, IntPtr.Zero);
}
catch{}
}

Using the above source lines I was able to load the images but my effort to draw image fails, throwing an OutOfMemory Excep :( Further, I tried the same by creating a Win32 mobile App, in which I could bot Load and Draw the desired images with the following lines.

void DrawImage(HDC *hdc, char *FileName, int width, int height)
{
IImagingFactory *pImgFactory = NULL;
IImage *pImage = NULL;
RECT rc = { 0, 0, width, height};

WCHAR Name[MAX_PATH] ={0}; 
mbstowcs (Name, FileName, strlen (FileName));
CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory,
                                NULL,
                                CLSCTX_INPROC_SERVER,
                                IID_IImagingFactory,
                                (void **)&pImgFactory)))
{
    // Load the image from the JPG file.
    if (SUCCEEDED(pImgFactory->CreateImageFromFile(Name, &pImage)))
    {
        pImage->Draw(*hdc, &rc, NULL);
        pImage->Release();
    }

    pImgFactory->Release();
}
CoUninitialize();}

I even tried creating a win32 dll and invoke it from C# app but I couldn't. Can anyone help me out to resolve this issue?

A: 

you need to increase a memory limit for it ... Got a similar problem with different language (PHP), once increased a memory limit to 25mb. it solved a problem.

David King
And how does he do that?
Brian
A: 

Though you don't want to use OpenNETCF, I will offer it anyway: This seems to be exactly what you are experiencing.
Have to admit I didn't try this myself - just googled a bit.

Shaihi
I read the post I gave a link to more thoroughly. There seems to be a link to a solution with source code in native code and managed code that calls it. If you try to access Alex Feinman's blog, change the .org in the url to .com
Shaihi
Thank Shaihi ill try..
Shadow
+2  A: 

Of course it does. You loaded an image and then are trying to display the whole thing. To do so, the CF has to turn it into a Bitmap for the display driver. 3264x2448x16bpp == nearly 16MB. What you should be doing is generating a thumbnail and drawing that to the rectangle.

And, as usual, the Smart Device Framework already has this done for you.

ctacke
Thanks i will look into this
Shadow