question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WriteableBitmap hangs when source bitmap is rendered on other thread

See original GitHub issue
  • .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.
}

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
SetTrendcommented, May 12, 2021

Thank you for your great efforts, @lindexi!

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

1reaction
SetTrendcommented, May 24, 2021

@lindexi:

Splendid! It runs like a charm!

Good work!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cross-thread exception when trying to update image from ...
This leaves a race: the dispatcher can first set the bitmap as the source, and then your loop may run and try to...
Read more >
WriteableBitmap Class (System.Windows.Media.Imaging)
The UI thread responds to user input, timers, and other events. An application can have multiple UI threads. The render thread composes and...
Read more >
Fast updates of WriteableBitmap? : r/csharp
I'm working on an application that requires me to quickly update a 300 x 600 pixel WriteableBitmap (or really just a Bitmap, but...
Read more >
WriteableBitmap crashes on constructor when run from ...
I am trying to use WriteableBitmap in one of my tests. However, when launched via opentap UI Editor test launched, WriteableBitmap crashes ......
Read more >
Monogame slow inside WPF - Windows
Hey, I'm trying to render via Monogame inside multiple WPF controls (as preview what it's going to look like) and I have serve...
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