0

When i try to convert a base64string to an Image in C#, I'm getting output as "System.Drawing.Bitmap" instead of the actual Image:

Click for Image

public Image DownFile(string base64String)//string file
{
    //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);
    image.Save("E:/Project Utilitie Connection/FileDownloadTask/Images", System.Drawing.Imaging.ImageFormat.Jpeg);

    return image;
}
stuartd
  • 70,509
  • 14
  • 132
  • 163
Sandeep
  • 85
  • 1
  • 10
  • 1
    [Is there a recommended way to return an image using ASP.NET Web API](https://stackoverflow.com/questions/12467546/is-there-a-recommended-way-to-return-an-image-using-asp-net-web-api) should help – stuartd Jun 29 '17 at 16:06
  • Please show how do you prepare the base64String – Steve Jun 29 '17 at 16:08
  • @Steve [xamarin-android-request-to-server](https://stackoverflow.com/questions/44820825/xamarin-android-request-to-server-is-not-establishing-instead-getting-an-excepti) – Sandeep Jun 29 '17 at 16:12

2 Answers2

0

try a using statement for the MemoryStream, like so:

    byte[] bytes = Convert.FromBase64String(base64String);

    Image image;
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        image = Image.FromStream(ms);
    }

    return image;
Icculus018
  • 1,018
  • 11
  • 19
  • Still the same old output no change. – Sandeep Jun 29 '17 at 16:07
  • _I couldn't able to return the image but gave a path to download the image_ `using (Image img = Image.FromStream(new MemoryStream(imageBytes))) { img.Save("Path with filename and extension ", ImageFormat.Jpeg); return img; }` – Sandeep Jul 04 '17 at 06:55
0

The framework is converting the Image object to string which is why you are seeing System.Drawig.Bitmap

Create a response, giving it the bytes from the converted base64 string and then setting the content type for response so the client will know how to render the content.

public class ValuesController : ApiController {
    [HttpGet]
    public IHttpActionResult DownFile(string base64String) {
        if (!string.IsNullOrWhiteSpace(base64String)) {
            byte[] imageBytes = Convert.FromBase64String(base64String);
            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(imageBytes);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
            return ResponseMessage(response);
        }
        return BadRequest();
    }
}

This is a very simplified example to demonstrate how it can be done. Take some time to review and understand what was done so that it can be implemented into your particular scenario.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • when i run it using browser it is showing error **Error : The resource cannot be found** [Image Link](http://imgur.com/a/V9kbg) – Sandeep Jun 30 '17 at 07:26
  • Change the URL to api/controller but showing some exception [Click for Image](http://imgur.com/a/QaEce) – Sandeep Jun 30 '17 at 07:45