我需要从用户那里获得图像文件路径并将图像存储在我的 SQL Server 数据库中。
我从用户那里获取文件并使用该方法转换为 byte[]
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;
}
但现在我无法将其转换回 BitmapSource。
我编写的将其转换回来的代码抛出异常
代码:
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;
}
我在 bitmapImage.SetSource( ms ); 行上遇到异常
异常信息为“灾难性故障”
Maybe SetSource
不读取 MemoryStream,而是链接到它,当您稍后使用 BitmapSource silverlight 想要使用 MemoryStream 来获取图像时,但由于您的使用,它已经被释放并且不再有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)