Why when resizing png image by code and changing it's bit depth to 32 it's not the same as resizing the image in Paint ? my whole project is not working well when resizing in code.

Chocolade 536 Reputation points
2023-02-03T16:16:22.37+00:00

I have this code. the method that convert to bit depth 32 and resizing the image :

the image on the hard disk 2023_01_31_02_10.png is size 940x940 bit depth 8 when i'm editing the image in Paint changing the size to 512x512 and saving it for example to 1.png then the image 1.png size is 512x512 bit depth 32.

but when i'm doing it in my code using the method Create32bpp i'm getting other results. the saved file 2.png is size 512x512 bit depth 32 and still i'm getting other results if i'm making the changes in Paint.

public static Bitmap Create32bpp(System.Drawing.Image image, Size size)

    {

        Bitmap bmp = new Bitmap(size.Width, size.Height,

            System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        using (Graphics gr = Graphics.FromImage(bmp))

            gr.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height));

        return bmp;

    }

    private void btnCreateEffect_Click(object sender, EventArgs e)

    {

        Bitmap bmp = Create32bpp(new Bitmap(@"d:\New folder\2023_01_31_02_10.png"), new Size(512, 512));

        bmp.Save(@"d:\New folder\2.png");

        bmp.Dispose();

    }

now when i'm using Paint to do this the result image is what i wanted to be :

1

The image is jpg because i can't upload here bmp images types. but the result is what i wanted to be when making the changes in Paint.

but when i'm doing it with the code the result image is :

newImage1111111

what i want to do is to get batch of images all of them are png type 940x940 size bit depth 8 and convert them to png type images size 512x512 and bit depth 32.

the method above does change the png images size and bit depth but the result is not the same as in the Paint.

in the paint when i change the size to 512x512 then save it as png it's giving me message that it will lose transparency.

but the result is fine when doing it through the code i'm getting this black image.

how can i make what i'm doing in the paint but in code ?

the image above the first one with the white background is looking the same before changing it i want to keep it with the original white background just to change the size to 512x512 and the bit depth to 32.

for some reason it's not working when using the code.

Developer technologies Windows Forms
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2023-02-03T17:46:44.42+00:00

    The code is doing what it's expected to do, which is keeping the transparency, but if you need to have white background, just use g.Clear(Color.White) before you draw the image.

    For example:

    using (var input = Image.FromFile(@"C:\files\input.png"))//640x480, 8bpp
    using (var output = new Bitmap(512, 512, 
        System.Drawing.Imaging.PixelFormat.Format32bppArgb))
    using (var g = Graphics.FromImage(output))
    {
        g.Clear(Color.White);
        g.DrawImage(input, 0, 0, output.Width, output.Height);
        output.Save(@"C:\files\output.png", ImageFormat.Png);
    }
    

    The output image is 32bpp, without transparency, with a white back color.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.