Gdiplus遮罩图像来自另一个图像 [英] Gdiplus mask image from another image

查看:87
本文介绍了Gdiplus遮罩图像来自另一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以使用其他图像的Alpha来操纵图像的Alpha?

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

假设我有一个Image,我想更改其alpha起始不透明,从左至右完全透明,目前我用LinearGradientBrush绘制了另一个图像,并通过逐个像素地循环从第二个图像中设置了原始图像的alpha,是否还有另一个Gdiplus中的某种方式,某些图像蒙版或将两个图像的Alpha混合?

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?

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

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.

推荐答案

我认为您是正确的,因为您必须逐个像素地执行此操作.我还搜索了一种更纯"的方法,但这就是我最终得到的结果:

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

这篇关于Gdiplus遮罩图像来自另一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆