How to convert png image to jpg with white background in C#?


How to convert png image to jpg with white background using C# or jQuery ?


Asked by:- SnehalSawant
0
: 12860 At:- 6/8/2018 12:07:33 PM
C# jquery png-to-jpg

your heading says you need to know about how to convert from png to jpg while description says how to convert from jpg to img?? What is your question exactly? 0
By : Vinnu - at :- 6/8/2018 12:32:39 PM






5 Answers
profileImage Answered by:- Vinnu

As your question title says you want to convert from "png to jpg" with white background, an description says "jpg to png" conversion is required, please change it as per your need

I am not sure if there is any way using jQuery but C# is good,here is png to jpg conversion code in C#

// Assuming myImage is the PNG you are converting
using (var b = new Bitmap(myImage.Width, myImage.Height)) {
    b.SetResolution(myImage.HorizontalResolution, myImage.VerticalResolution);

    using (var g = Graphics.FromImage(b)) {
        g.Clear(Color.White);
        g.DrawImageUnscaled(myImage, 0, 0);
    }
    
}

//source https://stackoverflow.com/questions/6513633/convert-transparent-png-to-jpg-with-non-black-background-color

Similar, another way  is as below

using (Bitmap b = new Bitmap(bitmap.Width, bitmap.Height))
{
    using (Graphics g = Graphics.FromImage(b))
    {
        g.Clear(Color.White);
        g.DrawImage(bitmap, 0, 0,bitmap.Width,bitmap.Height);
    }
    // Encoder parameter for image quality
    EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L);
    // Jpeg image codec
    ImageCodecInfo imgcodec = GetEncoderInfo("image/" + ImageFormat.Jpeg.ToString().ToLower());

    EncoderParameters encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = qualityParam;
    b.Save("c:\\imagepath\\SomeFile.jpg", imgcodec, encoderParams);
}

in the above code bitmap is

Bitmap  bitmap = (Bitmap)Bitmap .FromFile("c:\\imagepath\\Somefile.png");

For Converting "jpg to PNG" in C#

 Image bmpImageToConvert = Image.FromFile("images/sample3.jpg");
        Image bmpNewImage = new Bitmap(bmpImageToConvert.Width,
                                       bmpImageToConvert.Height);
        Graphics gfxNewImage = Graphics.FromImage(bmpNewImage);
        gfxNewImage.DrawImage(bmpImageToConvert,
                              new Rectangle(0, 0, bmpNewImage.Width,
                                            bmpNewImage.Height),
                              0, 0,
                              bmpImageToConvert.
                              Width,bmpImageToConvert.Height,
                              GraphicsUnit.Pixel);
        gfxNewImage.Dispose();
        bmpImageToConvert.Dispose();

        

        bmpNewImage.Save("images/sample3t.png", ImageFormat.Png);

namespace required

using System.Drawing;
using System.Drawing.Imaging;

You are done.

2
At:- 6/8/2018 12:46:27 PM


profileImage Answered by:- vikas_jk

Hello @SnehalSawant

Above solution works for me, I executed the above first solution in my local PC MVC project, here is the C# Code

 WebClient wc = new WebClient();
            byte[] imageData = wc.DownloadData(Server.MapPath("~/Uploads/Triple-Spiral-4turns_green_transparent.png"));
           
            MemoryStream stream = new MemoryStream(imageData);
            Image myImage = Image.FromStream(stream);
            using (var b = new Bitmap(myImage.Width, myImage.Height))
            {
                b.SetResolution(myImage.HorizontalResolution, myImage.VerticalResolution);

                using (var g = Graphics.FromImage(b))
                {
                    g.Clear(Color.White);
                    g.DrawImageUnscaled(myImage, 0, 0);
                  
                }
               
                b.Save(Server.MapPath("~/Uploads/Triple-Spiral-4turns_green_transparent.jpg"));
            }

For the above example, I have downloaded a sample .png image named as "Triple-Spiral-4turns_green_transparent.png" and placed it inside my project's "Uploads" folder, here is the complete execution with gif image

png-to-jpg-in-csharp-conversion-example-min.gif

As you can see in the above image, I was able to successfully convert .png to .jpg with white background.

 

1
At:- 6/11/2018 12:49:43 PM


profileImage Answered by:- vikas_jk

I tried to test the above image, It is working fine, png is converting into jpg with same background no black background.

image-converted-min.png

As you can see it will convert transparent image into transparent jpg file.

If you are looking to remove background color from the image which isn't transparent, you would have to use Magick.NET package here are some of the links which may help you

https://forums.asp.net/t/2090662.aspx?How+to+make+PNG+image+with+white+background+transparent+using+C+

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8fd6da9-f7e7-432b-a637-c5958abff1ea/how-to-effectively-remove-background-of-an-image-c?forum=csharpgeneral

This may help you

1
At:- 6/12/2018 7:11:09 AM


profileImage Answered by:- SnehalSawant

0
At:- 6/11/2018 5:27:39 PM
Can you elaborate what does above images mean? 0
By : vikas_jk - at :- 6/12/2018 6:33:23 AM


profileImage Answered by:- SnehalSawant

For converting png images to jpg,it doesnot changes background to white .Is there any other solution for this?

THanks.

-1
At:- 6/11/2018 10:12:06 AM
Hello Sir, It works only for transparent images , but images with dark background color it doesnot work. I am attaching the images in answer post . Thanks. 0
By : SnehalSawant - at :- 6/11/2018 5:25:58 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use