wpf 无法将“System.Data.Linq.Binary”类型的对象转换为“System.Byte[]”类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15058000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]'
提问by KeyboardFriendly
Receiving the error 'Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]'.' In visual studio. I am have images stored in a sql server db that I am displaying in a treeview format. I can open the dbml designer and change all the System.Data.Linq.Binary to System.Byte but the images come out fuzzy and blurry. Any thoughts?
收到错误“无法将类型为“System.Data.Linq.Binary”的对象转换为类型“System.Byte[]”。在视觉工作室。我有图像存储在我以树视图格式显示的 sql server 数据库中。我可以打开 dbml 设计器并将所有 System.Data.Linq.Binary 更改为 System.Byte,但图像模糊不清。有什么想法吗?
Here is the code:
这是代码:
public class ImageBytesConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
BitmapImage bitmap = new BitmapImage();
if (value != null)
{
byte[] photo = (byte[])value;
MemoryStream stream = new MemoryStream();
int offset = 78;
stream.Write(photo, offset, photo.Length - offset);
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
}
return bitmap;
}
}
回答by sa_ddam213
You will have to use the ToArraymethod from Binaryto get the byte[]value.
您必须使用ToArrayfrom 方法Binary来获取byte[]值。
public class BinaryToByteArrayConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value is System.Data.Linq.Binary)
{
byte[] array = (value as System.Data.Linq.Binary).ToArray();
BitmapImage bitmap = new BitmapImage();
using (MemoryStream stream = new MemoryStream())
{
int offset = 78;
stream.Write(array, offset, array.Length - offset);
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
}
return bitmap;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
回答by codekaizen
Use System.Data.Linq.Binary.ToArray()
用 System.Data.Linq.Binary.ToArray()
The fuzziness and blurriness very unlikely due to the conversion of the bytes, but rather to the control you are using to display it not being aligned to the pixel grid, or else being resized slightly, stretching the image and causing it to be upscaled and blurred. Make sure the image is not stretched and the control is aligned to the pixel grid with SnapsToDevicePixels="True"
由于字节的转换,模糊和模糊的可能性很小,而是因为您用来显示它的控件没有与像素网格对齐,或者稍微调整大小,拉伸图像并导致它被放大和模糊. 确保图像没有被拉伸并且控件与像素网格对齐SnapsToDevicePixels="True"
Also, here is some help for your code:
此外,这里有一些对您的代码的帮助:
public class ImageBytesConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
BitmapImage bitmap = new BitmapImage();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
if (value != null)
{
byte[] photo = ((System.Data.Linq.Binary)value).ToArray();
using(MemoryStream stream = new MemoryStream())
{
int offset = 78;
stream.Write(photo, offset, photo.Length - offset);
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
}
return bitmap;
}
return null;
}
}

