尝试流式图像文件时出错 HRESULT 0x88982F72



我正在尝试使用以下简单代码流式传输图像文件。

Stream stream = File.OpenRead(myFileInfo.ToString());

当我这样做时,Visual Studio会给我发一个例外。此文件是一个简单的 jpeg。在调试模式下,我在 BitmapDecoder 类中看到我的文件没有帧。与具有相同扩展名的其他文件进行比较,所有文件都有一个框架。

我已经使用 FileStream 类尝试了该解决方案,但它不起作用:'(

我的主要代码是这样的:

BitmapImage myBitmapImage = new BitmapImage();
using (Stream stream = File.OpenRead(fileInfo.ToString()))
{
    myBitmapImage.BeginInit();
    myBitmapImage.StreamSource = stream;
    myBitmapImage.EndInit();
}

它是在转换器中编写的,自然绑定到 Image 控件。但是异常是在以下行的 Image.Source 属性中设置之前引发的:

myBitmapImage.EndInit();

另一个细节 :图像文件可以用Photoshop,Paint.net 和其他程序打开。当这些最后保存副本时,新文件不会出现问题,无法使用相同的代码打开。

但我不能一直对我们的客户说这样做(一天大约 50 次:s)。

谢谢。

我的详情如下:

System.IO.IOException was unhandled by user code
  HResult=-2146232800
  Message=Impossible de lire à partir du flux.
  Source=PresentationCore
  StackTrace:
       à System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorContextsDelegate getColorContexts)
       à System.Windows.Media.Imaging.BitmapFrameDecode.get_ColorContexts()
       à System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
       à System.Windows.Media.Imaging.BitmapImage.EndInit()
       à EDIs.Imaging.Converter.UriToBitmapSourceConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture) dans C:UsersGaetDocumentsVisual Studio 2010ProjectsDotNetMDP.EDIsEDIs.ImagingConverterUriToBitmapSourceConverter.cs:ligne 40
       à System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
       à System.Windows.Data.BindingExpression.Activate(Object item)
       à System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
       à System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp)
       à System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp)
       à System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
       à System.Windows.FrameworkTemplate.ReceivePropertySet(Object targetObject, XamlMember member, Object value, DependencyObject templatedParent)
       à System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__4(Object sender, XamlSetValueEventArgs setArgs)
       à System.Xaml.XamlObjectWriter.OnSetValue(Object eventSender, XamlMember member, Object value)
       à System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent)
       à System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)
       à System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx)
       à System.Xaml.XamlObjectWriter.WriteEndObject()
       à System.Xaml.XamlWriter.WriteNode(XamlReader reader)
       à System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
  InnerException: System.Runtime.InteropServices.COMException
       HResult=-2003292302
       Message=Exception de HRESULT : 0x88982F72
       ErrorCode=-2003292302
       InnerException: 

看看斯科特·汉塞尔曼的这篇博客文章 处理具有错误元数据的图像 - WPF 中损坏的颜色配置文件

他建议的修复程序适用于我的情况。来自博客:

var bi = new BitmapImage();
bi.BeginInit();
    bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
    bi.UriSource = new Uri("http://hanselman.com/blog/images/JPGwithBadColorProfile.jpg");
bi.EndInit();
foo.Source = bi;

希望这有帮助。

    BitmapImage Art3 = new BitmapImage();
    using (FileStream stream = File.OpenRead("c:\temp\Album.jpg"))
    {
        Art3.BeginInit();
        Art3.StreamSource = stream;
        Art3.EndInit();
    }
    artwork.Source = Art3;

"插图"是应在其中显示图像的 XAML 对象。

相关内容

最新更新