2

As an example I have converted a canvas element with a re-sized image and posted into a hidden input field that's now encoded as

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...

This value then posted to the same page which I need to convert this string into an image and save onto the server.

Code Behind File (upload.aspx)

protected void btnUpload_Click(object sender, EventArgs e)
    {
        HttpPostedFile filePosted = Request.Files["newinput"];
        string base64String = filePosted.ToString();

            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);

//I DONT KNOW HOW TO WRITE ABOVE INTO THE SaveAs CONDITION BELOW


        if (filePosted != null && filePosted.ContentLength > 0)
        {
            string fileNameApplication = Path.GetFileName(filePosted.FileName);
            string fileExtensionApplication = Path.GetExtension(fileNameApplication);

            // generating a random guid for a new file at server for the uploaded file
            string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
            // getting a valid server path to save
            string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile);

            if (fileNameApplication != String.Empty)
            {
                filePosted.SaveAs(filePath);
            }

        }

I'm pretty sure I need to convert this the imagedata to a binary file before saving on the server but I can't quite get how I need to amend the code above. Any ideas? The code to save to the server doesn't work.

Once I have converted this to an image and changed it's name as above - I'm storing this back to a database via LINQ - with a URL appended to it.

Any help would be greatly appreciated.

Chris
  • 67
  • 1
  • 2
  • 10
  • Technically, no that is not the answer as I've tried that and that code doesn't work, therefore the reason why I'm posting. – Chris Jun 01 '15 at 04:39
  • Any error/exception here ? – Dhrumil Jun 01 '15 at 04:40
  • I don't quite understand how to add 'code' File.WriteAllBytes( Path.Combine ( @"D:\apps\PJP_TEST\PJPTest\Online\ImageStorage\" , imgName ) , imageBytes ); – Chris Jun 01 '15 at 04:42
  • I want to be able to convert the encoding to a file and save onto the server (not client side) – Chris Jun 01 '15 at 04:43
  • So the link proposed as possible duplicate is quite right. You just need to get the encoded part from the string and use the answer given there. – Sami Kuhmonen Jun 01 '15 at 04:45
  • No. I have already passed the encoded part and I've just modified my code above to include but can't quite workout how to save the binary file from the decoded string to the server. See above. – Chris Jun 01 '15 at 04:57
  • BTW - I'm no C# expert therefore why I'm asking for any help so any help would be greatly appreciated. – Chris Jun 01 '15 at 04:59

1 Answers1

3

Hope below functions helps.

public string ImageToBase64(Image image,
          System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }

        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }

EDIT 1 -

From the comments it seems that you are getting base64 string and you need to save it as image on server and then whenever required you need to show that image using physical server path.

Ok. Base64ToImage will give you image for your base64 string. You can save it on server using

image.Save("PATH", System.Drawing.Imaging.ImageFormat.Jpeg);

And this "PATH" you have supplied or created can be stored in DB as URL, which you can use at the time of display.

Note: Make sure that you have write access to folder where you are saving image.

EDIT-2 Your function should look like below. Please put validation code, error handling as required.

protected void btnUpload_Click(object sender, EventArgs e)
    {
        HttpPostedFile filePosted = Request.Files["newinput"];
        string base64String = filePosted.ToString();

            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            System.Drawing.Image image   = System.Drawing.Image.FromStream(ms, true);
            string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
            string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile);
            image.Save(filepath,ImageFormat.Jpeg);
   }
Amit
  • 882
  • 6
  • 13
  • Thanks Amit. How would I call this to include into my code above? Now, I'm already passing an image that has been encoded already from a canvas therefore I'm just wanting to convert the data from postedFile into an binary image and save to the server – Chris Jun 01 '15 at 05:11
  • Try with above functions. It will work. You do not need to remove data:image/jpeg;base64.... – Amit Jun 01 '15 at 05:23
  • I only need to use the Function Base64ToImage as I already have in the format. So the output of return image would be the filename as well? – Chris Jun 01 '15 at 05:25
  • I'm needing to save the it as a binary file and then rename the file based on my code (I need to assign the GUID as it's file name) – Chris Jun 01 '15 at 05:26
  • When I use your code @amit I'm getting the following error Cannot implicitly convert type 'System.Drawing.Image' to 'System.Web.UI.WebControls.Image' This is when I hover over return image – Chris Jun 01 '15 at 05:30
  • Function ImageToBase64 will give you binary. Write it in file or save in database. When you want to show image...read file string or from database column and use function Base64ToImage...this will give you image. – Amit Jun 01 '15 at 05:31
  • I'm confused here. I already have passed the file as Base64 (and then need to save it back to a binary file so I can save **on the web server**. I don't want to save into a database apart from the converted file filename. When I access the file it will be via a URL that I have created and stored into a databse that references the physical file on the web server – Chris Jun 01 '15 at 05:34
  • Do you just want to upload image on webserver? Then filePosted.SaveAs(filePath); this will serve the purpose. You do not need to convert it into binary and then to image. If you are getting Base64 encoded string and want to convert it into image and save on webserver then write that encoded string as image file on webserver directly. – Amit Jun 01 '15 at 05:41
  • The SaveAs function won't work as the file is base64 format. Don't I need to convert it to binary before saving to the web server? I am getting it as Base64 encoded sting but need to covert and save on web server directly which your code throws an error – Chris Jun 01 '15 at 05:45
  • Please check my EDIT. – Amit Jun 01 '15 at 05:57
  • I'm calling this function from enclosing around postFile however, I'm still not sure how this ties in with my existing code. – Chris Jun 01 '15 at 09:15
  • Thanks Amit. I have noticed that the method? that I've trying to extract the data isn't correct as it's passing null data to it HttpPostedFile filePosted = Request.Files["newinput"]; I don't know enough about what is happening here - but basically I need to pass it as from Request.Form["newinput"] but don't know how to call it at this stage. Any ideas? – Chris Jun 01 '15 at 11:56
  • I should've also added that the image data is being passed from `code` `code` This is where the encoded canvas data is being stored and then posted to the code behind function. I know this has data as I can do request.form["newinput"] and I can see the data string. You cannot call a hidden field as such like newinput.PostedFile etc. Any ideas? – Chris Jun 01 '15 at 12:11
  • It can be accessed as HiddenField.Value. If your original question has been answered then please close the same and open new one for the new problems you are encountering. Thanks – Amit Jun 01 '15 at 12:15
  • Thanks Amit - appreciate your help. I'll open another one as I was able as I'm now getting that the base64 string is bad input. – Chris Jun 01 '15 at 12:29
  • There's no method `Save` in the `Image` class – EvgeniySharapov Jan 26 '16 at 03:19