GrayScale(由 ColorMatrix)导致 OutOfMemoryException。 为什么?

发布于 2024-07-21 07:26:55 字数 1239 浏览 4 评论 0原文

我有 2 个表单,A 和 B。在表单 A 上,我单击一个按钮,图像将被加载到位于表单 B 上的 PictureBox 中。并且,我想通过以下方式将此图像设置为灰度:

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

I have 2 forms, A and B. On the Form A, I click a button and an Image is being loaded to a PictureBox located ona the Form B. And, I want to set GrayScale to this image by:

   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;
    }

This code works properly when the PictureBox is on the same form (A). But, when it is on the Form B, the OutOfMemoryException is raised. Why ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

起风了 2024-07-28 07:26:55

恐怕有更多的问题/事情需要您调查,而不是实际的答案:

  1. 如对您答案的评论 - 图像对象正确吗?

  2. 如果不是,则意味着传递到此方法的 PictureBox 对象有问题,或者您无法正确访问 PictureBox 的图像。

    如果不是,

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

More questions/things for you to investigate rather than an actual answer I'm afraid:

  1. As in the comment to your answer - is the Image object correct?

  2. If not then that implies that there's something wrong with the PictureBox object passed into this method, or that you can't access the PictureBox's Image properly.

My first thought was threading, but both forms should be in the UI thread.

病女 2024-07-28 07:26:55

好的,我已经修好了:)
解决方案是,我必须从 OpenDialog.FileName 创建一个 Bitmap 对象,然后设置 PictureBox.Image = myBitmap

我一开始并没有这样做,我只是设置 PictureBox.Load(OpenDialog.FileName) 。 那是我的错误。

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

Ok, I've Fixed it :)
The solution is, I had to created a Bitmap object from the OpenDialog.FileName, and later set PictureBox.Image = myBitmap

I didn't do it at the beginning, I was just setting PictureBox.Load(OpenDialog.FileName). And that was my mistake.

Ok, thank's for Your cooperation, ChrisF ! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文