版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第WPF仿微信实现截图功能的方法详解目录前言一、ScreenCut.cs代码如下二、ScreenCut.xaml代码如下三、ScreenCutExample.xaml代码如下每日一笑
肚子疼,去厕所排便,结果什么都没拉出来。看着自己坐在马桶上痛苦又努力却一无所获的样子,仿佛看到了自己平凡的一生。
前言
有小伙伴需要在软件反馈窗体增加截图功能需求,所以今天来实现一个仿微信的截图。
效果预览(更多效果请下载源码体验)
一、ScreenCut.cs代码如下
usingMicrosoft.Win32;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Drawing.Drawing2D;
usingSystem.Drawing.Text;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Shapes;
namespaceWPFDevelopers.Controls
[TemplatePart(Name=CanvasTemplateName,Type=typeof(Canvas))]
[TemplatePart(Name=RectangleLeftTemplateName,Type=typeof(Rectangle))]
[TemplatePart(Name=RectangleTopTemplateName,Type=typeof(Rectangle))]
[TemplatePart(Name=RectangleRightTemplateName,Type=typeof(Rectangle))]
[TemplatePart(Name=RectangleBottomTemplateName,Type=typeof(Rectangle))]
[TemplatePart(Name=BorderTemplateName,Type=typeof(Border))]
[TemplatePart(Name=WrapPanelTemplateName,Type=typeof(WrapPanel))]
[TemplatePart(Name=ButtonSaveTemplateName,Type=typeof(Button))]
[TemplatePart(Name=ButtonCancelTemplateName,Type=typeof(Button))]
[TemplatePart(Name=ButtonCompleteTemplateName,Type=typeof(Button))]
publicclassScreenCut:Window
privateconststringCanvasTemplateName="PART_Canvas";
privateconststringRectangleLeftTemplateName="PART_RectangleLeft";
privateconststringRectangleTopTemplateName="PART_RectangleTop";
privateconststringRectangleRightTemplateName="PART_RectangleRight";
privateconststringRectangleBottomTemplateName="PART_RectangleBottom";
privateconststringBorderTemplateName="PART_Border";
privateconststringWrapPanelTemplateName="PART_WrapPanel";
privateconststringButtonSaveTemplateName="PART_ButtonSave";
privateconststringButtonCancelTemplateName="PART_ButtonCancel";
privateconststringButtonCompleteTemplateName="PART_ButtonComplete";
privateCanvas_canvas;
privateRectangle_rectangleLeft,_rectangleTop,_rectangleRight,_rectangleBottom;
privateBorder_border;
privateWrapPanel_wrapPanel;
privateButton_buttonSave,_buttonCancel,_buttonComplete;
privateRectrect;
privatePointpointStart,pointEnd;
privateboolisMouseUp=false;
staticScreenCut()
DefaultStyleKeyProperty.OverrideMetadata(typeof(ScreenCut),newFrameworkPropertyMetadata(typeof(ScreenCut)));
publicoverridevoidOnApplyTemplate()
base.OnApplyTemplate();
_canvas=GetTemplateChild(CanvasTemplateName)asCanvas;
_rectangleLeft=GetTemplateChild(RectangleLeftTemplateName)asRectangle;
_rectangleTop=GetTemplateChild(RectangleTopTemplateName)asRectangle;
_rectangleRight=GetTemplateChild(RectangleRightTemplateName)asRectangle;
_rectangleBottom=GetTemplateChild(RectangleBottomTemplateName)asRectangle;
_border=GetTemplateChild(BorderTemplateName)asBorder;
_wrapPanel=GetTemplateChild(WrapPanelTemplateName)asWrapPanel;
_buttonSave=GetTemplateChild(ButtonSaveTemplateName)asButton;
if(_buttonSave!=null)
_buttonSave.Click+=_buttonSave_Click;
_buttonCancel=GetTemplateChild(ButtonCancelTemplateName)asButton;
if(_buttonCancel!=null)
_buttonCancel.Click+=_buttonCancel_Click;
_buttonComplete=GetTemplateChild(ButtonCompleteTemplateName)asButton;
if(_buttonComplete!=null)
_buttonComplete.Click+=_buttonComplete_Click;
this._canvas.Background=newImageBrush(ChangeBitmapToImageSource(CaptureScreen()));
_rectangleLeft.Width=_canvas.Width;
_rectangleLeft.Height=_canvas.Height;
privatevoid_buttonSave_Click(objectsender,RoutedEventArgse)
SaveFileDialogdlg=newSaveFileDialog();
dlg.FileName=$"WPFDevelopers{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg";
dlg.DefaultExt=".jpg";
dlg.Filter="imagefile|*.jpg";
if(dlg.ShowDialog()==true)
BitmapEncoderpngEncoder=newPngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(CutBitmap()));
using(varfs=System.IO.File.OpenWrite(dlg.FileName))
pngEncoder.Save(fs);
fs.Dispose();
fs.Close();
Close();
privatevoid_buttonComplete_Click(objectsender,RoutedEventArgse)
Clipboard.SetImage(CutBitmap());
Close();
CroppedBitmapCutBitmap()
varrenderTargetBitmap=newRenderTargetBitmap((int)_canvas.Width,
(int)_canvas.Height,96d,96d,System.Windows.Media.PixelFormats.Default);
renderTargetBitmap.Render(_canvas);
returnnewCroppedBitmap(renderTargetBitmap,newInt32Rect((int)rect.X,(int)rect.Y,(int)rect.Width,(int)rect.Height));
privatevoid_buttonCancel_Click(objectsender,RoutedEventArgse)
Close();
protectedoverridevoidOnPreviewKeyDown(KeyEventArgse)
if(e.Key==Key.Escape)
Close();
protectedoverridevoidOnPreviewMouseLeftButtonDown(MouseButtonEventArgse)
if(!isMouseUp)
_wrapPanel.Visibility=Visibility.Hidden;
pointStart=e.GetPosition(_canvas);
pointEnd=pointStart;
rect=newRect(pointStart,pointEnd);
protectedoverridevoidOnPreviewMouseMove(MouseEventArgse)
if(e.LeftButton==MouseButtonState.Pressed!isMouseUp)
varcurrent=e.GetPosition(_canvas);
MoveAllRectangle(current);
protectedoverridevoidOnPreviewMouseLeftButtonUp(MouseButtonEventArgse)
if(!isMouseUp)
_wrapPanel.Visibility=Visibility.Visible;
Canvas.SetLeft(this._wrapPanel,rect.X+rect.Width-this._wrapPanel.ActualWidth);
Canvas.SetTop(this._wrapPanel,rect.Y+rect.Height+4);
isMouseUp=true;
voidMoveAllRectangle(Pointcurrent)
pointEnd=current;
rect=newRect(pointStart,pointEnd);
this._rectangleLeft.Width=rect.X;
this._rectangleLeft.Height=_canvas.Height;
Canvas.SetLeft(this._rectangleTop,this._rectangleLeft.Width);
this._rectangleTop.Width=rect.Width;
doubleh=0.0;
if(current.YpointStart.Y)
h=current.Y;
else
h=current.Y-rect.Height;
this._rectangleTop.Height=h;
Canvas.SetLeft(this._rectangleRight,this._rectangleLeft.Width+rect.Width);
this._rectangleRight.Width=_canvas.Width-(rect.Width+this._rectangleLeft.Width);
this._rectangleRight.Height=_canvas.Height;
Canvas.SetLeft(this._rectangleBottom,this._rectangleLeft.Width);
Canvas.SetTop(this._rectangleBottom,rect.Height+this._rectangleTop.Height);
this._rectangleBottom.Width=rect.Width;
this._rectangleBottom.Height=_canvas.Height-(rect.Height+this._rectangleTop.Height);
this._border.Height=rect.Height;
this._border.Width=rect.Width;
Canvas.SetLeft(this._border,rect.X);
Canvas.SetTop(this._border,rect.Y);
System.Drawing.BitmapCaptureScreen()
varbmpCaptured=newSystem.Drawing.Bitmap((int)SystemParameters.PrimaryScreenWidth,(int)SystemParameters.PrimaryScreenHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using(System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(bmpCaptured))
g.SmoothingMode=SmoothingMode.AntiAlias;
g.CompositingQuality=CompositingQuality.HighQuality;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.TextRenderingHint=TextRenderingHint.ClearTypeGridFit;
g.PixelOffsetMode=PixelOffsetMode.HighQuality;
g.CopyFromScreen(0,0,0,0,bmpCaptured.Size,System.Drawing.CopyPixelOperation.SourceCopy);
returnbmpCaptured;
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
publicstaticexternboolDeleteObject(IntPtrhObject);
ImageSourceChangeBitmapToImageSource(System.Drawing.Bitmapbitmap)
IntPtrhBitmap=bitmap.GetHbitmap();
ImageSourcewpfBitmap=System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
if(!DeleteObject(hBitmap))
thrownewSystem.ComponentModel.Win32Exception();
returnwpfBitmap;
}
二、ScreenCut.xaml代码如下
ResourceDictionaryxmlns="/winfx/2006/xaml/presentation"
xmlns:x="/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPFDevelopers.Controls"
ResourceDictionary.MergedDictionaries
ResourceDictionarySource="Basic/ControlBasic.xaml"/
ResourceDictionarySource="../Styles/Styles.Buttons.xaml"/
/ResourceDictionary.MergedDictionaries
Stylex:Key="RectangleStyle"TargetType="{x:TypeRectangle}"
SetterProperty="Fill"Value="{StaticResourceBlackSolidColorBrush}"/
SetterProperty="Opacity"Value=".5"/
/Style
StyleTargetType="{x:Typecontrols:ScreenCut}"BasedOn="{StaticResourceControlBasicStyle}"
SetterProperty="WindowState"Value="Maximized"/
SetterProperty="WindowStyle"Value="None"/
SetterProperty="Template"
Setter.Value
ControlTemplateTargetType="{x:Typecontrols:ScreenCut}"
Canvasx:Name="PART_Canvas"
Width="{BindingSource={x:StaticSystemParameters.PrimaryScreenWidth}}"
Height="{BindingSource={x:StaticSystemParameters.PrimaryScreenHeight}}"
Rectanglex:Name="PART_RectangleLeft"/
Rectanglex:Name="PART_RectangleTop"/
Rectanglex:Name="PART_RectangleRight"/
Rectanglex:Name="PART_RectangleBottom"/
Borderx:Name="PART_Border"BorderBrush="{StaticResourceSuccessPressedSolidColorBrush}"
BorderThickness="1"/
WrapPanelx:Name="PART_WrapPanel"
Visibility="Hidden"Panel.ZIndex="99"
Height="38"Back
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 老旧小区公共设施维修管理策略
- 景观照明布设施工技术方案
- 老旧小区建筑材料环保选择
- 混凝土基础施工工艺流程优化方案
- 公司知识管理体系建设方案
- 工业尾气二氧化碳综合处理利用项目环境影响报告书
- 2024-2025学年反射疗法师3级每日一练试卷附完整答案详解(必刷)
- 公共设施管理与维护知识竞赛考试
- 储能项目成本控制策略
- 城市老旧供水管网安全与效能提升改造工程施工方案
- 2026北京航空航天大学 机械工程及自动化学院聘用编专职事务助理、F岗招聘1人考试备考题库及答案解析
- 网络安全培训教材与教学大纲(标准版)
- 医学人文培训课件
- 学堂在线 雨课堂 学堂云 科研伦理与学术规范 期末考试答案
- 2026年商丘学院单招(计算机)测试模拟题库附答案
- 机场防鸟撞培训大纲
- 医院培训课件:《中医护理文书书写规范》
- 涉外侵权课件
- 国企合规风控培训课件
- 肿瘤科医疗质量与安全管理
- 2025年体育彩票考试题目及答案
评论
0/150
提交评论