Pregunta

¿Hay alguna forma de manipular alfa de una imagen usando alfa de otra imagen?

Supongamos que tengo una imagen y quiero alterar su alfa de inicio opaco a la izquierda y totalmente transparente a la derecha, actualmente dibujo otra imagen con LinearGradientBrush y establezco el alfa de la imagen original de la segunda imagen haciendo un bucle píxel por píxel, ¿hay otra? ¿Cómo en Gdiplus, alguna máscara de imagen, o mezcla alfa de dos imágenes?

Conclusión: parece que en GDI + no hay forma de mezclar dos imágenes, solo parece ser la forma manual iterando a través de píxeles.

¿Fue útil?

Solución

Creo que tienes razón en que tienes que hacer esto píxel por píxel. También he buscado un más "puro" manera de hacerlo, pero esto es lo que terminé con:

    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 );
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top