图像数据在处理过程中发生了溢出:WriteableBitmap WPF

huangapple go评论71阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月6日 20:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定