Win2D。触发重绘事件时保留CanvasControl的旧图形 [英] Win2D. Keep old drawing of CanvasControl when redraw event fired

查看:70
本文介绍了Win2D。触发重绘事件时保留CanvasControl的旧图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单示例


C#

 void canvasControl_Draw (CanvasControl发送者,CanvasDrawEventArgs args)
{
var rnd = new Random(DateTime.Now.Millisecond);
args.DrawingSession.DrawEllipse((byte)rnd.Next(150),(byte)rnd.Next(100),(byte)rnd.Next(150),30,Colors.Red,3);

canvasControl.Invalidate();
}


Xaml

< canvas:CanvasControl x:Name =" canvasControl" Grid.Row = QUOT 1 QUOT; Draw =" canvasControl_Draw" /> 




每个新的Draw事件CanvasControl清晰图像。我需要保留图表的旧数据。如何避免每次清除CanvasControl并在示例中看到许多省略号?

解决方案

您好,
 

创建绘图会话时,Win2D始终自动清除Canvas Control。但是CanvasRenderTargets不是。应用程序可以对CanvasRenderTargets进行增量更改,并避免每次重绘整个场景。

您可以参考以下代码

 
private CanvasRenderTarget _offscreen;

private void CanvasControl_OnDraw(CanvasControl sender,CanvasDrawEventArgs args)
{
if(_offscreen == null)
{
CanvasDevice device = CanvasDevice.GetSharedDevice( );
_offscreen = new CanvasRenderTarget(device,width:(int)sender.ActualWidth,height:(int)sender.ActualHeight,dpi:96);
}

var rnd = new Random(DateTime.Now.Millisecond);
using(CanvasDrawingSession ds = _offscreen.CreateDrawingSession())
{
ds.DrawEllipse((byte)rnd.Next(150),(byte)rnd.Next(100),(byte) )rnd.Next(150),30,Colors.Red,3);
}

if(_offscreen!= null)
{
args.DrawingSession.DrawImage(_offscreen,
new Rect(50,50,_offscreen。 Bounds.Width,_offscreen.Bounds.Height));
}

CanvasControl.Invalidate();
}






最好的问候,


Simple example

C#

 void canvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            var rnd = new Random(DateTime.Now.Millisecond);
            args.DrawingSession.DrawEllipse((byte)rnd.Next(150), (byte)rnd.Next(100), (byte)rnd.Next(150), 30, Colors.Red, 3);
            
            canvasControl.Invalidate();
        }

Xaml

<canvas:CanvasControl x:Name="canvasControl" Grid.Row="1"    Draw="canvasControl_Draw"/>


Every new Draw event CanvasControl clear image. I need to keep old data for charts.How to avoid of clearing CanvasControl every time and see many ellipses in the example?

解决方案

Hello
 
Canvas Control is always cleared automatically by Win2D when a drawing session is created. But CanvasRenderTargets is not. Apps have the ability to make incremental changes to CanvasRenderTargets, and avoid redrawing an entire scene every time.
You can refer to the following code

 
        private CanvasRenderTarget _offscreen;
 
        private void CanvasControl_OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            if (_offscreen == null)
            {
                CanvasDevice device = CanvasDevice.GetSharedDevice();
                _offscreen = new CanvasRenderTarget(device, width: (int)sender.ActualWidth, height: (int)sender.ActualHeight, dpi: 96);
            }
 
            var rnd = new Random(DateTime.Now.Millisecond);
            using (CanvasDrawingSession ds = _offscreen.CreateDrawingSession())
            {
                ds.DrawEllipse((byte)rnd.Next(150), (byte)rnd.Next(100), (byte)rnd.Next(150), 30, Colors.Red, 3);
            }
 
            if (_offscreen != null)
            {
                args.DrawingSession.DrawImage(_offscreen,
                    new Rect(50, 50, _offscreen.Bounds.Width, _offscreen.Bounds.Height));
            }
 
            CanvasControl.Invalidate();
        }
 


Best Regards,


这篇关于Win2D。触发重绘事件时保留CanvasControl的旧图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆