我有重新調整的位映射功能。這是我簡單地從另一個項目複製它這樣的「麪包和黃油」操作:如何調試誤導性的GDI OutOfMemory異常?
private Bitmap ResizeBitmap(Bitmap orig)
{
Bitmap resized = new Bitmap(this.Xsize, this.Ysize, PixelFormat.Format16bppGrayScale);
resized.SetResolution(orig.HorizontalResolution, orig.VerticalResolution);
using (Graphics g = Graphics.FromImage(resized))
{
g.DrawImage(orig, 0, 0, resized.Width, resized.Height);
}
return resized;
}
不過,我一直在Graphics g = Graphics.FromImage(resized)
越來越OutOfMemory異常。
我知道,when it comes to GDI, OutOfMemory exceptions usually mask other problems。我也很清楚,我想調整圖片的大小並不大而且(據我所知),因爲它們離開當前範圍GC應該沒有問題收集的情況。
不管怎樣,我一直在玩弄它一下,它目前看起來是這樣的:
private Bitmap ResizeBitmap(Bitmap orig)
{
lock(orig)
{
using (Bitmap resized = new Bitmap(this.Xsize, this.Ysize, PixelFormat.Format16bppGrayScale))
{
resized.SetResolution(orig.HorizontalResolution, orig.VerticalResolution);
using (Graphics g = Graphics.FromImage(resized))
{
g.DrawImage(orig, 0, 0, resized.Width, resized.Height);
}
return resized;
}
}
}
但現在我對resized.SetResolution(orig.HorizontalResolution, orig.VerticalResolution);
我得到InvalidOperation異常在黑暗中徘徊。有沒有更好的方法來解決這些煩人的GDI操作?
只是想知道 - 你試圖使用GDI +在ASP.NET應用程序?我們這樣做了一段時間,直到我們發現它不被支持並且會崩潰(有時)。 – GarethOwen
@GarethOwen不,這是一個winforms應用程序。 –