C# - Loading a Large JPEG file (> 2Mb) - Memory Issue

I'm writing a Pocket PC application in C#, VS2005. I'm using the .NET Compact Framework 2.0. All I want to do is load a large JPEG file (2-3MB) taken from a digital camera, and display it in a Picture Box.

When I try to do so using the following code, I get HResult -2147024882 --- "OutOfMemoryException"

Bitmap bmp = new Bitmap(
@"\Storage Card\TEMP\2MBJPEG.JPG");

I know that other programs are able to do this, even if it involves making an unmanged call.

I know this can be done because other programs can do it (eg. Resco Photo Viewer). I've also seen examples that use c++ (although I'm not familiar with that language).

How can I load a large bitmap (even if it ends up being scaled down to a thumbnail)




Answer this question

C# - Loading a Large JPEG file (> 2Mb) - Memory Issue

  • Vinces

    Excellent! That worked. Thank you for that Alex.

    Here are the steps that i followed to get it working:

    1. Go to http://www.opennetcf.org/PermaLink.aspx guid=d57ace50-2762-4b19-b07d-39421829d410 and download the Download the SDF 2.0 Beta1 Redistributables. Install.
    2. Create a Smart Device Project in VS.NET 2005 targeting PPC 2003 SE device. I had to right-click my project and choose "Upgrade" to ensure it was targetting the .NET CF v2.0 rather than .NET CF 1.1 SP3.
    3. I added the OpenNETCF.dll and OpenNETCF.Drawing.dll files as references from the default installation directory (C:\Program Files\OpenNETCF\Smart Device Framework 2.0\)
    4. I created a simple app with a picturebox on a form, that loads my image when the form is loaded. Here's the relevant code, some copied from Alex Feinman's web log:


    #region Using directives
    using
    System;
    using
    System.Drawing;
    using
    System.Collections;
    using
    System.Windows.Forms;
    using
    System.Runtime.InteropServices;
    using
    OpenNETCF.Drawing.Imaging;
    using
    System.IO;
    #endregion

    ....

    const string szFileName = @"\Storage Card\TEMP\2MBJPEG.JPG";


    private void Form1_Load(object sender, EventArgs
    e)
    {
    IBitmapImage
    imageBitmap;
    FileStream
    fsImage;
    fsImage =
    new FileStream
    (
    szFileName,
    FileMode
    .Open);
    imageBitmap = CreateThumbnail(
    fsImage,
    new Size
    (100, 100));

    Bitmap bm = ImageUtils
    .IBitmapImageToBitmap(
    imageBitmap);
    pictureBox1.Image = bm;
    }


    static public IBitmapImage CreateThumbnail(Stream stream, Size
    size)
    {
    IBitmapImage
    imageBitmap;
    ImageInfo
    ii;
    IImage
    image;
    ImagingFactory factory = new ImagingFactoryClass
    ();
    factory.CreateImageFromStream(
    new StreamOnFile
    (stream),
    out
    image);
    image.GetImageInfo(
    out
    ii);
    factory.CreateBitmapFromImage(
    image,
    (
    uint
    )size.Width,
    (
    uint
    )size.Height,
    ii.PixelFormat,
    InterpolationHint
    .InterpolationHintDefault,
    out
    imageBitmap);
    return
    imageBitmap;
    }



  • SintratF

  • Dietmar Kupke

    I would suggest posting question about that on the mentioned NG. Or you can debug the code and find our what's going on.



  • noob

    Is anyone else able to get the sample posted (http://www.alexfeinman.com/download.asp doc=LoadBitmap.zip) working with large images

  • CPG

    Thanks for the suggestion to debug the code, but as mentioned in the original question, I'm not familiar with c++, and 2 didn't find any project options in the c++ dll code/project that allowed starting the project as a separate instance of the IDE. i will make a post to the newsgroup, but there must be another way....

  • TonyTheCalypsoKid

    The Imaging libraries in the SDF v2 should be able to do that.

    http://www.opennetcf.org/sdf

    Take a look at Alex's blog for details:

    http://blog.opennetcf.org/afeinman/


  • ceto

    Ilya - Thanks for the link. I downloaded the example application, and gave it a whirl, however it didn't work either. I changed the location of the file to open to be my file, and I ran the application. It successfully popped up a message indicating the dimensions of the image, however the attempt to load the file failed (with no reason given). The return 'data' variable from the C++ DLL function call returned 0 bytes.


  • C# - Loading a Large JPEG file (> 2Mb) - Memory Issue