有没有办法使用其他图像中的alpha来操纵图像的alpha?

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

结论:似乎在GDI +中没有办法混合两个图像,只有通过迭代像素才能看出手动方式。

有帮助吗?

解决方案

我认为你是正确的,你必须逐个像素地做这个。我还搜索了一个更“纯粹”的东西。这样做的方法,但这就是我最终得到的结果:

    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 );
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top