OutOfMemory exception

B

Bruce

I am making a simple application that displays a single bitmap image in
a picturebox. I am using the Windows Mobile SDK and my application
works fine on the emulator, but on the phone I am getting an OutOfMemory
Exception whenever I call new Bitmap(filepath). This happens the first time
an image is loaded and I have tried forcing Garbage Collection before I load
it. I am using a Treo 700w Windows Mobile Smartphone. Below is the code I
am trying to run.

There is no other application running at the same time, so it is amazing
that I'm getting an OutOfMemory exception. Am I doing something wrong, or
is there any way to prevent this exception?

-- Bruce
private void menuItem3_Click(object sender, EventArgs e)

{

bool loadImage = false;

using (SelectPictureDialog picturePicker = new SelectPictureDialog())

{

picturePicker.Owner = this;

picturePicker.InitialDirectory = @"\Images";

picturePicker.CameraAccess = false;

picturePicker.ShowDrmContent = true;

picturePicker.ShowForwardLockedContent = false;

picturePicker.Filter = "All files|*.*";

DialogResult result = picturePicker.ShowDialog();

if (result == DialogResult.OK)

{

string fileExtension = Path.GetExtension(picturePicker.FileName);

assetName = picturePicker.FileName;

if (fileExtension.ToLower() == ".jpg")

loadImage = true;

}

}

this.Refresh();

if (loadImage)

{

try

{

pictureBox1.Image = new Bitmap(assetName);

}

catch

{

GC.Collect();

pictureBox1.Image = new Bitmap(assetName);

}

}

}
 
B

Bruce

Chris,

The JPEG image is 1280x1024, full color (I'm guessing thes are 24-bit
color), and the resulting file on the "disk" is about 80K. Not very big, at
least when compressed!?

Any recommendations?

Thanks, Bruce
 
G

Guest

Yes, but it becomes an uncompressed Bitmap when you load it. 1280x1024 is
1.3M pixels and at 24bpp that's around 4MB (if the math in my head is
correct).

-Chris
 
B

Bruce

I agree with your math. And meanwhile, the phone reports that it has only
about 6-7 MB of free memory typically. So 4MB is a large portion of that
memory.



However, by comparison, the phone has a couple built-in apps that take
photos (at this 1.3 Mpixel resolution using the integrated camera) as well
as navigate and view the images stored in the file system. So, somehow
these apps are able to load images of the same size/resolution as I am
trying to load with my app.



Any other guidance about how to avoid this OutOfMemory exception in the
context of my application?



Thanks, Bruce
 
G

Guest

And that app is not using a managed Bitmap class, they're likely using the
Imaging APIs to scale the photo for view. Obviously your phone doesn't have
a 1280x1024 screen, so a large number of the pixels in the bitmap cannot be
rendered on your screen and therefore don't need to be loaded. You
essentially need a thumbnail containing only a subset of the bits in the
image for display. SDF 2.0 Beta [1] has managed wrappers for the imaging
APIs and Alex Feinman[2] has a usage sample in January and February of his
blog.

[1] www.openetcf.org/sdf2
[2] http://blog.opennetcf.org/afeinman/

-Chris
 
G

Guest

I am also having this same problem, have you found a solution? Does CF2.0
SP1 fix the problem?

What is the recommended way of loading a JPG file from disk into memory as
Bitmap?

We have a WM5 application which allows the user to add up to 24 photos from
digital camera sd card or embedded device camera, so any info would be
greatly appreciated...

And that app is not using a managed Bitmap class, they're likely using the
Imaging APIs to scale the photo for view. Obviously your phone doesn't have
a 1280x1024 screen, so a large number of the pixels in the bitmap cannot be
rendered on your screen and therefore don't need to be loaded. You
essentially need a thumbnail containing only a subset of the bits in the
image for display. SDF 2.0 Beta [1] has managed wrappers for the imaging
APIs and Alex Feinman[2] has a usage sample in January and February of his
blog.

[1] www.openetcf.org/sdf2
[2] http://blog.opennetcf.org/afeinman/

-Chris


Bruce said:
I agree with your math. And meanwhile, the phone reports that it has only
about 6-7 MB of free memory typically. So 4MB is a large portion of that
memory.



However, by comparison, the phone has a couple built-in apps that take
photos (at this 1.3 Mpixel resolution using the integrated camera) as well
as navigate and view the images stored in the file system. So, somehow
these apps are able to load images of the same size/resolution as I am
trying to load with my app.



Any other guidance about how to avoid this OutOfMemory exception in the
context of my application?



Thanks, Bruce
 
G

Guest

The solution hasn't changed from what I posted before. The SDF Imaging
stuff will handle this. It's not a CF bug, there just are no classes in the
CF that do this.

-Chris


Dale said:
I am also having this same problem, have you found a solution? Does CF2.0
SP1 fix the problem?

What is the recommended way of loading a JPG file from disk into memory as
Bitmap?

We have a WM5 application which allows the user to add up to 24 photos
from
digital camera sd card or embedded device camera, so any info would be
greatly appreciated...

And that app is not using a managed Bitmap class, they're likely using
the
Imaging APIs to scale the photo for view. Obviously your phone doesn't
have
a 1280x1024 screen, so a large number of the pixels in the bitmap cannot
be
rendered on your screen and therefore don't need to be loaded. You
essentially need a thumbnail containing only a subset of the bits in the
image for display. SDF 2.0 Beta [1] has managed wrappers for the imaging
APIs and Alex Feinman[2] has a usage sample in January and February of
his
blog.

[1] www.openetcf.org/sdf2
[2] http://blog.opennetcf.org/afeinman/

-Chris


Bruce said:
I agree with your math. And meanwhile, the phone reports that it has
only
about 6-7 MB of free memory typically. So 4MB is a large portion of
that
memory.



However, by comparison, the phone has a couple built-in apps that take
photos (at this 1.3 Mpixel resolution using the integrated camera) as
well
as navigate and view the images stored in the file system. So, somehow
these apps are able to load images of the same size/resolution as I am
trying to load with my app.



Any other guidance about how to avoid this OutOfMemory exception in the
context of my application?



Thanks, Bruce



"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Yes, but it becomes an uncompressed Bitmap when you load it. 1280x1024
is
1.3M pixels and at 24bpp that's around 4MB (if the math in my head is
correct).

-Chris


Chris,

The JPEG image is 1280x1024, full color (I'm guessing thes are 24-bit
color), and the resulting file on the "disk" is about 80K. Not very
big, at least when compressed!?

Any recommendations?

Thanks, Bruce

How big is the picture (resolution, color depth, etc.)?

-Chris



I am making a simple application that displays a single bitmap
image
in
a picturebox. I am using the Windows Mobile SDK and my application
works fine on the emulator, but on the phone I am getting an
OutOfMemory
Exception whenever I call new Bitmap(filepath). This happens the
first time
an image is loaded and I have tried forcing Garbage Collection
before
I load
it. I am using a Treo 700w Windows Mobile Smartphone. Below is
the
code I
am trying to run.

There is no other application running at the same time, so it is
amazing that I'm getting an OutOfMemory exception. Am I doing
something wrong, or is there any way to prevent this exception?

-- Bruce
private void menuItem3_Click(object sender, EventArgs e)

{

bool loadImage = false;

using (SelectPictureDialog picturePicker = new
SelectPictureDialog())

{

picturePicker.Owner = this;

picturePicker.InitialDirectory = @"\Images";

picturePicker.CameraAccess = false;

picturePicker.ShowDrmContent = true;

picturePicker.ShowForwardLockedContent = false;

picturePicker.Filter = "All files|*.*";

DialogResult result = picturePicker.ShowDialog();

if (result == DialogResult.OK)

{

string fileExtension = Path.GetExtension(picturePicker.FileName);

assetName = picturePicker.FileName;

if (fileExtension.ToLower() == ".jpg")

loadImage = true;

}

}

this.Refresh();

if (loadImage)

{

try

{

pictureBox1.Image = new Bitmap(assetName);

}

catch

{

GC.Collect();

pictureBox1.Image = new Bitmap(assetName);

}

}

}
 
G

Guest

Ok, looks like OpenNETCF.Drawing.Imaging should solve my OutOfMemory problem,
however, where do I get the source code for this library?

The SourceGear Vault does not contain this namespace
(http://vault.netcf.tv/VaultService/...spx?repid=2&path=$/SDF/v1.4/OpenNETCF.Drawing),
and I would rather include the actual source code in our application rather
then deploying an additional CAB to clients device.

Can you point me to the location where the csharp source code is available
for download?

The solution hasn't changed from what I posted before. The SDF Imaging
stuff will handle this. It's not a CF bug, there just are no classes in the
CF that do this.

-Chris


Dale said:
I am also having this same problem, have you found a solution? Does CF2.0
SP1 fix the problem?

What is the recommended way of loading a JPG file from disk into memory as
Bitmap?

We have a WM5 application which allows the user to add up to 24 photos
from
digital camera sd card or embedded device camera, so any info would be
greatly appreciated...

And that app is not using a managed Bitmap class, they're likely using
the
Imaging APIs to scale the photo for view. Obviously your phone doesn't
have
a 1280x1024 screen, so a large number of the pixels in the bitmap cannot
be
rendered on your screen and therefore don't need to be loaded. You
essentially need a thumbnail containing only a subset of the bits in the
image for display. SDF 2.0 Beta [1] has managed wrappers for the imaging
APIs and Alex Feinman[2] has a usage sample in January and February of
his
blog.

[1] www.openetcf.org/sdf2
[2] http://blog.opennetcf.org/afeinman/

-Chris


I agree with your math. And meanwhile, the phone reports that it has
only
about 6-7 MB of free memory typically. So 4MB is a large portion of
that
memory.



However, by comparison, the phone has a couple built-in apps that take
photos (at this 1.3 Mpixel resolution using the integrated camera) as
well
as navigate and view the images stored in the file system. So, somehow
these apps are able to load images of the same size/resolution as I am
trying to load with my app.



Any other guidance about how to avoid this OutOfMemory exception in the
context of my application?



Thanks, Bruce



"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Yes, but it becomes an uncompressed Bitmap when you load it. 1280x1024
is
1.3M pixels and at 24bpp that's around 4MB (if the math in my head is
correct).

-Chris


Chris,

The JPEG image is 1280x1024, full color (I'm guessing thes are 24-bit
color), and the resulting file on the "disk" is about 80K. Not very
big, at least when compressed!?

Any recommendations?

Thanks, Bruce

How big is the picture (resolution, color depth, etc.)?

-Chris



I am making a simple application that displays a single bitmap
image
in
a picturebox. I am using the Windows Mobile SDK and my application
works fine on the emulator, but on the phone I am getting an
OutOfMemory
Exception whenever I call new Bitmap(filepath). This happens the
first time
an image is loaded and I have tried forcing Garbage Collection
before
I load
it. I am using a Treo 700w Windows Mobile Smartphone. Below is
the
code I
am trying to run.

There is no other application running at the same time, so it is
amazing that I'm getting an OutOfMemory exception. Am I doing
something wrong, or is there any way to prevent this exception?

-- Bruce
private void menuItem3_Click(object sender, EventArgs e)

{

bool loadImage = false;

using (SelectPictureDialog picturePicker = new
SelectPictureDialog())

{

picturePicker.Owner = this;

picturePicker.InitialDirectory = @"\Images";

picturePicker.CameraAccess = false;

picturePicker.ShowDrmContent = true;

picturePicker.ShowForwardLockedContent = false;

picturePicker.Filter = "All files|*.*";

DialogResult result = picturePicker.ShowDialog();

if (result == DialogResult.OK)

{

string fileExtension = Path.GetExtension(picturePicker.FileName);

assetName = picturePicker.FileName;

if (fileExtension.ToLower() == ".jpg")

loadImage = true;

}

}

this.Refresh();

if (loadImage)

{

try

{

pictureBox1.Image = new Bitmap(assetName);

}

catch

{

GC.Collect();

pictureBox1.Image = new Bitmap(assetName);

}

}

}
 
G

Guest

www.opennetcf.org/sdf has links to the community binaries, as well as to the
store for the source code and Studio integration packages.


--
Chris Tacke
OpenNETCF Consulting
www.opennetcf.com
--




Dale said:
Ok, looks like OpenNETCF.Drawing.Imaging should solve my OutOfMemory
problem,
however, where do I get the source code for this library?

The SourceGear Vault does not contain this namespace
(http://vault.netcf.tv/VaultService/...spx?repid=2&path=$/SDF/v1.4/OpenNETCF.Drawing),
and I would rather include the actual source code in our application
rather
then deploying an additional CAB to clients device.

Can you point me to the location where the csharp source code is available
for download?

The solution hasn't changed from what I posted before. The SDF Imaging
stuff will handle this. It's not a CF bug, there just are no classes in
the
CF that do this.

-Chris


Dale said:
I am also having this same problem, have you found a solution? Does
CF2.0
SP1 fix the problem?

What is the recommended way of loading a JPG file from disk into memory
as
Bitmap?

We have a WM5 application which allows the user to add up to 24 photos
from
digital camera sd card or embedded device camera, so any info would be
greatly appreciated...

:

And that app is not using a managed Bitmap class, they're likely using
the
Imaging APIs to scale the photo for view. Obviously your phone
doesn't
have
a 1280x1024 screen, so a large number of the pixels in the bitmap
cannot
be
rendered on your screen and therefore don't need to be loaded. You
essentially need a thumbnail containing only a subset of the bits in
the
image for display. SDF 2.0 Beta [1] has managed wrappers for the
imaging
APIs and Alex Feinman[2] has a usage sample in January and February of
his
blog.

[1] www.openetcf.org/sdf2
[2] http://blog.opennetcf.org/afeinman/

-Chris


I agree with your math. And meanwhile, the phone reports that it has
only
about 6-7 MB of free memory typically. So 4MB is a large portion of
that
memory.



However, by comparison, the phone has a couple built-in apps that
take
photos (at this 1.3 Mpixel resolution using the integrated camera)
as
well
as navigate and view the images stored in the file system. So,
somehow
these apps are able to load images of the same size/resolution as I
am
trying to load with my app.



Any other guidance about how to avoid this OutOfMemory exception in
the
context of my application?



Thanks, Bruce



"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Yes, but it becomes an uncompressed Bitmap when you load it.
1280x1024
is
1.3M pixels and at 24bpp that's around 4MB (if the math in my head
is
correct).

-Chris


Chris,

The JPEG image is 1280x1024, full color (I'm guessing thes are
24-bit
color), and the resulting file on the "disk" is about 80K. Not
very
big, at least when compressed!?

Any recommendations?

Thanks, Bruce

How big is the picture (resolution, color depth, etc.)?

-Chris



I am making a simple application that displays a single bitmap
image
in
a picturebox. I am using the Windows Mobile SDK and my
application
works fine on the emulator, but on the phone I am getting an
OutOfMemory
Exception whenever I call new Bitmap(filepath). This happens
the
first time
an image is loaded and I have tried forcing Garbage Collection
before
I load
it. I am using a Treo 700w Windows Mobile Smartphone. Below is
the
code I
am trying to run.

There is no other application running at the same time, so it is
amazing that I'm getting an OutOfMemory exception. Am I doing
something wrong, or is there any way to prevent this exception?

-- Bruce
private void menuItem3_Click(object sender, EventArgs e)

{

bool loadImage = false;

using (SelectPictureDialog picturePicker = new
SelectPictureDialog())

{

picturePicker.Owner = this;

picturePicker.InitialDirectory = @"\Images";

picturePicker.CameraAccess = false;

picturePicker.ShowDrmContent = true;

picturePicker.ShowForwardLockedContent = false;

picturePicker.Filter = "All files|*.*";

DialogResult result = picturePicker.ShowDialog();

if (result == DialogResult.OK)

{

string fileExtension =
Path.GetExtension(picturePicker.FileName);

assetName = picturePicker.FileName;

if (fileExtension.ToLower() == ".jpg")

loadImage = true;

}

}

this.Refresh();

if (loadImage)

{

try

{

pictureBox1.Image = new Bitmap(assetName);

}

catch

{

GC.Collect();

pictureBox1.Image = new Bitmap(assetName);

}

}

}
 
H

Hilton

I assume you're creating Bitmap objects. Are you calling bitmap.Dispose()
when you're done with it? If not, you have a memory leak due to a bad CF
design (MHO).

Hilton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top