2009-05-29 83 views
2

我有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。为什么?

+0

在什么地方OutOfMemoryException异常引起人们的关注? – ChrisF 2009-05-29 11:37:30

+0

pb中的图像有多大? – 2009-05-29 11:38:51

回答

1

更多问题/事情对你进行调查,而不是一个实际的答案恐怕:

  1. 如你的答案评论 - Image对象是否正确?

  2. 如果没有,那么这意味着,有一些错误的图片框对象传入此方法,或者你不能正常访问图片框的形象。

我的第一个想法是线程化,但两种形式都应该在UI线程中。

0

好吧,我固定它:) 解决的办法是,我不得不创建从OpenDialog.FileName Bitmap对象,后来设置PictureBox.Image = MYBITMAP

真的不是我的一开始,我只是设置PictureBox.Load(OpenDialog.FileName)。这是我的错误。

好的,谢谢对您的合作,ChrisF! :)

相关问题