来自另一幅图像的 Gdiplus 掩模图像

发布于 2024-07-25 04:49:09 字数 245 浏览 11 评论 0原文

有没有办法使用另一个图像的 Alpha 来操纵图像的 Alpha?

假设我有一个图像,我想改变它的 alpha,从左侧开始不透明,在右侧完全透明,目前我用 LinearGradientBrush 绘制另一个图像,并通过逐像素循环从第二个图像设置原始图像的 alpha,Gdiplus 中还有另一种方法吗、一些图像蒙版,或者混合两个图像的 alpha?

结论:GDI+ 中似乎没有办法混合两个图像,唯一的方法似乎是通过迭代像素的手动方式。

Is there a way to manipulate alpha of an image using alpha from another image?

Suppose I have a Image and I want to alter its alpha starting opaque at left and totally transparent at right, currently I draw another image with LinearGradientBrush and set alpha of orginal image from second image by looping pixel by pixel, is there another way in Gdiplus, some image mask, or blending alpha of two images?

Conclusion: it seems there is no way in GDI+ to blend two images, only way seems to be the manual way by iterating thru pixels.

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

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

发布评论

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

评论(1

爱冒险 2024-08-01 04:49:09

我认为你是正确的,因为你必须逐像素地执行此操作。 我还寻找了一种更“纯粹”的方法来做到这一点,但这就是我最终得到的:

    public enum ChannelARGB
    {
        Blue = 0,
        Green = 1,
        Red = 2,
        Alpha = 3
    }

    public static void transferOneARGBChannelFromOneBitmapToAnother(
        Bitmap source,
        Bitmap dest,
        ChannelARGB sourceChannel,
        ChannelARGB destChannel )
    {
        if ( source.Size!=dest.Size )
            throw new ArgumentException();
        Rectangle r = new Rectangle( Point.Empty, source.Size );
        BitmapData bdSrc = source.LockBits( r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
        BitmapData bdDst = dest.LockBits( r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb );
        unsafe
        {
            byte* bpSrc = (byte*)bdSrc.Scan0.ToPointer();
            byte* bpDst = (byte*)bdDst.Scan0.ToPointer();
            bpSrc += (int)sourceChannel;
            bpDst += (int)destChannel;
            for ( int i = r.Height * r.Width; i > 0; i-- )
            {
                *bpDst = *bpSrc;
                bpSrc += 4;
                bpDst += 4;
            }
        }
        source.UnlockBits( bdSrc );
        dest.UnlockBits( bdDst );
    }

I think you're correct in that you have to do this pixel-by-pixel. I've also searched for a more "pure" way to do it, but this is what I ended up with:

    public enum ChannelARGB
    {
        Blue = 0,
        Green = 1,
        Red = 2,
        Alpha = 3
    }

    public static void transferOneARGBChannelFromOneBitmapToAnother(
        Bitmap source,
        Bitmap dest,
        ChannelARGB sourceChannel,
        ChannelARGB destChannel )
    {
        if ( source.Size!=dest.Size )
            throw new ArgumentException();
        Rectangle r = new Rectangle( Point.Empty, source.Size );
        BitmapData bdSrc = source.LockBits( r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
        BitmapData bdDst = dest.LockBits( r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb );
        unsafe
        {
            byte* bpSrc = (byte*)bdSrc.Scan0.ToPointer();
            byte* bpDst = (byte*)bdDst.Scan0.ToPointer();
            bpSrc += (int)sourceChannel;
            bpDst += (int)destChannel;
            for ( int i = r.Height * r.Width; i > 0; i-- )
            {
                *bpDst = *bpSrc;
                bpSrc += 4;
                bpDst += 4;
            }
        }
        source.UnlockBits( bdSrc );
        dest.UnlockBits( bdDst );
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文