【问题标题】:GDI+ LockBits / AlphaGDI+ LockBits / Alpha
【发布时间】:2010-09-04 23:01:36
【问题描述】:

我正在尝试通过将我的 PNG 中的 alpha 值复制到另一个相同大小的图像上来创建带有 alpha 通道的 PNG 的剪贴蒙版。全部使用 LockBits,然后是 UnlockBits。看来我的频道设置正确,但我没有看到它在后续绘图操作中使用。

为了尽可能简化事情,我使用几乎相同的逻辑仅在单个图像中设置红色通道值,但在保存图像后,再次没有任何变化。如果我单步执行代码,则红色通道有效设置正确。这是简化的代码。感谢任何 cmets 或帮助。

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

【问题讨论】:

    标签: c# gdi+


    【解决方案1】:

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

    更改您的扩展方法以适用于 System.Drawing.Bitmap 类型。

    【讨论】:

    • 啊,是的。我只是想通了!几天来,我一直在抨击这一点。我正要删除问题,但你赢了! +1!
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 2013-07-22
    • 2013-06-05
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2011-02-24
    • 2012-01-30
    相关资源
    最近更新 更多