做个网站多少钱一年,wordpress 回复提醒,佛山市网站建站网站,wordpress批量下载外链图片WPF开发者QQ群此群已满340500857 #xff0c;请加新群458041663由于微信群人数太多入群请添加小编微信号yanjinhuawechat 或 W_Feng_aiQ 邀请入群需备注WPF开发者 接着上一篇#xff0c;兼容屏幕缩放问题。01—代码如下一、创建ScreenCut.xaml代码如下。ResourceDictiona… WPF开发者QQ群此群已满340500857 请加新群458041663 由于微信群人数太多入群请添加小编微信号 yanjinhuawechat 或 W_Feng_aiQ 邀请入群 需备注WPF开发者 接着上一篇兼容屏幕缩放问题。01—代码如下一、创建ScreenCut.xaml代码如下。ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:controlsclr-namespace:WPFDevelopers.ControlsResourceDictionary.MergedDictionariesResourceDictionary SourceBasic/ControlBasic.xaml/ResourceDictionary SourceButtonsStyles.xaml//ResourceDictionary.MergedDictionariesStyle x:KeyRectangleStyle TargetType{x:Type Rectangle}Setter PropertyFill Value{DynamicResource BlackSolidColorBrush}/Setter PropertyOpacity Value.5//StyleStyle TargetType{x:Type controls:ScreenCut} BasedOn{StaticResource ControlBasicStyle}Setter PropertyWindowState ValueMaximized/Setter PropertyWindowStyle ValueNone/Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type controls:ScreenCut}Canvas x:NamePART_CanvasWidth{Binding Source{x:Static SystemParameters.PrimaryScreenWidth}}Height{Binding Source{x:Static SystemParameters.PrimaryScreenHeight}}Rectangle x:NamePART_RectangleLeft Style{DynamicResource RectangleStyle}/Rectangle x:NamePART_RectangleTop Style{DynamicResource RectangleStyle}/Rectangle x:NamePART_RectangleRight Style{DynamicResource RectangleStyle}/Rectangle x:NamePART_RectangleBottom Style{DynamicResource RectangleStyle}/Border x:NamePART_Border BorderBrush{DynamicResource PrimaryNormalSolidColorBrush} BorderThickness1 BackgroundTransparent CursorSizeAll/WrapPanel x:NamePART_WrapPanel VisibilityHidden Panel.ZIndex99Height38 Background{DynamicResource WhiteSolidColorBrush}VerticalAlignmentCenterButton x:NamePART_ButtonSave Style{DynamicResource PathButton}ToolTip保存 Margin10,0,0,0Button.ContentPath Fill{DynamicResource InfoPressedSolidColorBrush} Width18 Height18 StretchFill Data{StaticResource PathSave}//Button.Content/ButtonButton x:NamePART_ButtonCancel Style{DynamicResource PathButton}ToolTip取消Button.ContentPath Fill{DynamicResource DangerPressedSolidColorBrush} Width14 Height14 StretchFill Data{StaticResource PathCancel}//Button.Content/ButtonButton x:NamePART_ButtonComplete Style{DynamicResource PathButton}ToolTip完成 Margin0,0,10,0Button.ContentPath Fill{DynamicResource SuccessPressedSolidColorBrush} Width20 Height15 StretchFill Data{StaticResource PathComplete}//Button.Content/Button/WrapPanel/Canvas/ControlTemplate/Setter.Value/Setter/Style
/ResourceDictionary二、创建ScreenCut.cs代码如下。using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFDevelopers.Helpers;namespace WPFDevelopers.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))]public class ScreenCut : Window{private const string CanvasTemplateName PART_Canvas;private const string RectangleLeftTemplateName PART_RectangleLeft;private const string RectangleTopTemplateName PART_RectangleTop;private const string RectangleRightTemplateName PART_RectangleRight;private const string RectangleBottomTemplateName PART_RectangleBottom;private const string BorderTemplateName PART_Border;private const string WrapPanelTemplateName PART_WrapPanel;private const string ButtonSaveTemplateName PART_ButtonSave;private const string ButtonCancelTemplateName PART_ButtonCancel;private const string ButtonCompleteTemplateName PART_ButtonComplete;private Canvas _canvas;private Rectangle _rectangleLeft, _rectangleTop, _rectangleRight, _rectangleBottom;private Border _border;private WrapPanel _wrapPanel;private Button _buttonSave, _buttonCancel, _buttonComplete;private Rect rect;private Point pointStart, pointEnd;private bool isMouseUp false;private Win32ApiHelper.DeskTopSize size;static ScreenCut(){DefaultStyleKeyProperty.OverrideMetadata(typeof(ScreenCut), new FrameworkPropertyMetadata(typeof(ScreenCut)));}public override void OnApplyTemplate(){base.OnApplyTemplate();_canvas GetTemplateChild(CanvasTemplateName) as Canvas;_rectangleLeft GetTemplateChild(RectangleLeftTemplateName) as Rectangle;_rectangleTop GetTemplateChild(RectangleTopTemplateName) as Rectangle;_rectangleRight GetTemplateChild(RectangleRightTemplateName) as Rectangle;_rectangleBottom GetTemplateChild(RectangleBottomTemplateName) as Rectangle;_border GetTemplateChild(BorderTemplateName) as Border;_wrapPanel GetTemplateChild(WrapPanelTemplateName) as WrapPanel;_buttonSave GetTemplateChild(ButtonSaveTemplateName) as Button;if (_buttonSave ! null)_buttonSave.Click _buttonSave_Click;_buttonCancel GetTemplateChild(ButtonCancelTemplateName) as Button;if (_buttonCancel ! null)_buttonCancel.Click _buttonCancel_Click;_buttonComplete GetTemplateChild(ButtonCompleteTemplateName) as Button;if (_buttonComplete ! null)_buttonComplete.Click _buttonComplete_Click;_canvas.Background new ImageBrush(Capture());_rectangleLeft.Width _canvas.Width;_rectangleLeft.Height _canvas.Height;}private void _buttonSave_Click(object sender, RoutedEventArgs e){SaveFileDialog dlg new SaveFileDialog();dlg.FileName $WPFDevelopers{DateTime.Now.ToString(yyyyMMddHHmmss)}.jpg;dlg.DefaultExt .jpg;dlg.Filter image file|*.jpg;if (dlg.ShowDialog() true){BitmapEncoder pngEncoder new PngBitmapEncoder();pngEncoder.Frames.Add(BitmapFrame.Create(CutBitmap()));using (var fs System.IO.File.OpenWrite(dlg.FileName)){pngEncoder.Save(fs);fs.Dispose();fs.Close();}}Close();}private void _buttonComplete_Click(object sender, RoutedEventArgs e){Clipboard.SetImage(CutBitmap());Close();}CroppedBitmap CutBitmap(){_border.Visibility Visibility.Collapsed;_rectangleLeft.Visibility Visibility.Collapsed;_rectangleTop.Visibility Visibility.Collapsed;_rectangleRight.Visibility Visibility.Collapsed;_rectangleBottom.Visibility Visibility.Collapsed;var renderTargetBitmap new RenderTargetBitmap((int)_canvas.Width,(int)_canvas.Height, 96d, 96d, System.Windows.Media.PixelFormats.Default);renderTargetBitmap.Render(_canvas);return new CroppedBitmap(renderTargetBitmap, new Int32Rect((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height));}private void _buttonCancel_Click(object sender, RoutedEventArgs e){Close();}protected override void OnPreviewKeyDown(KeyEventArgs e){if (e.Key Key.Escape)Close();}protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e){pointStart e.GetPosition(_canvas);if (!isMouseUp){_wrapPanel.Visibility Visibility.Hidden;pointEnd pointStart;rect new Rect(pointStart, pointEnd);}}protected override void OnPreviewMouseMove(MouseEventArgs e){if (e.LeftButton MouseButtonState.Pressed){var current e.GetPosition(_canvas);if (!isMouseUp){MoveAllRectangle(current);}else{if (current ! pointStart){var vector Point.Subtract(current, pointStart);var left Canvas.GetLeft(_border) vector.X;var top Canvas.GetTop(_border) vector.Y;if (left 0)left 0;if (top 0)top 0;if (left _border.Width _canvas.ActualWidth)left _canvas.ActualWidth - _border.ActualWidth;if (top _border.Height _canvas.ActualHeight)top _canvas.ActualHeight - _border.ActualHeight;pointStart current;Canvas.SetLeft(_border, left);Canvas.SetTop(_border, top);rect new Rect(new Point(left, top), new Point(left _border.Width, top _border.Height));_rectangleLeft.Width left 0 ? 0 : left _canvas.ActualWidth ? _canvas.ActualWidth : left;_rectangleLeft.Height _canvas.ActualHeight;Canvas.SetLeft(_rectangleTop, _rectangleLeft.Width);_rectangleTop.Height top 0 ? 0 : top _canvas.ActualHeight ? _canvas.ActualHeight : top;Canvas.SetLeft(_rectangleRight, left _border.Width);var wRight _canvas.ActualWidth - (_border.Width _rectangleLeft.Width);_rectangleRight.Width wRight 0 ? 0 : wRight;_rectangleRight.Height _canvas.ActualHeight;Canvas.SetLeft(_rectangleBottom, _rectangleLeft.Width);Canvas.SetTop(_rectangleBottom, top _border.Height);_rectangleBottom.Width _border.Width;var hBottom _canvas.ActualHeight - (top _border.Height);_rectangleBottom.Height hBottom 0 ? 0 : hBottom;}}}}protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e){_wrapPanel.Visibility Visibility.Visible;Canvas.SetLeft(_wrapPanel, rect.X rect.Width - _wrapPanel.ActualWidth);var y Canvas.GetTop(_border) _border.ActualHeight _wrapPanel.ActualHeight;if (y _canvas.ActualHeight)y Canvas.GetTop(_border) - _wrapPanel.ActualHeight - 4;elsey Canvas.GetTop(_border) _border.ActualHeight 4;Canvas.SetTop(_wrapPanel, y);isMouseUp true;}void MoveAllRectangle(Point current){pointEnd current;rect new Rect(pointStart, pointEnd);_rectangleLeft.Width rect.X;_rectangleLeft.Height _canvas.Height;Canvas.SetLeft(_rectangleTop, _rectangleLeft.Width);_rectangleTop.Width rect.Width;double h 0.0;if (current.Y pointStart.Y)h current.Y;elseh current.Y - rect.Height;_rectangleTop.Height h;Canvas.SetLeft(_rectangleRight, _rectangleLeft.Width rect.Width);_rectangleRight.Width _canvas.Width - (rect.Width _rectangleLeft.Width);_rectangleRight.Height _canvas.Height;Canvas.SetLeft(_rectangleBottom, _rectangleLeft.Width);Canvas.SetTop(_rectangleBottom, rect.Height _rectangleTop.Height);_rectangleBottom.Width rect.Width;_rectangleBottom.Height _canvas.Height - (rect.Height _rectangleTop.Height);_border.Height rect.Height;_border.Width rect.Width;Canvas.SetLeft(_border, rect.X);Canvas.SetTop(_border, rect.Y);}BitmapSource Capture(){IntPtr hBitmap;IntPtr hDC Win32ApiHelper.GetDC(Win32ApiHelper.GetDesktopWindow());IntPtr hMemDC Win32ApiHelper.CreateCompatibleDC(hDC);size.cx Win32ApiHelper.GetSystemMetrics(0);size.cy Win32ApiHelper.GetSystemMetrics(1);hBitmap Win32ApiHelper.CreateCompatibleBitmap(hDC, size.cx, size.cy);if (hBitmap ! IntPtr.Zero){IntPtr hOld (IntPtr)Win32ApiHelper.SelectObject(hMemDC, hBitmap);Win32ApiHelper.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, Win32ApiHelper.TernaryRasterOperations.SRCCOPY);Win32ApiHelper.SelectObject(hMemDC, hOld);Win32ApiHelper.DeleteDC(hMemDC);Win32ApiHelper.ReleaseDC(Win32ApiHelper.GetDesktopWindow(), hDC);var bsource Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());Win32ApiHelper.DeleteObject(hBitmap);GC.Collect();return bsource;}return null;}}
}三、新建WINAPI DLL Imports.cs代码如下。#region WINAPI DLL Imports[DllImport(gdi32.dll, ExactSpelling true, PreserveSig true, SetLastError true)]public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);[DllImport(gdi32.dll)]public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);[DllImport(gdi32.dll, SetLastError true)]public static extern IntPtr CreateCompatibleDC(IntPtr hdc);[DllImport(gdi32.dll)]public static extern bool DeleteObject(IntPtr hObject);[DllImport(gdi32.dll)]public static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);[DllImport(user32.dll)]public static extern IntPtr GetDC(IntPtr hWnd);[DllImport(user32.dll)]public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);[DllImport(gdi32.dll, EntryPoint DeleteDC)]public static extern IntPtr DeleteDC(IntPtr hDc);public const int SM_CXSCREEN 0;public const int SM_CYSCREEN 1;[DllImport(user32.dll, EntryPoint GetDesktopWindow)]public static extern IntPtr GetDesktopWindow();[DllImport(user32.dll, EntryPoint GetSystemMetrics)]public static extern int GetSystemMetrics(int abc);[DllImport(user32.dll, EntryPoint GetWindowDC)]public static extern IntPtr GetWindowDC(Int32 ptr);public struct DeskTopSize{public int cx;public int cy;}public enum TernaryRasterOperations : uint{/// summarydest source/summarySRCCOPY 0x00CC0020,/// summarydest source OR dest/summarySRCPAINT 0x00EE0086,/// summarydest source AND dest/summarySRCAND 0x008800C6,/// summarydest source XOR dest/summarySRCINVERT 0x00660046,/// summarydest source AND (NOT dest)/summarySRCERASE 0x00440328,/// summarydest (NOT source)/summaryNOTSRCCOPY 0x00330008,/// summarydest (NOT src) AND (NOT dest)/summaryNOTSRCERASE 0x001100A6,/// summarydest (source AND pattern)/summaryMERGECOPY 0x00C000CA,/// summarydest (NOT source) OR dest/summaryMERGEPAINT 0x00BB0226,/// summarydest pattern/summaryPATCOPY 0x00F00021,/// summarydest DPSnoo/summaryPATPAINT 0x00FB0A09,/// summarydest pattern XOR dest/summaryPATINVERT 0x005A0049,/// summarydest (NOT dest)/summaryDSTINVERT 0x00550009,/// summarydest BLACK/summaryBLACKNESS 0x00000042,/// summarydest WHITE/summaryWHITENESS 0x00FF0062}[DllImport(gdi32.dll)]public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);#endregion}四、新建ScreenCutExample.xaml.cs代码如下。var screenCut new ScreenCut();screenCut.ShowDialog();//screenCut.Show();02—效果预览鸣谢素材提供者 - 吴锋源码地址如下Githubhttps://github.com/WPFDevelopersOrgGiteehttps://gitee.com/WPFDevelopersOrgWPF开发者QQ群 340500857 | 458041663Githubhttps://github.com/WPFDevelopersOrg出处https://www.cnblogs.com/yanjinhua版权本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。转载请著名作者 出处 https://github.com/WPFDevelopersOrg扫一扫关注我们更多知识早知道点击阅读原文可跳转至源代码