处理过程中产生溢出的图像数据:WriteableBitmap WPF


WriteableBitmap bitmap = new WriteableBitmap(300000, 300000, 300, 300, PixelFormats.Rgb24, null);

System.OverflowException: 'The image data generated an overflow during processing.'

我正在创建一个WriteableBitmap,它只是抛出这个错误。当我尝试将pixelWidth和pixelHeight减少100倍时,错误就出现了。我该怎么办?

错误堆栈跟踪:

at System.Windows.Media.Imaging.WriteableBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight, Double dpiX, Double dpiY, PixelFormat pixelFormat, BitmapPalette palette)
at WriteableBitmap_DMC.MainWindow.Init() in WriteableBitmap-DMCMainWindow.xaml.cs:line 33
at WriteableBitmap_DMC.MainWindow.MainWindow_Loaded(Object sender, RoutedEventArgs e) in WriteableBitmap-DMCWriteableBitmap-DMCMainWindow.xaml.cs:line 29
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.RaiseEvent(RoutedEventArgs e)
at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
at System.Windows.Interop.HwndTarget.OnResize()
at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(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)

根据这篇文章,你明显超过了位图的最大尺寸:

图像的最大高度和宽度为2^16像素,每通道32位* 4通道。BitmapSource的最大大小是2^32字节(64千兆字节),最大图像大小是4千兆像素。

300000远远大于2^16。它将有900亿像素,总大小为270千兆字节。所以你的尺寸超过了所有标准。

管理这么大的图像文件至少可以说是很麻烦的。因此,处理非常大的图像的常用方法是将它们分成更小的块,并按块的大小进行渲染。在文件大小和文件数之间折衷,例如2048x2048块。

最新更新