wpf: WriteableBitmap hangs when source bitmap is rendered on other thread

  • .NET Core Version: 5.0.201
  • Windows version: Windows 10x64 20H2 19042.906
  • Does the bug reproduce also in WPF for .NET Framework 4.8? I’m not able to test: .NET Framework 4.8 not available in VS 2019 New Project wizard However, I can reproduce in .NET Framework 4.72.
  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc…)? No.

Problem description:

WriteableBitmap isn’t thread safe.

Creating a WriteableBitmap from a bitmap that’s assigned to an Image control will hang the UI thread when the UI thread is busy rendering the image.

Actual behavior:

The WPF application will hang unexpectedly and randomly.

Expected behavior:

The WPF application should not hang.

Minimal repro:

https://github.com/SetTrend/BitmapSourceTest

private void ProcessImageAsync(string filePath)
{
  TransformedBitmap tb = new TransformedBitmap(new BitmapImage(new Uri(filePath)), new RotateTransform(90));

  CopyBitmapSourceToUi(tb);

  _ = new WriteableBitmap(tb);
}

private void CopyBitmapSourceToUi(BitmapSource image)
{
  BitmapSource uiSource;

  uiSource = BitmapFrame.Create(image);
  uiSource.Freeze();  // locks the bitmap source, so other threads can access

  Dispatcher.Invoke(() => origImage.Source = uiSource);
  //Thread.Sleep(10);   // WPF needs time to render the bitmap. During this period, creating a WriteableBitmap makes the program hang.
}

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 2
  • Comments: 34 (18 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you for your great efforts, @lindexi!

And will it run with my piece of code, and with BitmapSource.Create(), too?

@lindexi:

Splendid! It runs like a charm!

Good work!

@lindexi:

Thank you for getting back on this issue.

Unfortunately we’re experiencing heavy workload this week which will continue for two weeks. I may probably not be able to attend this issue again until mid of June. But I will reply for sure.

Please bear with me.

I downloaded your version, but it doesn’t seem to remedy the issue:

Cannot launch  NET 5 application - 3

Then, when I then copied your .NET 6 files into my project, it didn’t run at all, I’m afraid:

https://i.ibb.co/VxGzKX6/Cannot-launch-NET-5-application-2.gif

I don’t get it … When I try to launch my application after publishing it the way you suggested, it won’t start (still .NET 5, I didn’t overwrite the files yet):

Cannot launch  NET 5 application

Thank you for taking the time @lindexi.

From your responses I have some concerns/questions, as I got a bit confused by them.

Please, let me share my concerns with you:

  • You are addressing me in your replies. To me this implies that you’d expect me to add valuable information to your statement. Yet, all I can provide is an external programmer’s point of view. Yes, I can comprehend for sure - but I wouldn’t be able to provide a PR.

  • I was hoping this to be an internal WPF issue, which is inhibiting the use case of creating/updating bitmaps in the background and showing the (intermediate) result in the GUI thread where applicable.

  • Don’t forget: The code I provided is just a boiled-down minimum repo. In real life, whenever the GUI thread redraws the bitmap (e.g. due to resizing), this issue occurs.

  • I was hoping you’d come up with an internal bug fix. The workaround you provide may be working, but is it really feasible? Shouldn’t rather the sync/locking mechanism be improved to be thread safe?

    I mean: Shouldn’t a code like uiSource = BitmapFrame.Create(image); (from OP) fully disconnect uiSource from image, i.e. also create a new sync object?