I want to design a continues moving "waterfall" to represent a radio spectrum. The way to do this in for example HTML5 is to draw 1 horizontal line at the top. Then copy the entire drawing from the top until the last line -1. (So the whole is shifted 1 line down)
And then again draw 1 line at the top.
Is that even the right approach in Win2d?
It took me some time to discover that it cannot be done in the draw event, the canvas get cleared at every start of a session.
Now struggling with offscreen drawing. For simplification we use just a rectangle in the example.
At the class I define device and offscreen:
private CanvasRenderTarget offscreen;
private CanvasDevice device = CanvasDevice.GetSharedDevice();
private DispatcherTimer Timer;
In the constructor:
offscreen = new CanvasRenderTarget(device, 300, 255, 96);
Timer = new DispatcherTimer();
Timer.Interval = TimeSpan.FromMilliseconds(refreshRate);
Timer.Tick += Timer_Tick;
Timer.Start();
Work in the Timer_Tick event:
private void Timer_Tick(object? sender, object e)
{
using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
{
// Draw rectangle
ds.FillRectangle(10, 10, 100, 100, Colors.Red);
// Source rect to be copied
Rect sourceRegion = new Rect(10, 10, 50, 50);
// Define the destination point on the current canvas
Vector2 destinationPoint = new Vector2(150, 150);
// Copy the specified region from the source to the destination
**ds.DrawImage(offscreen, destinationPoint, sourceRegion); // Unhandled error**
}
if (this.waterfallCanvas != null) this.waterfallCanvas.Invalidate();
}
private void waterfallCanvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
if (offscreen != null) args.DrawingSession.DrawImage(offscreen, 0, 0);
}
It throws a unhandled InteropServices.COMExeption