如何解决尝试加载某些图像会导致“值未在预期范围内”
有人可以告诉我为什么会出错吗?当我尝试使用new bitmap
加载某些图像时,值不在预期范围内吗?错误似乎出在元数据中。
该错误在imgTemp.EndInit();
处终止,问题似乎出在元数据上,但未说明任何具体内容
此处是代码:
public static BitmapImage LoadImage(string path,int imgWidth,int imgHeight,Rotation imgRotation)
{
BitmapImage imgTemp = new BitmapImage();
imgTemp.BeginInit();
imgTemp.CacheOption = BitmapCacheOption.OnLoad;
//imgTemp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
using FileStream stream = File.OpenRead(path);
imgTemp.StreamSource = stream;
if (Settings.DownsizeImagetoggle)
{
Rect r = WpfScreen.GetScreenFrom(Application.Current.MainWindow).ScreenBounds;
if (imgWidth > r.Width || imgHeight > r.Height)
imgTemp.DecodePixelWidth = (int)(imgWidth * ScaletoBox(imgWidth,(int)r.Width,imgHeight,(int)r.Height));
}
if (imgRotation != Rotation.Rotate0)
imgTemp.Rotation = imgRotation;
imgTemp.EndInit();
imgTemp.Freeze();
return imgTemp;
}
异常详细信息:
HResult=0x80070057
Message=Value does not fall within the expected range.
Source=PresentationCore
StackTrace:
at System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorContextsDelegate getColorContexts)
at System.Windows.Media.Imaging.BitmapFrameDecode.get_ColorContexts()
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.EndInit()
at FIvstandard.Core.Tools.LoadImage(String path,Int32 imgWidth,Int32 imgHeight,Rotation imgRotation) in D:\CS\Fast Image Viewer Standalone\Fast Image Viewer Standalone\Core\Tools.cs:line 35
at FIvstandard.MainWindow.NewUri(String path) in D:\CS\Fast Image Viewer Standalone\Fast Image Viewer Standalone\MainWindow.xaml.cs:line 774
at FIvstandard.MainWindow.OpenNewFile(String path) in D:\CS\Fast Image Viewer Standalone\Fast Image Viewer Standalone\MainWindow.xaml.cs:line 594
at FIvstandard.MainWindow.OnopenbrowseImage(Object sender,RoutedEventArgs e) in D:\CS\Fast Image Viewer Standalone\Fast Image Viewer Standalone\MainWindow.xaml.cs:line 1095
at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler,Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source,Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEvent newEvent)
at System.Windows.UIElement.onmouseupThunk(Object sender,MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler,RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessstagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd,InputMode mode,Int32 timestamp,RawMouseActions actions,Int32 x,Int32 y,Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd,WindowMessage msg,IntPtr wParam,IntPtr lParam,Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Boolean& handled)
at MS.Win32.HwndSubclass.dispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Int32 numArgs,Delegate catchHandler)
at System.Windows.Threading.dispatcher.LegacyInvokeImpl(dispatcherPriority priority,TimeSpan timeout,Delegate method,Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.dispatchMessage(MSG& msg)
at System.Windows.Threading.dispatcher.PushFrameImpl(dispatcherFrame frame)
at System.Windows.Application.Rundispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at FIvstandard.App.Main()
Inner Exception 1:
ArgumentException: Value does not fall within the expected range.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。