Trying to load certain images gives "Value does not fall within the expected range"

0

Can someone tell me why I get error Value does not fall within the expected range when I try to load certain images using new bitmap? The error seems to be in the metadata.

The error stops out at imgTemp.EndInit(); and the issue seems to be the metadata, but does not say anything specific

Autos preview

heres the code:

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;
    }

Exception Details:

  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, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, 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, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, 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, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, 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, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, 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.
c#
image
image-processing
image-loading
asked on Stack Overflow Sep 24, 2020 by Nevaran • edited Sep 24, 2020 by Nevaran

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0