首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GDI+ LockBits / Alpha

GDI+ LockBits / Alpha
EN

Stack Overflow用户
提问于 2010-09-05 07:01:36
回答 1查看 1.3K关注 0票数 0

我试图通过将PNG中的alpha值复制到另一个相同大小的图像上,来创建具有alpha通道的PNG的剪切蒙版。全部使用LockBits,然后使用UnlockBits。我的通道似乎被正确地设置了,但我看不到它在后续的绘制操作中被使用。

为了尽可能简化,我使用了几乎相同的逻辑,只在单个图像中设置红色通道的值,但在保存图像后,再一次没有更改。如果我单步执行代码,红色通道的有效设置是正确的。以下是简化的代码。任何意见或帮助,我们将不胜感激。

代码语言:javascript
复制
        var image = Image.FromFile(@"C:\imaging\image.jpg");
        image.LoadRed();
        image.Save(@"C:\imaging\output.jpg");

    // image.jpg and output.jpg are the same.
    // I would expect output to be washed over with lots of red but it isn't

   public static void LoadRed(this Image destination)
    {
        var destinationBitmap = new Bitmap(destination);

        const int blueChannel = 0;
        const int greenChannel = 1;
        const int redChannel = 2;
        const int alphaChannel = 3;

        var rec = new Rectangle(Point.Empty, destination.Size);

        var destinationData = destinationBitmap.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

        unsafe
        {
            byte* destinationPointer = (byte*)destinationData.Scan0.ToPointer();

            destinationPointer += redChannel;

            for (int i = rec.Width * rec.Height; i > 0; i--)
            {
                *destinationPointer = 255;
                destinationPointer += 4;
            }
        }

        destinationBitmap.UnlockBits(destinationData);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-05 07:43:28

您的问题与您使用提供给扩展方法的图像作为参数创建一个新的Bitmap实例有关。但是,一旦该方法完成,您将保存原始图像,而不是修改后的位图。

更改您的扩展方法以在System.Drawing.Bitmap类型上工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3644308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档