开发者

Getting out of memory exception when loading high resolution image with IImagingFactory

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(fil开发者_开发问答eName, 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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜