[英]Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]'
Receiving the error 'Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]'.' 收到错误'无法将类型'System.Data.Linq.Binary'的对象转换为'System.Byte []'。' In visual studio.
在视觉工作室。 I am have images stored in a sql server db that I am displaying in a treeview format.
我有一个存储在sql server db中的图像,我以树视图格式显示。 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.
我可以打开dbml设计器并将所有System.Data.Linq.Binary更改为System.Byte,但图像模糊不清。 Any thoughts?
有什么想法吗?
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;
}
}
You will have to use the ToArray
method from Binary
to get the byte[]
value. 您必须使用
Binary
的ToArray
方法来获取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;
}
}
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.