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).
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 ?
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);
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.
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());
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.