feat: add screenshot to image

dev_ocr_translate
DESKTOP-3BO4HSG\ksat 2 years ago
parent 1f1ecb1af6
commit 2afd51c783

@ -78,6 +78,9 @@
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="WpfScreenHelper, Version=2.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\WpfScreenHelper.2.1.0\lib\net40\WpfScreenHelper.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">

@ -2,6 +2,7 @@
using STranslate.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
@ -9,6 +10,8 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using System.Windows.Media;
namespace STranslate.Util
{
@ -236,5 +239,33 @@ namespace STranslate.Util
return strA_Z;
}
#endregion
#region Screenshot
public static ImageBrush BitmapToImageBrush(Bitmap bmp)
{
ImageBrush brush = new ImageBrush();
IntPtr hBitmap = bmp.GetHbitmap();
ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
brush.ImageSource = wpfBitmap;
//TODO: flush Memory
return brush;
}
/// <summary>
/// 清理内存
/// </summary>
public static void FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
Helper.NativeMethodHelper.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
#endregion
}
}

@ -34,6 +34,9 @@
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding MouseLeftUpCmd}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Closed">
<i:InvokeCommandAction Command="{Binding ClosedCmd}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Canvas x:Name="ScreenCanvas">

@ -22,9 +22,13 @@ namespace STranslate.View
{
public ScreenShotWindow()
{
DataContext = new ScreenShotVM(this);
var datas = (DataContext as ScreenShotVM).InitView1();
InitializeComponent();
DataContext = new ScreenShotVM(this);
(DataContext as ScreenShotVM).InitView2(datas);
}
}
}

@ -329,7 +329,8 @@ namespace STranslate.ViewModel
var ratio = (double)engStr.Length / text.Length;
//3. 判断英文字符个数占第一步所有字符个数比例,若超过一半则判定原字符串为英文字符串,否则为中文字符串
if (ratio > 0.5)
//TODO: 配置项
if (ratio > 0.8)
{
return new Tuple<string, string>(LanguageEnum.EN.GetDescription(), LanguageEnum.ZH.GetDescription());
}

@ -1,16 +1,22 @@
using STranslate.Helper;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace STranslate.ViewModel
{
public class ScreenShotVM : BaseVM
{
/// <summary>
/// reference https://github.com/NPCDW/WpfTool
/// </summary>
/// <param name="ui"></param>
public ScreenShotVM(Window ui)
{
_ScreenShotWin = ui;
@ -19,14 +25,136 @@ namespace STranslate.ViewModel
_ScreenShotWin.Close();
});
//鼠标移动
MouseMoveCmd = new RelayCommand((_) => true, (_) =>
{
if (MouseDown)
{
System.Windows.Point CurrentPoint = Mouse.GetPosition(_ScreenShotWin);
Rectangle = new Rect(StartPoint, CurrentPoint);
Canvas.SetLeft(_ScreenShotWin.FindName("LeftMask") as System.Windows.Shapes.Rectangle, 0);
Canvas.SetTop(_ScreenShotWin.FindName("LeftMask") as System.Windows.Shapes.Rectangle, 0);
(_ScreenShotWin.FindName("LeftMask") as System.Windows.Shapes.Rectangle).Width = Rectangle.X;
(_ScreenShotWin.FindName("LeftMask") as System.Windows.Shapes.Rectangle).Height = bitmap.Height;
Canvas.SetLeft(_ScreenShotWin.FindName("RightMask") as System.Windows.Shapes.Rectangle, Rectangle.Left + Rectangle.Width);
Canvas.SetTop(_ScreenShotWin.FindName("RightMask") as System.Windows.Shapes.Rectangle, 0);
(_ScreenShotWin.FindName("RightMask") as System.Windows.Shapes.Rectangle).Width = bitmap.Width - Rectangle.Left - Rectangle.Width;
(_ScreenShotWin.FindName("RightMask") as System.Windows.Shapes.Rectangle).Height = bitmap.Height;
Canvas.SetLeft(_ScreenShotWin.FindName("UpMask") as System.Windows.Shapes.Rectangle, Rectangle.Left);
Canvas.SetTop(_ScreenShotWin.FindName("UpMask") as System.Windows.Shapes.Rectangle, 0);
(_ScreenShotWin.FindName("UpMask") as System.Windows.Shapes.Rectangle).Width = Rectangle.Width;
(_ScreenShotWin.FindName("UpMask") as System.Windows.Shapes.Rectangle).Height = Rectangle.Y;
Canvas.SetLeft(_ScreenShotWin.FindName("DownMask") as System.Windows.Shapes.Rectangle, Rectangle.Left);
Canvas.SetTop(_ScreenShotWin.FindName("DownMask") as System.Windows.Shapes.Rectangle, Rectangle.Y + Rectangle.Height);
(_ScreenShotWin.FindName("DownMask") as System.Windows.Shapes.Rectangle).Width = Rectangle.Width;
(_ScreenShotWin.FindName("DownMask") as System.Windows.Shapes.Rectangle).Height = bitmap.Height - Rectangle.Height - Rectangle.Y;
}
});
//左键Down
MouseLeftDownCmd = new RelayCommand((_) => true, (_) =>
{
MouseDown = true;
StartPoint = Mouse.GetPosition(_ScreenShotWin);
});
//左键Up
MouseLeftUpCmd = new RelayCommand((_) => true, (_) =>
{
MouseDown = false;
int x = (int)(Rectangle.X * dpiScale);
int y = (int)(Rectangle.Y * dpiScale);
int width = (int)(Rectangle.Width * dpiScale);
int height = (int)(Rectangle.Height * dpiScale);
if (width <= 0 || height <= 0)
{
return;
}
Bitmap bmpOut = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(bitmap,
new Rectangle(0, 0, width, height),
new Rectangle(x, y, width, height),
GraphicsUnit.Pixel);
_ScreenShotWin.Close();
TestSaveBmp(bmpOut);
});
ClosedCmd = new RelayCommand((_) => true, (_) =>
{
Util.Util.FlushMemory();
});
}
private void TestSaveBmp(Bitmap bmp)
{
bmp.Save("D:\\a.png", System.Drawing.Imaging.ImageFormat.Bmp);
}
public Tuple<Rect, int, int, int, int> InitView1()
{
// 获取鼠标所在屏幕
System.Drawing.Point ms = System.Windows.Forms.Control.MousePosition;
Rect bounds = new Rect();
int x = 0, y = 0, width = 0, height = 0;
foreach (WpfScreenHelper.Screen screen in WpfScreenHelper.Screen.AllScreens)
{
bounds = screen.WpfBounds;
dpiScale = screen.ScaleFactor;
x = (int)(bounds.X * dpiScale);
y = (int)(bounds.Y * dpiScale);
width = (int)(bounds.Width * dpiScale);
height = (int)(bounds.Height * dpiScale);
if (x <= ms.X && ms.X < x + width && y <= ms.Y && ms.Y < y + height)
{
break;
}
}
return new Tuple<Rect, int, int, int, int>(bounds, x, y, width, height);
}
public void InitView2(Tuple<Rect, int, int, int, int> tuple)
{
// 设置窗体位置、大小实际宽高单位unit
_ScreenShotWin.Top = tuple.Item1.X;
_ScreenShotWin.Left = tuple.Item1.Y;
_ScreenShotWin.Width = tuple.Item1.Width;
_ScreenShotWin.Height = tuple.Item1.Height;
// 设置遮罩
Canvas.SetLeft(_ScreenShotWin, tuple.Item1.X);
Canvas.SetTop(_ScreenShotWin, tuple.Item1.Y);
(_ScreenShotWin.FindName("LeftMask") as System.Windows.Shapes.Rectangle).Width = tuple.Item1.Width;
(_ScreenShotWin.FindName("LeftMask") as System.Windows.Shapes.Rectangle).Height = tuple.Item1.Height;
// 设置窗体背景像素宽高单位px
bitmap = new Bitmap(tuple.Item4, tuple.Item5);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(tuple.Item2, tuple.Item3, 0, 0, new System.Drawing.Size(tuple.Item4, tuple.Item5), CopyPixelOperation.SourceCopy);
}
_ScreenShotWin.Background = Util.Util.BitmapToImageBrush(bitmap);
}
public ICommand EscCmd { get; private set; }
public ICommand MouseMoveCmd { get; private set; }
public ICommand MouseLeftDownCmd { get; private set; }
public ICommand MouseLeftUpCmd { get; private set; }
public ICommand ClosedCmd { get; private set; }
private Window _ScreenShotWin; //Window
private Rect Rectangle = new Rect(); //保存的矩形
private System.Windows.Point StartPoint; //鼠标按下的点
private bool MouseDown; //鼠标是否被按下
private Bitmap bitmap; // 截屏图片
private double dpiScale = 1;
private Window _ScreenShotWin;
}
}

@ -3,4 +3,5 @@
<package id="Hardcodet.NotifyIcon.Wpf" version="1.1.0" targetFramework="net48" />
<package id="Microsoft.Xaml.Behaviors.Wpf" version="1.1.39" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" />
<package id="WpfScreenHelper" version="2.1.0" targetFramework="net48" />
</packages>
Loading…
Cancel
Save