1

我有一个带有 alpha 通道的 png 图像,对于我们的一个不支持 alpha 的工具,我需要编辑图像,以便每个透明的像素都获得特定的颜色但保留 alpha,因此 png 仍然适用于所有工具谁支持阿尔法。

到目前为止,这是我的代码,但它不起作用,有人可以帮忙吗?图片是我的源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);
}
4

0 回答 0