I'm trying to stream an image file with the simple code below.
Stream stream = File.OpenRead(myFileInfo.ToString());
When I do it, Visual Studio send me an exception. This file is a simple jpeg. In Debug mode, I see with the BitmapDecoder class that my file has not Frames. In comparission with other files of same extension, all have one Frame.
I already try the solution using the FileStream class and it doesn't work :'(
My main code is this :
BitmapImage myBitmapImage = new BitmapImage();
using (Stream stream = File.OpenRead(fileInfo.ToString()))
{
myBitmapImage.BeginInit();
myBitmapImage.StreamSource = stream;
myBitmapImage.EndInit();
}
It is written in a converter and naturally is binding to a Image control. But the exception is throw before set in the Image.Source property at the line below :
myBitmapImage.EndInit();
Another detail : the image file can be open with Photoshop, Paint.net and other programs. When these last save a copy, new files doesn't present problem to open with the same code.
But I can't say to our customers to do this all the time (Approximately 50 times in a day :s).
Thanks.
My excpetion detail below:
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:\Users\Gaet\Documents\Visual Studio 2010\Projects\DotNet\MDP.EDIs\EDIs.Imaging\Converter\UriToBitmapSourceConverter.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:
Have a look at this blog post by Scott Hanselman Dealing with Images with Bad Metadata - Corrupted Color Profiles in WPF
His suggested fix worked in my case. From the blog:
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;
Hope this helps.
BitmapImage Art3 = new BitmapImage();
using (FileStream stream = File.OpenRead("c:\\temp\\Album.jpg"))
{
Art3.BeginInit();
Art3.StreamSource = stream;
Art3.EndInit();
}
artwork.Source = Art3;
"artwork" is the XAML object where image is supposed to be shown.
User contributions licensed under CC BY-SA 3.0