MemoryStream from BitmapSource, need to reduce memory consumption
I have a MemoryStream
of 10K which was created from a bitmap of 2MB and compressed using JPEG. Since MemoryStream
can’t directly be placed in System.Windows.Controls.Image
for the GUI, I am using the following intermediate code to convert this back to BitmapImag开发者_运维技巧e
and eventually System.Windows.Controls.Image
.
My question is, when I store this in BitmapImage
, the memory allocation is taking around
2MB. Is this expected? Is there any way to reduce the memory?
I have around 300 thumbnails and this converstion takes around 600MB, which is very high.
Appreciate your help!
Is there any way to reduce the memory?
Yes their is: Don't create your memorystream from the image itself, instead use a thumbnail of it.
Here is a sample code of how to do it:
private void button1_Click(object sender, EventArgs e)
{
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(@"C:\Documents and Settings\Sameh\My Documents\My Pictures\Picture\Picture 004.jpg"); //3664 x 2748 = 3.32 MB
Image myThumbnail = myBitmap.GetThumbnailImage(myBitmap.Width / 100, myBitmap.Height / 100 , myCallback, IntPtr.Zero);
//now use your thumbnail as you like
myThumbnail.Save(@"C:\Documents and Settings\Sameh\My Documents\My Pictures\Picture\Thumbnail 004.jpg");
//the size of the saved image: 36 x 27 = 2.89 KB
//you can create your memory stream from this thumbnail now
}
public bool ThumbnailCallback()
{
return false;
}
And here is more details about the solution.
精彩评论