WP简单截图功能

前端代码如下:

<ScrollViewer>
<StackPanel Name="STPScreen">
<Grid Name="gdScreenPhoto">
<Image Height="460" Width="300" Name="imgPhoto" Source="/Images/test.png"></Image>
</Grid>
<Button Name="btnCut" Content="剪切"></Button>
<Image Name="imgDisplay"></Image>
<TextBlock Name="txtb"></TextBlock>
</StackPanel>
</ScrollViewer>

后台代码如下:

public ScreenshotDemo()
{
this.InitializeComponent();
btnCut.Click += BtnCut_Click;
SetPictureArea();
}

/// <summary>
/// 进行截图功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void BtnCut_Click(object sender, RoutedEventArgs e)
{
CilpImage();
var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(gdScreenPhoto);
imgDisplay.Source = bitmap;
RectangleGeometry geo = new RectangleGeometry();
//恢复原图
geo.Rect = new Rect(new Point(0, 0), new Point(imgPhoto.Width, imgPhoto.Height));
imgPhoto.Clip = geo;
}

/// <summary>
/// 给指定控件设置白色的透明长方形遮罩
/// </summary>
void SetPictureArea()
{

Rectangle rec = new Rectangle();
rec.Opacity = 0.5;
rec.Fill = new SolidColorBrush(Colors.White);
rec.Width = imgPhoto.Width;
rec.Height = imgPhoto.Height;
rec.Stroke = new SolidColorBrush(Colors.Red);
rec.StrokeThickness = 2;
rec.Margin = imgPhoto.Margin;
//添加滑动事件监听
rec.ManipulationMode = ManipulationModes.All;
rec.ManipulationDelta += Rec_ManipulationDelta;
gdScreenPhoto.Children.Add(rec);
gdScreenPhoto.Width = imgPhoto.Width;
gdScreenPhoto.Height = imgPhoto.Height;
}

/// <summary>
/// 截取图片区域,只是隐藏了原图的部分,并没有真正截图
/// </summary>
void CilpImage()
{
RectangleGeometry RecGeo = new RectangleGeometry();
Rectangle rec = (Rectangle)(from c in gdScreenPhoto.Children where c.Opacity == 0.5 select c).First();
GeneralTransform Gen = rec.TransformToVisual(gdScreenPhoto);
Point p = Gen.TransformPoint(new Point(0, 0));
Point p1 = Gen.TransformPoint(new Point(rec.Width, rec.Height));
RecGeo.Rect=new Rect(p,p1);
imgPhoto.Clip = RecGeo;
//隐藏截图的透明遮罩
rec.Visibility = Visibility.Collapsed;
}

/// <summary>
/// 手指滑动改变遮罩的长宽
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Rec_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
Rectangle rec = (Rectangle)sender;
//往右滑动宽度减少,左滑宽度增加
if (rec.Width >= (int)e.Delta.Translation.X)
{
rec.Width -= (int)e.Delta.Translation.X;
}
//往下滑动高度减少,上滑动高度增加
if (rec.Height >= (int)e.Delta.Translation.Y)
{
rec.Height -= (int)e.Delta.Translation.Y;
}
txtb.Text = e.Delta.Translation.X.ToString() + "," + e.Delta.Translation.Y.ToString();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值