png如何保持alpha通道但改变背景颜色

时间:2013-03-06 09:57:31

标签: c# c#-4.0 png png-transparency

我有一个带有alpha通道的png图像,对于我们的一个不支持alpha的工具,我需要编辑图像,这样每个透明的像素都会获得特定的颜色,但保留alpha,这样png仍可用于所有支持alpha的工具。

到目前为止,这是我的代码,但它不起作用,任何人都可以提供帮助吗?图片是我的源png,我选择红色作为颜色。

由于

using (Bitmap ImageAttachedConverted = new Bitmap(Picture.Width, Picture.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
    using (Graphics GraphicsPicture = Graphics.FromImage(ImageAttachedConverted))
    {
        GraphicsPicture.InterpolationMode = InterpolationMode.HighQualityBicubic;
        GraphicsPicture.CompositingQuality = CompositingQuality.HighQuality;

        //GraphicsPicture.Clear(Color.Red);

        //GraphicsPicture.DrawImage(
        //    Picture,
        //    new Rectangle(0, 0, ImageAttachedConverted.Width, ImageAttachedConverted.Height),
        //    new Rectangle(0, 0, Picture.Width, Picture.Height),
        //    GraphicsUnit.Pixel);

        // 2nd method pixel bypixel 
        Bitmap img = Picture as Bitmap;
        for (int y = 0; y < ImageAttachedConverted.Height; y++)
        {
            for (int x = 0; x < ImageAttachedConverted.Width; x++)
            {
                //Get Colours at the pixel point 
                Color col1 = img.GetPixel(x, y);

                Color temp = Color.FromArgb(
            0,
            255,
            0,
            0);
                if (col1.A < 100)
                    ImageAttachedConverted.SetPixel(x, y, temp);
                else ImageAttachedConverted.SetPixel(x, y, col1);
            }
        }
        Color temp2 = Color.FromArgb(
            0,
            255,
            0,
            0);
        //ImageAttachedConverted.MakeTransparent(temp2);
    }

    ImageAttachedConverted.Save(Destination.FullName, ImageFormat.Png);
}

0 个答案:

没有答案