The following test function
(Pls do not discuss why using GDI here.)
using System.Drawing;
...
class SomeClass
{
...
public void Test(string name)
{
using (var bmp = new Bitmap(200, 200))
{
using (var gr = Graphics.FromImage(bmp))
{
gr.FillRectangle(Brushes.Green, 0, 0, bmp.Width, bmp.Height);
bmp.Save(Path.Combine(@"C:\Temp\" + name + ".png"));
}
}
}
}
Cases 2. and 3. are called from a (WPF) MenuItem.Command being executed. Case 4 is also called during MenuItem.Command being executed but delegates to the thread pool (ThreadPool.QueueUserWorkItem(...)) for testing purpose.
The problem seems not to be the Save(..) method. Looks like the drawing method(s) (e.g. FillRectangle) of the Graphics do not work as expected (the Brushes look fine).
Well, I cannot anwer my question but I can describe my workaround: Instead of directly calling Test(...) delegating it to the dispatcher seems to help.
Dispatcher.CurrentDispatcher.Invoke(() => { Test("SomeName"); });
User contributions licensed under CC BY-SA 3.0