I need to have image file path from the user and store the image in my sql server database.
I get the file from the user and convert to byte[] using the method
public static byte[] ImageToByteArray( BitmapSource bitmapSource )
{
byte[] imgAsByteArray = null;
if( bitmapSource != null )
{
imgAsByteArray = ( new WriteableBitmap( ( BitmapSource )bitmapSource ) ).Pixels.SelectMany( p => new byte[]
{
( byte ) p ,
( byte )( p >> 8 ),
( byte )( p >> 16 ),
( byte )( p >> 24 )
} ).ToArray();
}
return imgAsByteArray;
}
but now i can't convert it back to BitmapSource. The code that i wrote to convert it back throw an exception
The code:
public static BitmapSourcebyteArrayToImage( byte[] imageBytes )
{
BitmapImage bitmapImage = null;
using( MemoryStream ms = new MemoryStream( imageBytes, 0, imageBytes.Length ) )
{
bitmapImage = new BitmapImage();
bitmapImage.SetSource( ms );
}
return (BitmapSource)bitmapImage;
}
I get the exception on the line bitmapImage.SetSource( ms );
The exception information is 'catastropic fail'