I need to have the path to the image file from the user and save the image in my sql server database.
I get the file from the user and convert it 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 not convert it to BitmapSource. The code I wrote to convert it throws 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 an exception in the string bitmapImage.SetSource (ms);
Exception Information - Catastrophic Failure
source
share