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'
Maybe SetSource does not read the MemoryStream but links to it and when you later use the BitmapSource silverlight wants to use the MemoryStream to get the image but because of your using it is already disposed and no longer valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With