When i try to set the Source of WPF Image to a Image file that is ~11MB size and Shot in 14 MeagaPixcel Camera, the memory shoots up to around 170 MB when the image is rendered on the screen and the memory also never comes down after Rendering. If i try to do the same using .Net 2.0 Picturebox control the memory utilized is only .5MB to 1MB. Logically if the file size of an image is 11MB then it should at the maximum occupy only 11MB while rendering right? What is the cause of such behaviour in WPF? and is there any way to dispose the extra junk of memory after the rendering is compeleted on the screen?
views:
265answers:
1To answer the first part of your question:
Images shot on digital cameras are stored as jpg files and hence compressed. When read into memory it will be uncompressed. This accounts for the difference in size you are seeing here.
For example a photo shot on a Canon EOS 450 has a file size on disk of 3 MB. It's dimensions are 3072 x 2048. This leads to a size in memory of 3072 * 2048 pixels * 24 bits / pixel = 18,874,368 bytes (does that make sense - I'm never 100% sure of these calculations)
The memory usage won't come down until the object holding the image data falls out of scope and is cleared by the garbage collection.
For example you'll need something along the lines of this code:
using (Image image = Image.FromFile(imageName))
{
// Non property item properties
FileName = imageName;
PixelFormat = image.PixelFormat;
Width = image.Size.Width;
Height = image.Size.Height;
foreach (PropertyItem pi in image.PropertyItems)
{
EXIFPropertyItem exifpi = new EXIFPropertyItem(pi);
this.propertyItems.Add(exifpi);
}
}
Once I've got all the information I need from the image the using
statement allows the garbage collection to kick in and free the memory.