我有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