C# Windows应用商店应用放大镜

C# Windows应用商店应用放大镜,c#,windows-store-apps,windows-8.1,C#,Windows Store Apps,Windows 8.1,我正在为Windows8.1开发一个绘图应用程序,需要一种简单的方法来显示用户正在触摸的区域。显然,当你用手指时,很难看到你在画什么。我需要做的是能够将目标元素渲染为位图,并将其显示给用户,以便用户可以看到他们正在绘制的位置。下面是我的答案。下面是代码。要使用它,只需在任何需要的地方以XAML(或编程方式)添加控件,并将Target属性设置为需要预览的控件 例如,在我的例子中,我是在WebView的顶部绘制的,所以我将WebView设置为目标,并将放大镜添加为WebView的同级 public

我正在为Windows8.1开发一个绘图应用程序,需要一种简单的方法来显示用户正在触摸的区域。显然,当你用手指时,很难看到你在画什么。我需要做的是能够将目标元素渲染为位图,并将其显示给用户,以便用户可以看到他们正在绘制的位置。下面是我的答案。

下面是代码。要使用它,只需在任何需要的地方以XAML(或编程方式)添加控件,并将Target属性设置为需要预览的控件

例如,在我的例子中,我是在WebView的顶部绘制的,所以我将WebView设置为目标,并将放大镜添加为WebView的同级

public class Magnifier : Grid
{
    public Magnifier() : base()
    {
        this.Canvas = new Canvas();

        Border border = new Border();
        border.HorizontalAlignment = HorizontalAlignment.Left;
        border.VerticalAlignment = VerticalAlignment.Top;
        border.Child = this.Canvas;
        border.BorderBrush = new SolidColorBrush(Colors.Red);
        border.BorderThickness = new Thickness(10);
        border.Background = new SolidColorBrush(Colors.Black);

        this.Children.Add(border);

        this.border = border;
    }

    public async Task<object> Update(int x, int y)
    {
        this.Visibility = Visibility.Visible;

        // changes the amount of magnification
        int magnification = 2;

        // render the preview of the target
        RenderTargetBitmap bitmap = new RenderTargetBitmap();
        await bitmap.RenderAsync(this.Target);

        this.Canvas.Width = this.PreviewWidth;
        this.Canvas.Height = this.PreviewHeight;

        double w = this.PreviewWidth / (2 * magnification);
        double h = this.PreviewHeight / (2 * magnification);

        ImageBrush brush = new ImageBrush();
        brush.ImageSource = bitmap;

        double scaleX = this.Target.ActualWidth / this.PreviewWidth;
        double scaleY = this.Target.ActualHeight / this.PreviewHeight;

        TransformGroup transform = new TransformGroup();
        TranslateTransform translate = new TranslateTransform();
        translate.X = -(x - w) / scaleX;
        translate.Y = -(y - h) / scaleY;
        transform.Children.Add(translate);

        ScaleTransform scale = new ScaleTransform();
        scale.ScaleX = scaleX * magnification;
        scale.ScaleY = scaleY * magnification;

        transform.Children.Add(scale);

        brush.Transform = transform;

        Rectangle rect = new Rectangle();
        rect.Width = this.PreviewWidth;
        rect.Height = this.PreviewHeight;
        rect.Fill = brush;
        this.Canvas.Children.Clear();
        this.Canvas.Children.Add(rect);

        Ellipse centerDot = new Ellipse();
        centerDot.Width = 6;
        centerDot.Height = 6;
        centerDot.Fill = new SolidColorBrush(Colors.Red);

        Canvas.SetLeft(centerDot, this.PreviewWidth / 2 - 3);
        Canvas.SetTop(centerDot, this.PreviewHeight / 2 - 3);

        this.Canvas.Children.Add(centerDot);

        return null;
    }

    public Canvas Canvas { get; set; }
    public int PreviewWidth { get; set; }
    public int PreviewHeight { get; set; }
    private Border border = null;
    public FrameworkElement Target { get; set; }
}
公共类放大镜:栅格
{
公共放大镜():base()
{
this.Canvas=newcanvas();
边框=新边框();
border.HorizontalAlignment=HorizontalAlignment.Left;
border.VerticalAlignment=VerticalAlignment.Top;
border.Child=this.Canvas;
border.BorderBrush=新的SolidColorBrush(Colors.Red);
border.BorderThickness=新厚度(10);
border.Background=新的SolidColorBrush(Colors.Black);
this.Children.Add(边框);
this.border=边界;
}
公共异步任务更新(int x,int y)
{
this.Visibility=Visibility.Visible;
//更改放大倍数
放大倍数=2;
//渲染目标的预览
RenderTargetBitmap位图=新建RenderTargetBitmap();
等待bitmap.RenderAsync(this.Target);
this.Canvas.Width=this.PreviewWidth;
this.Canvas.Height=this.PreviewHeight;
双w=此宽度/(2倍放大倍数);
双倍h=这一点。预视光/(2倍放大率);
ImageBrush笔刷=新的ImageBrush();
brush.ImageSource=位图;
double scaleX=this.Target.ActualWidth/this.PreviewWidth;
双刻度=this.Target.ActualHeight/this.PreviewHeight;
TransformGroup transform=新建TransformGroup();
TranslateTransform translate=新的TranslateTransform();
translate.X=-(X-w)/scaleX;
translate.Y=-(Y-h)/scaleY;
transform.Children.Add(translate);
ScaleTransform scale=新的ScaleTransform();
scale.ScaleX=ScaleX*放大倍数;
scale.ScaleY=ScaleY*放大倍数;
变换。子对象。添加(比例);
brush.Transform=Transform;
矩形rect=新矩形();
rect.Width=this.PreviewWidth;
rect.Height=this.PreviewHeight;
矩形填充=刷子;
this.Canvas.Children.Clear();
this.Canvas.Children.Add(rect);
椭圆中心点=新椭圆();
中心点宽度=6;
中心点高度=6;
centerDot.Fill=新的SolidColorBrush(Colors.Red);
Canvas.SetLeft(centerDot,this.PreviewWidth/2-3);
Canvas.SetTop(centerDot,this.PreviewHeight/2-3);
this.Canvas.Children.Add(centerDot);
返回null;
}
公共画布{get;set;}
public int PreviewWidth{get;set;}
公共int预览视图{get;set;}
私有边界=空;
公共框架元素目标{get;set;}
}