英文:
The image data generated an overflow during processing: WriteableBitmap WPF
问题
WriteableBitmap bitmap = new WriteableBitmap(300000, 300000, 300, 300, PixelFormats.Rgb24, null);
System.OverflowException: '图像数据在处理过程中发生溢出。'
我创建了一个 WriteableBitmap,但它抛出了这个错误。当我尝试将 pixelWidth 和 pixelHeight 减少 100 倍时,错误消失了。我应该怎么做?
错误堆栈跟踪:
在 System.Windows.Media.Imaging.WriteableBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight, Double dpiX, Double dpiY, PixelFormat pixelFormat, BitmapPalette palette) 处
WriteableBitmap_DMC.MainWindow.Init() 中的 WriteableBitmap-DMC\MainWindow.xaml.cs: 第 33 行
WriteableBitmap_DMC.MainWindow.MainWindow_Loaded(Object sender, RoutedEventArgs e) 中的 WriteableBitmap-DMC\WriteableBitmap-DMC\MainWindow.xaml.cs: 第 29 行
在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 处
在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 处
在 System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 处
在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 处
在 System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent) 处
在 System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root) 处
在 System.Windows.Media.MediaContext.FireLoadedPendingCallbacks() 处
在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 处
在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) 处
在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) 处
在 System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget) 处
在 System.Windows.Interop.HwndTarget.OnResize() 处
在 System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam) 处
在 System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 处
在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 处
在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 处
在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 处
在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 处
在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 处
在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 处
英文:
WriteableBitmap bitmap = new WriteableBitmap(300000, 300000, 300, 300, PixelFormats.Rgb24, null);
System.OverflowException: 'The image data generated an overflow during processing.'
I'm creating a WriteableBitmap and it just throws this error. When I try to reduce the pixelWidth and pixelHeight by a factor of 100 the error goes. What should I do ?
Error Stack Trace:
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-DMC\MainWindow.xaml.cs:line 33
at WriteableBitmap_DMC.MainWindow.MainWindow_Loaded(Object sender, RoutedEventArgs e) in WriteableBitmap-DMC\WriteableBitmap-DMC\MainWindow.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)
答案1
得分: 2
根据这篇帖子,您明显超出了位图的最大尺寸:
> 图像的最大高度和宽度为2^16像素,每通道32位 * 4通道。 BitmapSource的最大尺寸为2^32字节(64吉字节),最大图像尺寸为四吉像素。
300000远大于2^16。 这将产生90吉像素和总尺寸为270吉字节。 因此,在所有标准上,您都超出了尺寸。
管理如此大的图像文件将会相当繁琐,可以说至少如此。 处理非常大的图像的常见方法是将它们分成较小的块,并逐块呈现。 比如将它们分成2048x2048的块,以在文件大小和文件数量之间取得一个折中。
英文:
According to this post you are significantly exceeding the maximum size of the bitmap:
> The maximum height and width of an image is 2^16 pixels at 32 bits per channel * 4 channels. The maximum size of a BitmapSource is 2^32 bytes (64 gigabytes) and the maximum image size is four gigapixels.
300000 is much larger than 2^16. There would be 90 gigapixels and a total size of 270 gigabytes. So you are exceeding the size on all criteria.
Managing such large image files would be cumbersome to say the least. So the common method to handle very large images is to split them into smaller chunks and render the chunks size by side. Say chunks of 2048x2048 for a compromise between file size and file count.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论