简体   繁体   English

无法将'System.Data.Linq.Binary'类型的对象强制转换为'System.Byte []'

[英]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. 您必须使用BinaryToArray方法来获取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.

相关问题 如何将'byte []'类型转换为'System.Data.Linq.Binary' - How to convert type 'byte[]' to 'System.Data.Linq.Binary' Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” MySQL数据提供程序-无法将类型为“ System.Byte []”的对象转换为类型为“ System.IConvertible” - MySQL Data Provider - Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible' 错误:System.InvalidCastException:无法将“System.Byte”类型的对象转换为“System.Int32”类型 - Error: System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32' LINQ,聚合操作不支持“System.Data.Linq.Binary”类型 - The type 'System.Data.Linq.Binary' is not supported in aggregation operations - LINQ 无法从SQL Server获取图像数据时将类型为“ System.Int32”的对象转换为类型为“ System.Byte []”的对象 - Unable to cast object of type 'System.Int32' to type 'System.Byte[]' in get Image data from SQL Server 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' 无法将类型为'System.Byte []'的对象强制转换为'System.IConvertible' - Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible' 无法将类型为“ system.byte”的对象转换为类型为“ system.iconvertible”的对象 - Unable to cast object of type 'system.byte ' to type 'system.iconvertible' 错误-无法将类型为“ System.Byte []”的对象转换为类型为“ System.IConvertible”的对象 - Error-Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM