Convert PNG with alpha channel from any bit depth to 32 bit depth using C#

494 Views Asked by At

I want to convert a PNG from any Bit Depth with alpha channel into 32bit depth with alpha channel using C#. properties of sample 4bit png with alpha channel I have tried following two methods, but the output is not converted into 32Bit.

1st method

private Bitmap ChangePixelFormat(Bitmap inputImage, PixelFormat newFormat)
    {
        Bitmap bmp = new Bitmap(inputImage.Width, inputImage.Height, newFormat);
        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.DrawImage(inputImage, 0, 0);
        }
        return bmp;
    }

2nd method

public Bitmap ConvertTo32bpp(string filepath)
    {
        using (System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(filepath))
        {
            Bitmap myBitmap;
            ImageCodecInfo myImageCodecInfo;
            System.Drawing.Imaging.Encoder myEncoder;
            EncoderParameter myEncoderParameter;
            EncoderParameters myEncoderParameters;

            myBitmap = new Bitmap(filepath);

            myImageCodecInfo = GetEncoderInfo("image/png");

            myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;

            myEncoderParameters = new EncoderParameters(1);

            myEncoderParameter =
                new EncoderParameter(myEncoder, 32L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            myBitmap.Save(filepath, myImageCodecInfo, myEncoderParameters);
            return myBitmap;
        }

    }
0

There are 0 best solutions below