0

I need to convert byte array to image. I have stored image as byte array in my sql server database which I got as an argument of a method.
this is my db table:

Rid    int  
Name   varchar 
Email  varchar  
Photo  Image  
Abdu
  • 185
  • 1
  • 7
  • 17

3 Answers3

2

Given that your initial question is;

How can I convert byte array to image and display the image in a image controller in asp.net

I assume you are working with MVC. The easiest way is to create your controller like this;

public class ImageController : Controller 
{
    public ActionResult GetImage(int i)
    {
        byte[] bytes = db.GetImage(i); //Get the image from your database
        return File(bytes, "image/png"); //or "image/jpeg", depending on the format
    }
}

Then, in your view, simply use

<img src="@Url.Action("GetImage", "Image", new { id = Model.Id })" />

Where Model.Id is the Id of the image.

There are other methods such as converting the image to a Base64-array and putting it in a model, for example;

public class YourModel 
{
    public int Id { get; set; }
    public string ImageB64 { get; set; }
}

And in your controller;

public class YourController : Controller 
{
    public ActionResult YourView(int id)
    {
        var model = new YourModel();
        var image = db.GetImage(id);

        model.Id = id;
        model.ImageB64 = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(image));

        return View(model);
    }
}

And in your view;

<img src="@Model.ImageB64" />

But doing it like the above will not cache the image and will bloat your CSS. Generally this is not the best way to display images. Stick with the first option if possible.

Dion V.
  • 2,090
  • 2
  • 14
  • 24
1

Try this

// Lets assume you have taken image byte array into imageBytes variable
MemoryStream ms = new MemoryStream(imageBytes);
Image image= Image.FromStream(ms);

// Save the file into folder 
image.Save(filename: "filename with path");
Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
  • yes , I tried this ..but it show the error "system.web.ui.webcontrols.image' does not contain a definition for Fromstream" – Abdu May 02 '16 at 09:35
  • you need to add System.Drawing name space. Remove system.web.ui.webcontrols.image for Image. Use Image class from System.Drawing namespace only – Jitendra Pancholi May 02 '16 at 10:27
  • @Abdu: problem solved? – Jitendra Pancholi May 02 '16 at 12:23
  • @ Jitendra Pancholi: Not completely solved . I did as u mentioned . But image is not displayed.. – Abdu May 04 '16 at 05:12
  • @ Jitendra Pancholi: Not completely solved . I will explain more. we both works on an android app. My friend is doing the front-end using Android and i am doing the back-end in asp.net with sql.We are using Jsonservice (RESTfull webservice) for this project..We need to store images to database. On front-end my friend converts image to byte[] and send as a parameter through a method. I just stored the byte into database field named photo whose datatype is image. But we couldn't find the result. Is there any way so that my friend can send the image other than byte[] – Abdu May 04 '16 at 05:28
  • for this situation, you should convert that byte array into image as i mentioned above and save as file into your disk. that would give you the optimal result while returning the data to mobile app. I have already done the same thing few months ago. – Jitendra Pancholi May 04 '16 at 13:05
  • I added a line to save the file into folder as per your requirement. You can now return imageurl to mobile app for fast rendering. – Jitendra Pancholi May 04 '16 at 13:14
  • sir, I just checked my Josonservice methods with a ASP.NET webform. All methods works perfectly. but when my friend use the service then problem comes. – Abdu May 05 '16 at 04:58
  • What is your service url? let me try to consume it and see the error – Jitendra Pancholi May 05 '16 at 05:08
0

You could use Image.FromStream and do something like this.

Image img;

using (var ms = new MemoryStream(bytes))
{
    img= Image.FromStream(ms);
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35