Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这两种将图像从SQLCE加载到WPF图像的方法会产生不同的结果?_Wpf_Sql Server Ce_Memorystream_Valueconverter - Fatal编程技术网

为什么这两种将图像从SQLCE加载到WPF图像的方法会产生不同的结果?

为什么这两种将图像从SQLCE加载到WPF图像的方法会产生不同的结果?,wpf,sql-server-ce,memorystream,valueconverter,Wpf,Sql Server Ce,Memorystream,Valueconverter,在ValueConverter中,我试图将System.Data.Linq.Binary(SQL CE图像)转换为BitmapImage。此方法有效(图像正确显示在表单上): 此方法不起作用(但奇怪的是,没有引发异常): 良好的编程实践表明,您应该处理您创建的任何流。。。所以我很困惑为什么第二种方法不起作用,而第一种方法起作用。有什么见解吗?试试这个: public object Convert(object value, Type targetType, object parameter,

ValueConverter
中,我试图将
System.Data.Linq.Binary
(SQL CE图像)转换为
BitmapImage
。此方法有效(图像正确显示在表单上):

此方法不起作用(但奇怪的是,没有引发异常):

良好的编程实践表明,您应该处理您创建的任何流。。。所以我很困惑为什么第二种方法不起作用,而第一种方法起作用。有什么见解吗?

试试这个:

public object Convert(object value, Type targetType, object parameter, 
                                                     CultureInfo culture) {
    Binary binary = value as Binary;
    if (binary != null) {
        using (var stream = new MemoryStream(binary.ToArray())) {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad; 
            bitmap.StreamSource = stream;
            bitmap.EndInit();
            bitmap.Freeze(); 
            return bitmap;
        }
    }
    return null;
}

在您的非工作版本中,您的
使用
块意味着在图像实际解码之前流已关闭。

我猜,当您处理
内存流时,您正在使位图的
流源
无效。因此,当位图试图渲染时,没有可用的有效数据。

但是他在
内存流
被释放之前返回位图,不是吗?@VoodooChild,很好,但是WPF有可能在
释放()之后才尝试实际渲染位图吗
已被调用?我这里有一些简单的示例代码:从字节[]到图像的转换代码使用
Image.FromStream(ms)这似乎是特定于Windows窗体的(我使用的是WPF)。我检查了
System.Windows.Media.Imaging.BitmapImage
System.Windows.Controls.Image
,它们都没有
FromStream
方法。谢谢你的链接。
public object Convert(object value, Type targetType, object parameter, 
                                                     CultureInfo culture) {
    Binary binary = value as Binary;
    if (binary != null) {
        using (var stream = new MemoryStream(binary.ToArray())) {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = stream;
            bitmap.EndInit();
            return bitmap;
        }
    }
    return null;
}
public object Convert(object value, Type targetType, object parameter, 
                                                     CultureInfo culture) {
    Binary binary = value as Binary;
    if (binary != null) {
        using (var stream = new MemoryStream(binary.ToArray())) {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad; 
            bitmap.StreamSource = stream;
            bitmap.EndInit();
            bitmap.Freeze(); 
            return bitmap;
        }
    }
    return null;
}