2

I have this table, and want to get the data from it. It contains an image file that I saved as varbinary datatype. I want to show the image in the datagrid as a description.

enter image description here

I have the following code for conversion from bytes to varbinary (image) and from image to byte.

public byte[] ConvertToBytes(BitmapImage bitmapImage)
{
    using (var stream = new MemoryStream())
    {
        var encoder = new PngBitmapEncoder(); // or some other encoder
        encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
        encoder.Save(stream);
        return stream.ToArray();
    }
}

public ImageSource BytesToImage(byte[] imageData)
{
    using (var ms = new MemoryStream(imageData))
    {
        var image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.StreamSource = ms;
        image.EndInit();

        ImageSource imgSrc = image as ImageSource;

        return imgSrc;
    }
}

The conversion from image to byte[] works. But I'm not sure if the code for conversion from bytes to image works.

I also have a compile error in the linq query where I call the data from database:

public List<RoomViewModel> getRooms()
{
    List<RoomViewModel> getRoomsQuery;

    using (db = new MyHotelDBContext())
    {
        getRoomsQuery = (from r in db.Rooms
                         join t in db.RoomTypes on r.RoomTypeID equals t.ID
                         join f in db.Floors on r.FloorID equals f.ID
                         select new RoomViewModel
                         {
                             ID = r.ID,
                             Nr = r.Nr,
                             rType = t.Name,
                             Image = BytesToImage(r.Image as BitmapImage),
                             Description = r.Description,
                             rFloor = f.Floor1
                         }).ToList();
    }
    return getRoomsQuery ?? new List<RoomViewModel>(); 
}

Where is the problem with this line:

Image = BytesToImage(r.Image as BitmapImage)

How can I fix/improve this code?

5
  • Q: the (conversion) from image to byte[] works. But I'm not sure if the code for (conversion) from bytes to image works. A: Exactly what have you done to verify this? SUGGESTION: Until you can sucessfully round-trip from an image => database => back to displayable image, you probably shouldn't assume anything "works"... Q: What exactly is the "compile error"???? Commented Nov 15, 2015 at 19:47
  • You have to pass a byte array to BytesToImage, not a BitmapImage. So it should read BytesToImage(r.Image as byte[]). Commented Nov 15, 2015 at 19:48
  • Note that you can directly return image from BytesToImage. No need for ImageSource imgSrc = image as ImageSource;. Commented Nov 15, 2015 at 19:50
  • I assume it works cuz i get no error and some data get insertet into the db table. i also knew that i can return image but i saw somewhere this way and tried it and forgot to go back in the short way... the error is: Severity Code Description Project File Line Error CS0039 Cannot convert type 'byte[]' to 'System.Windows.Media.Imaging.BitmapImage' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion WpfMyHotel Commented Nov 15, 2015 at 20:22
  • keep in mind that i'm a new developer and i'm still learning and need simple answer with simple explanations Commented Nov 15, 2015 at 20:24

2 Answers 2

0

This question appears to have been addressed here:

Display image from database in asp mvc

However, I would add a method that I find useful for small images. Instead of a spearate controller for generating the image file data, you can simply write the bytes to base64string in the source itself.

You would output the image bytes to the model:

                             select new RoomViewModel
                             {
                                 ID = r.ID,
                                 Nr = r.Nr,
                                 rType = t.Name,
                                 ImageBytes = r.Image as Byte[],
                                 ImageType = "image/jpg", // for example
                                 Description = r.Description,
                                 rFloor = f.Floor1
                             }).ToList();

Then render the src string in the appropriate transformation in the grid. Something like :

<img src="data:@Model.ImageType;base64,@Convert.ToBase64String(Model.ImageBytes)" />
Sign up to request clarification or add additional context in comments.

1 Comment

Not exactly the answer I'm looking for but helpful
0

I got the answer. I just replaced Image = BytesToImage(r.Image as BitmapImage) with Image = r.Image

Comments

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.