我有2種形式,A和B.在表單A上,我單擊一個按鈕並將圖像加載到位於表單B上的PictureBox。並且,我想通過以下方式將GrayScale設置爲此圖像:GrayScale(ColorMatrix)會導致OutOfMemoryException。爲什麼?
public void SetGrayScale(PictureBox pb)
{
ColorMatrix matrix = new ColorMatrix(new float[][]
{
new float[] {0.299f, 0.299f, 0.299f, 0, 0},
new float[] {0.587f, 0.587f, 0.587f, 0, 0},
new float[] {0.114f, 0.114f, 0.114f, 0, 0},
new float[] { 0, 0, 0, 1, 0},
new float[] { 0, 0, 0, 0, 0}
});
Image image = (Bitmap)pb.Image.Clone();
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix);
Graphics graphics = Graphics.FromImage(image);
graphics.DrawImage(image,
new Rectangle(0, 0, image.Width, image.Height),
0,
0,
image.Width,
image.Height,
GraphicsUnit.Pixel,
attributes);
graphics.Dispose();
pb.Image = image;
}
此代碼正常工作時在PictureBox是相同的形式(A)上。但是,當它在窗體B上時,會引發OutOfMemoryException。爲什麼?
在什麼地方OutOfMemoryException異常引起人們的關注? – ChrisF 2009-05-29 11:37:30
pb中的圖像有多大? – 2009-05-29 11:38:51