none
Store and retreive image from database using entity data model in C# RRS feed

  • Question

  • I am trying to store and retrieve image from the database using C#. For connecting to the database I am using entity data model. The data type in the database is image and when I map it to the .edmx file, it maps as a binary data type. (ErrorImage : image <-> ErrorImage : Binary).

    Can anyone please suggest the code that will help me achieve this?

     

    • Moved by edhickey Friday, June 10, 2011 2:49 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Friday, June 10, 2011 1:40 PM

Answers

  • Hi,

    There is no specific Image type in Entity Framework. All columns that is binary data in the database (Binary, Varbinary, Image etc) are converted to Binary (byte[]) type in EF.

    How to create and display an the image depends on what you're client is. But with the byte[] you can create a MemoryStream and then use this to create your image to display by using for example Bitmap in WinForms, in WPF you can use the BitmapImage class to do the same.

    Example WinForms:

    using(MemoryStream stream = new MemoryStream(myEntity.Image))
    {
      Bitmap image = new Bitmap(stream);
    }
    

    Hope this helps!

     


    --Rune
    Friday, June 10, 2011 4:17 PM

All replies

  • Hi,

    There is no specific Image type in Entity Framework. All columns that is binary data in the database (Binary, Varbinary, Image etc) are converted to Binary (byte[]) type in EF.

    How to create and display an the image depends on what you're client is. But with the byte[] you can create a MemoryStream and then use this to create your image to display by using for example Bitmap in WinForms, in WPF you can use the BitmapImage class to do the same.

    Example WinForms:

    using(MemoryStream stream = new MemoryStream(myEntity.Image))
    {
      Bitmap image = new Bitmap(stream);
    }
    

    Hope this helps!

     


    --Rune
    Friday, June 10, 2011 4:17 PM
  • Hi,

    Maybe you can try save image in database as "varbinary" data type.

    Regards

    Friday, June 10, 2011 4:17 PM
  • Convert image to bytes and save in database and vice-versa.

    Wednesday, April 9, 2014 8:03 AM