Reputation: 1106
So I'm trying to draw an overlay. This overlay is updated every 40 ms(this has to be high speed). It seems wpf doesn't draw bitmaps, so I tried to convert the Bitmap to a Bitmapsource. Problem with this is the conversion pretty much has to copy it to convert it. Bitmaps aren't threadsafe then in the middle of saving, its trying to update it and explodes. Locks don't seem to want to work.
My goal was to create the overlay once, and reuse it(clear it or something). Is there a way to either:
Here is the drawing method in the C# library. The imaging library seems to be wpf specific so I can't use say a WriteableBitmap off the get go.
private void UpdateOverlay(List<DrawableShape> shapes, Bitmap bitmap)
{
using (var g = Graphics.FromImage(bitmap)) //explosion here( ui thread usually using it at this time).
{
//g.Clear(Color.Transparent);
foreach (var shape in shapes)
{
switch (shape.Type)
{
case ShapeType.Circle:
g.DrawEllipse(new Pen(shape.Color), shape.Dimensions);
break;
case ShapeType.CircleFilled:
g.FillEllipse(new SolidBrush(shape.Color), shape.Dimensions);
break;
case ShapeType.Square:
g.DrawRectangle(new Pen(shape.Color), shape.Dimensions);
break;
case ShapeType.SquareFilled:
g.FillRectangle(new SolidBrush(shape.Color), shape.Dimensions);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
The rest is just a binding to the overlay with a value converter that calls CreateBitmapSourceFromHBitmap or Saves the bitmap to a stream etc.
Any help or insight into this matter would be greatly appreciated!
Upvotes: 2
Views: 1636
Reputation: 9255
Use a WriteableBitmap. It maintains a front and back buffer, so you can write on the back buffer while the front buffer is displayed in a Image
element. When you're done with the back buffer, data are copied to the front.
Upvotes: 1