1

Want to convert byte[] to Imagesource

here is my code for convert to byte

public object BufferFromImage(System.Windows.Media.ImageSource imageSource)
    {
        if (imageSource != null)
        {
            var image = (BitmapSource)imageSource;
            BitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(image));
            using (var ms = new MemoryStream())
            {
                encoder.Save(ms);
                return ms.ToArray();
            }
        }
        else
        {
            return DBNull.Value;
        }
    }

code for byte[] to Imagesource

 public ImageSource ByteToImage(byte[] imageData)
    {
        BitmapImage biImg = new BitmapImage();
        MemoryStream ms = new MemoryStream(imageData);
        biImg.BeginInit();
        biImg.StreamSource = ms;
        biImg.EndInit();
        ImageSource imgSrc = biImg as ImageSource;
        return imgSrc;
    }

This is giving me this error:

An unhandled exception of type 'System.NotSupportedException' occurred in PresentationCore.dll

Additional information: No imaging component suitable to complete this operation was found.

What is causing this and how can I fix it?

Sam Alex
  • 442
  • 1
  • 6
  • 21
  • 1
    [http://stackoverflow.com/questions/22065815/how-to-convert-byte-array-to-imagesource-for-windows-8-0-store-application](http://stackoverflow.com/questions/22065815/how-to-convert-byte-array-to-imagesource-for-windows-8-0-store-application) – Eminem Apr 07 '15 at 08:43
  • Use this link for file upload http://www.aspsnippets.com/Articles/Save-Files-to-SQL-Server-Database-using-FileUpload-Control.aspx – Pradnya Bolli Apr 07 '15 at 08:43
  • http://stackoverflow.com/a/8901493/4513879 use this link also – Pradnya Bolli Apr 07 '15 at 08:49
  • I have tried But not working – Sam Alex Apr 07 '15 at 11:16

1 Answers1

0

Looks like you can save it to a MemoryStream then convert the MemoryStream to a byte array.try to this

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray
}

//second

public byte[] imgToByteArray(Image img)
{
        using (MemoryStream mStream = new MemoryStream())
        {
            img.Save(mStream, img.RawFormat);
            return mStream.ToArray();
        }
}
Haresh Shyara
  • 1,826
  • 10
  • 13