Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm looking for a way in c# to convert base64 png to base64 jpg.
I'm extracting the data from the data-base and my api needs to return the base64 in jpg format without saving the file during the conversion.
Thanks.

What I have tried:

I've trued to return the thumbnail of the original image but the image is not returning good when saving it (for testing).
Posted
Updated 13-Sep-20 2:53am
Comments
[no name] 13-Sep-20 7:48am    
See here to post at the very bottom: {.NET » C#} Convert png to jpg @CODEKICKER[^]

Quote:
Convert base64 png image to base64 jpg image without saving

Procedure:
- Get base64 stream
- Convert back to normal png format
- Convert to jpg
- Convert to base64
Quote:
I've trued to return the thumbnail of the original image but the image is not returning good when saving it (for testing).

And we are supposed to know what is wrong ?
 
Share this answer
 
This should help you
C#
private byte[] Base64Png2Jpeg(string base64)
{
    byte[] jpgArray;
    Byte[] pngBytes = Convert.FromBase64String(base64);

    using (MemoryStream msPng = new MemoryStream(pngBytes))
    {
        using (Image  img = Image.FromStream(msPng))
        {
            using (MemoryStream msJpeg = new MemoryStream())
            {
                img.Save(msJpeg, ImageFormat.Jpeg);
                jpgArray = msJpeg.ToArray();
            }
        }
    }
    return (jpgArray);
}
 
Share this answer
 
v3
Read the Base64 data into an Image class instance viaq a stream
Save the Image as a JPG into a Stream, and convert that to Base64.
C#
private string FromBase64PNGToBase64JPG(string base64PNG)
    {
    byte[] bytes = Convert.FromBase64String(base64PNG);
    using (MemoryStream msIn = new MemoryStream(bytes))
        {
        using (Image pic = Image.FromStream(msIn))
            {
            using (MemoryStream msOut = new MemoryStream())
                {
                pic.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg);
                return Convert.ToBase64String(msOut.ToArray());
                }
            }
        }
    }
 
Share this answer
 
Comments
[no name] 13-Sep-20 8:58am    
Is the return type of your method really correct?
OriginalGriff 13-Sep-20 9:58am    
Ummm ... yes?
https://docs.microsoft.com/en-us/dotnet/api/system.convert.tobase64string?view=netcore-3.1#System_Convert_ToBase64String_System_Byte___
[no name] 13-Sep-20 10:18am    
Excuse me, I put my question completely unclear. Your code is correct per se, but I interpret the OP's request that he needs a jpeg (binary). Oooh my English :lol:
OriginalGriff 13-Sep-20 10:23am    
90% of the time Base64 is used, it's represented as a string (binary and byte arrays is what you're trying to get away from!)
The other 10% is of course for encryption ... :sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900