How to convert varBinary into image or video when retrieved from database in C#

10,001

Solution 1

Since your image is stored in binary format in the database, you want to "stream" this into an image object by leveraging the MemoryStream object.

Looking at your code, your solution will look something like this:

BitmapImage bmpImage = new BitmapImage();
MemoryStream msImageStream = new MemoryStream();    

msImageStream.Write(value, 0, value.Length);

bmpCardImage.BeginInit();
bmpCardImage.StreamSource = new MemoryStream(msImageStream.ToArray());
bmpCardImage.EndInit();

image.Source = bmpCardImage;

Solution 2

It's very easy, if you have a binary data and want to create an Image object, use this code:

public Image BinaryToImage(byte[] binaryData)
{
     MemoryStream ms = new MemoryStream(binaryData);
     Image img = Image.FromStream(ms);
     return img;
}
Share:
10,001

Related videos on Youtube

EmaK
Author by

EmaK

Updated on June 04, 2022

Comments

  • EmaK
    EmaK almost 2 years

    I am using visual studio 2010, (desktop application) and using LINQ to SQL to save image/video or audio files to database in dataType VarBinary (MAX). This I can do... Problem is, I can't get them and display them in xaml because I can't get the converting part correct. Here is what I have so far (though its not working);

        private void bt_Click (object sender, RoutedEventArgs e)
        {
           databaseDataContext context = new databaseDataContext();
    
            var imageValue = from s in context.Images
                                where s.imageID == 2
                                select s.imageFile;
    
            value = imageValue.Single().ToString();        
            //convert to string and taking down to next method to get converted in image
        }
    
        public string value { get; set; }
    
    
        public object ImageSource //taking from http://stackoverflow.com/
        {
            get
            {
                BitmapImage image = new BitmapImage();
                try
                {
                    image.BeginInit();
                    image.CacheOption = BitmapCacheOption.OnLoad;
                    image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    image.UriSource = new Uri(value, UriKind.Absolute);
                    image.EndInit();
    
                    Grid.Children.Add(image);
                }
                catch { return DependencyProperty.UnsetValue; } return image;
            }
    
        } 
    

    I not even sure if I am on the correct track? And I am assuming that video or audio is quite similar methods?

  • EmaK
    EmaK about 13 years
    Hi, in the 3rd line, msImageStream.Write(value, 0 , value.Length) I am getting a syntext error because there are looking for (Byte[], int, int), Variable 'value' is type string... which i know is incorrect now. What type must i assign it as i read it of the database? I had; value = imageValue.Single().ToString(); Thanks
  • EmaK
    EmaK about 13 years
    Thanks Max, i had came across this before but kept getting syntex error for using 'FromStream'. it says .Image does not contain a definition for FromStream. Do you know y this is? or what im missin? thanks
  • Dillie-O
    Dillie-O about 13 years
    If you can pull your value down from the database into a byte array instead of a string, you'll be set. Since it is stored as a varbinary type, this shouldn't be difficult.
  • EmaK
    EmaK about 13 years
    Great, thanks! it works! can i ask, is it much the same for video or audio? i will have a search to see what i can find.
  • Dillie-O
    Dillie-O about 13 years
    @Emma Mack: I'm glad it worked! I haven't worked with the audio objects, so I can't tell you, but ask on SO, somebody is bound to know.
  • Maxim Sautin
    Maxim Sautin about 13 years
    Emma, you have to add: 1) Reference to system.drawing.dll 2) Using System.Drawing; See details: msdn - Image.FromStream
  • Schoof
    Schoof over 12 years
    I can't import System.Drawing, it does not excist. Any ideas?
  • Maxim Sautin
    Maxim Sautin over 12 years
    Details: 1) In the "Solution Explorer" right click on the project name 2) Select "Add Reference ..." 3) In the tab ".NET" select "System.Drawing" The System.Drawing has to be on your machine in any case if you have installed .Net platform (any version). I hope this will help, Cheers!