Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting an image to png on upload

When a user uploads a jpg/gif/bmp image, I want this image to be converted to a png image and then converted to a base64 string.

I've been trying to get this to work but I've hit a brick wall really, can anyone help me out please?

My current code without the image conversion is below:

public ActionResult UploadToBase64String(HttpPostedFileBase file)
        {

                var binaryData = new Byte[file.InputStream.Length];
                file.InputStream.Read(binaryData, 0, (int) file.InputStream.Length);
                file.InputStream.Seek(0, SeekOrigin.Begin);
                file.InputStream.Close();

                string base64String = Convert.ToBase64String(binaryData, 0, binaryData.Length);

...
}
like image 832
Sam Jones Avatar asked Jun 25 '13 10:06

Sam Jones


1 Answers

You're not converting it at all there.. you can use something like this:

using System.Drawing;

Bitmap b = (Bitmap)Bitmap.FromStream(file.InputStream);

using (MemoryStream ms = new MemoryStream()) {
    b.Save(ms, ImageFormat.Png);

    // use the memory stream to base64 encode..
}
like image 124
Simon Whitehead Avatar answered Sep 25 '22 07:09

Simon Whitehead