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:
- Created 2 years ago
- Reactions:2
- Comments:34 (18 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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!