2

I am developing a simple code to get the image from filedialog and i want it to be displayed in picturebox. but i get error "outofmemory" with few images.

here is my code

Dim srcmap As Bitmap
srcmap = New Bitmap(OpenFileDialog1.FileName)
Dim destbit As New Bitmap(220, 220)
Dim srcRec As New Rectangle(0, 0, srcmap.Width, srcmap.Height)
Dim destRec As New Rectangle(0, 0, 220, 220)
Dim g As Graphics
g = Graphics.FromImage(destbit)
g.DrawImage(srcmap, destRec,srcRec, GraphicsUnit.Pixel)
picturebox.Image = destbit
Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
Rizz
  • 41
  • 2

1 Answers1

1

Mobile devices have limitted resources. Up to Windows CE 5 based OS (latest currently called Windows Embedded Handheld 6.5.3) each process gets only 32MB program memory. This memory is limitted by DLLs loaded by other processes and you may have 24MB or less available for a new process.

Instead of loading whole image data, which may 15MB (5MP image) or more you should load a thumbnail representation of the image data only. It does not make sense to load 15MB image data into a pictore box of for example only 1MB pixel data.

The OpenNetCF framework offers some classes to create thumbnails using streams. Other attempts to load the data and then resize it will fail.

I am sorry, but I only have C# code examples: here is an image helper class http://code.google.com/p/intermeccontrols/source/browse/DPAG7/Hasci.TestApp.And_Controls/IntermecControls/Hasci.TestApp.IntermecCamera3/ImageHelper.cs and here is how I used it to load 5MP images http://code.google.com/p/intermeccontrols/source/browse/DPAG7/Hasci.TestApp.And_Controls/IntermecControls/Hasci.TestApp.IntermecCamera3/IntermecCameraControl3.cs:

    OpenNETCF.Drawing.Imaging.StreamOnFile m_stream;
    Size m_size;
    /// <summary>
    /// this will handle also large bitmaps and show a thumbnailed version on a picturebox
    /// see http://blog.opennetcf.com/ctacke/2010/10/13/LoadingPartsOfLargeImagesInTheCompactFramework.aspx
    /// </summary>
    /// <param name="sFileName">the name of the file to load</param>
    private void showImage(string sFileName)
    {
        var stream = File.Open(sFileName, FileMode.Open);
        m_stream = new StreamOnFile(stream);
        m_size = ImageHelper.GetRawImageSize(m_stream);
        System.Diagnostics.Debug.WriteLine("showImage loading " + sFileName + ", width/height = " + m_size.Width.ToString() + "/"+ m_size.Height.ToString());
        //CameraPreview.Image = ImageHelper.CreateThumbnail(m_stream, CameraPreview.Width, CameraPreview.Height);
        CameraSnapshot.Image = ImageHelper.CreateThumbnail(m_stream, CameraPreview.Width, CameraPreview.Height);
        showSnapshot(true); //show still image
        m_stream.Dispose();
        stream.Close();
    }
josef
  • 5,951
  • 1
  • 13
  • 24