1

I have valid base64 image . I Convert it to image .

For saving with Image.Save(Path)
I have Gdi+ error .

when i try to save with bitmap . the image will save but the image is complete black Like enter image description here

And here is my code

            var encode = EncodeBase64(model.Base64Photo);

                //model.Base64Photo = model.Base64Photo.Replace("data:image/png;base64,", "").Replace("data:image/jpeg;base64,", "");
                //var imageBytes = Convert.FromBase64String(encode);

                var ms = new MemoryStream(encode);
                var returnImage = Image.FromStream(ms);
                var bitmap = new Bitmap(returnImage);
                bitmap.Save($@"C:\inetpub\wwwroot\Dropbox\Websites\2.fidilio.com\Storage\Images\animal\storage\images\animal\{model.Name}-{model.Email}.jpg", ImageFormat.Jpeg);
                bitmap.Dispose();
     public byte[] EncodeBase64(string data)
    {
        string s = data.Trim().Replace(" ", "+").Replace("-", "+").Replace("/", "+");
        if (s.Length % 4 > 0)
            s = s.PadRight(s.Length + 4 - s.Length % 4, '=');
        return Convert.FromBase64String(s);
    }

I confused so much that where is the problem

3
  • @CodyGray The PhysicalFolderPath is C:\\inetpup\\ Commented Dec 14, 2016 at 18:49
  • Cody.. what?? Anyhow, what GDI error were you getting? Commented Dec 14, 2016 at 18:55
  • A Generic error occured in GDI+ this error Commented Dec 14, 2016 at 18:57

1 Answer 1

2

It looks like you're making memory stream from string, am I right? You should make memory stream from byte array, like this.

string s = "iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAAAAAAcD2kOAAACOklEQVR4Ae3YhbIdIQwA0H7YBshzd3d3d7sKJO+vqwxzty4L7UyTkfU9K0CAN/iXQmCBBf5XYIEFFlhggQXeeqr9dDxvVgi/2F+IeoVw/VfgWoVw7VfgF4H/HHbE5L+uEZNLBnt7c3Rao6+w3t0cntUpEeyfJzRAzxF96b5MalB9p5QEdq3RAhG1virL8YgKR6qG+aj4uK5G2q7kxiMw7ZPAK/BpA/a55MYjpq/hUsBrAda9NV+GwxE90EwB07kKW7BSeuV4BBYpSal2U+GVDd5RSW4P67A7CUw3xmAoRV/71rDKiRoQXoKwrU65BB8qRN1X94lg/9KjQ/kaarkyHAp7GtjyNoQdsMOd8DqgGrUuGezawyq8cs+zj66zIxr1BSVMi3waq9QiR/j1TCHMkU0Axz6Xn47l6+g1fFp+7tem6yEtTHdoQmU2B56988R3QwphgxP3QHi1wCCryZOn+svVUpdCPdBwiWFf69UYQqnu3h4NBhGOOHmfi/cBY5hPbZkaty4pHOqOwnIYfU0Zepl0obEcsEBZurc0C+UX7nnyWWD/0GU6DxZbnKlDz+tFxzE12HSZYN/o1xgDTjjbEIYPIR5Sk85mg50dV7Em31I+2NK1DuULljjraJEWIHR1X3xW2D93q49VaZczj4/5FAsFsOSyD8zpYXV68czlh61nIvo/50AErlUIP5D/6aDHCuHFk7OfjpMVma8WWGCBBRZYYIEFFlhggQUWWGCBBRZYYIEFFlhggQV+B9418JaaYBt6AAAAAElFTkSuQmCC";

    var imageBytes = Convert.FromBase64String(s);

    var ms = new MemoryStream(imageBytes);
    var returnImage = Image.FromStream(ms);
    var bitmap = new Bitmap(returnImage);
    //bitmap.Save($@"C:\inetpub\wwwroot\Dropbox\Websites\2.fidilio.com\Storage\Images\animal\storage\images\animal\{model.Name}-{model.Email}.jpg", ImageFormat.Jpeg);
    bitmap.Save(@"c:\temp\img.jpg");
    bitmap.Dispose();
Sign up to request clarification or add additional context in comments.

2 Comments

well, did you try code from my answer? that is base64 encoded image, created on this site. Are you sure that your method returns valid base64 string?
it says me file is too big and doesnt get me base 64

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.