我有一个10K的MemoryStream,它是从2MB的位图创建的,并使用JPEG压缩。由于MemoryStream不能直接放在图形用户界面的System.Windows.Controls.Image中,因此我使用以下中间代码将其转换回BitmapImage并最终转换回System.Windows.Controls.Image。
我的问题是,当我将其存储在BitmapImage中时,内存分配大约占用2MB。这是意料之中的吗?有什么方法可以减少内存吗?
我有大约300个缩略图,这个对话大约需要600MB,这是非常高的。
感谢您的帮助!
发布于 2010-05-21 11:33:17
有什么方法可以减少内存吗?
是的,他们是:不要从图像本身创建你的记忆流,而是使用它的缩略图。
下面是一个如何做到这一点的示例代码:
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;
}关于解决方案的here is more details。
https://stackoverflow.com/questions/2879239
复制相似问题