Known issue: WPF will throw COM Exception when create RenderTargetBitmap too fast

See original GitHub issue
  • .NET Core Version: 3.1.300
  • Windows version: 10.0.18362
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

Problem description:

When we use RenderTargetBitmap to take the control screenshots, if our do it too frequent and too fast, then we will find that the framework throws an exception.

Actual behavior:

 System.Runtime.InteropServices.COMException: MILERR_WIN32ERROR

System.Runtime.InteropServices.COMException (0x88980003): MILERR_WIN32ERROR (Exception from HRESULT:0x88980003)
   in System.Windows.Media.Imaging.RenderTargetBitmap.FinalizeCreation()
   in System.Windows.Media.Imaging.RenderTargetBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight, Double dpiX, Double dpiY, PixelFormat pixelFormat)

Expected behavior:

No exception

Minimal repro:

https://github.com/dotnet-campus/wpf-issues/tree/master/RenderTargetBitmapThrowsCOMExceptionWhenCreatedTooFast

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
ulexcommented, Oct 6, 2022

The workaround without forcing the GC is to manually release the handle in the RenderTargetBitmap using reflection:

(bitmap.GetType().GetField("_renderTargetBitmap", BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(bitmap) as IDisposable)?.Dispose();
3reactions
miloushcommented, Jun 3, 2020

Yeah, from my experience if you are running a x64 process (not the default setting anymore) and have plenty of RAM, the finalizers do eventually start kicking in.

As for fix, the BitmapSource should be IDisposable and release the handle/IWICBitmap on disposal rather than in a finalizer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak in RenderTargetBitmap
The leak seems to occur on the RenderTargetBitmap.Render method exclusively, even the DrawingContext.DrawRectangle call doesn't noticeably ...
Read more >
WorkspaceManager is Throwing Exception
Hello, I am getting this exception while using Manager.ApplyWorkspace. The parameter value must be greater than zero.
Read more >
Windows Presentation Foundation (WPF) forum - RSSing.com
If I start creating the bitmaps too fast, the constructor of RenderTargetBitmap throws the exception: MILERR_WIN32ERROR (Exception from HRESULT: 0x88980003). I ...
Read more >
The slow screen redraws on the movement of the mouse - WPF
How can I speed up rendering when moving the mouse? For example, I draw two perpendicular lines on the screen, passing through the...
Read more >
WPF 使用RenderTargetBitmap 快速截图出现COMException ...
RenderTargetBitmap throws COM exception when created too fast: MILERR_WIN32ERROR ... 已经报告官方,请看Known issus: WPF will throw COM Exception when create ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found