【发布时间】:2013-07-28 11:03:57
【问题描述】:
好的,在我的项目资源中,我有一些图片 (.png)。每当用户单击Button 时,一个新图像将显示在ImageBox 中。因为我所有的图像都存储在我的项目资源中,所以我必须通过代码获取Image.Source。我设法通过使用这样的Method 来做到这一点:
public void ImageSource()
{
Bitmap someImage;
BitmapSource someImageSource;
someImage= new Bitmap(Properties.Resources.Image1);
someImageSource = getBitmapSourceFromBitmap(someImage);
ImageBox.Source = someImageSource;
}
public static BitmapSource getBitmapSourceFromBitmap(Bitmap bmp)
{
BitmapSource returnSource = null;
try
{
returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch { returnSource = null; }
return returnSource;
}
在我的应用程序中一切正常。没有错误,没有警告,每次我推送Button 时图像都会改变。在内存中进行了一些监控之后,我注意到每次调用getBitmapSourceFromBitmap 我的内存每次都会爆炸 100MB。
有谁知道为什么会这样?
对不起我的英语。
【问题讨论】:
标签: c# memory-leaks resources bitmapsource