-2

I am getting a GDI+ error while saving an image into a folder what is wrong with the below code?

[HttpPost]
public ActionResult CoverPhoto(string thumb_values)
{
        var ImageCode = thumb_values.Split(',');
        var imagepart = ImageCode[1];
        imagepart = imagepart.Replace('"', ' ');
        Image ConvertImage = Base64ToImage(imagepart);
        ConvertImage.Save(@"F:\TestFolder" , ImageFormat.Jpeg ); //Error there
        return View();
}

public Image Base64ToImage(string base64String)
{
        // Convert base 64 string to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        // Convert byte[] to Image
        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true, true);
            return image;
        }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
  • 3
    What **exactly** is the error? – Liam Feb 11 '19 at 11:12
  • 1
    `var ImageCode = thumb_values.Split(',');` this bit of code is weird? What exactly are you sending to the server? You realise that arrays are zero based right? – Liam Feb 11 '19 at 11:13
  • I would guess that you are corrupting the base 64 string by your split and replace action. – P. Grote Feb 11 '19 at 11:15
  • @P.Grote if i does not follow that split and replace process i am getting this error....The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. – shahsawar482 Feb 11 '19 at 11:21
  • @Liam i tried online converting baseImage64 ito image i get proper image that i want but that image is not saving into folder and getting error GDI+.....if i does not follow that split and replace process i am getting this error....The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters – shahsawar482 Feb 11 '19 at 11:22
  • 1
    Possible duplicate of [The input is not a valid Base-64 string as it contains a non-base 64 character](https://stackoverflow.com/questions/15114044/the-input-is-not-a-valid-base-64-string-as-it-contains-a-non-base-64-character) – Liam Feb 11 '19 at 11:29
  • @Liam not helpful because i tried all functions and then apply the string with online converter that does not work. but the process i am using that is working in online converter.problem is only that image not saving into folder error is GDI+ error incurred – shahsawar482 Feb 12 '19 at 01:51
  • {"data":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ"height":250,"left":-926,"top":-794}. i am posting from view to controller without split and replace like this is that right?.....one thing to know that why i am getting "data" at the start of string means first data word after curly bracket because i did not see string like this anywhere. is that right? – shahsawar482 Feb 12 '19 at 02:50

1 Answers1

2

From the above i can see you are passing folder path. Instead you have to pass the filename.

try to change

ConvertImage.Save(@"F:\TestFolder\myImage.jpeg" , ImageFormat.Jpeg );

can you try this code.

[HttpPost]
public ActionResult CoverPhoto(string thumb_values)
{
        var ImageCode = thumb_values.Split(',');
        var imagepart = ImageCode[1];
        imagepart = imagepart.Replace('"', ' ');
        byte[] imageBytes = Convert.FromBase64String(imagepart);
        System.IO.File.WriteAllBytes(@"F:\TestFolder\img.jpg" , imageBytes ); 
        return View();
}

Please refer to documentation for Image.Save method