// 固定区域截图 private Bitmap FixedScreen() { if (screenRect.Width <= 0 || screenRect.Height <= 0) { throw new Exception("请设置固定截图翻译坐标!"); } IntPtr hwnd = Api.FindWindowHandle(windowName, windowClass); Api.ShowWindowWait(hwnd, 500); Api.POINT p = new Api.POINT(screenRect.X, screenRect.Y); Api.ClientToScreen(hwnd, ref p); return(SpecifyScreenshot.Screenshot(p.X, p.Y, screenRect.Width, screenRect.Height)); }
// 绘制选择的框框 private void DrawSelectionBox(Graphics g) { Font font = new Font("微软雅黑", 12f); g.DrawImage(this.screenImage, this.selectedArea, this.selectedArea, GraphicsUnit.Pixel); g.DrawRectangle(Pens.DodgerBlue, this.selectedArea.Left, this.selectedArea.Top, this.selectedArea.Width - 1, this.selectedArea.Height - 1); string showText; if (WindowHandle != IntPtr.Zero) { Api.POINT p = new Api.POINT(); p.X = this.selectedArea.Left; p.Y = this.selectedArea.Top; // 屏幕坐标转为客户端窗口坐标 Api.ScreenToClient(WindowHandle, ref p); showText = string.Format($"按住鼠标左键选取要截取的区域\n按鼠标右键或ESC取消\nX相对坐标:{p.X} Y相对坐标:{p.Y} 宽:{this.selectedArea.Width} 高:{this.selectedArea.Height}"); } else { showText = string.Format($"按住鼠标左键选取要截取的区域\n按鼠标右键或ESC取消\nX坐标:{this.selectedArea.Left} Y坐标:{this.selectedArea.Top} 宽:{this.selectedArea.Width} 高:{this.selectedArea.Height}"); } // 测量显示字体需要的尺寸 Size sizeRequired = g.MeasureString(showText, font).ToSize(); // 显示字体的坐标 Point displayPos = new Point(this.selectedArea.Left, this.selectedArea.Top - sizeRequired.Height - 5); if (displayPos.Y < 0) { displayPos.Y = this.selectedArea.Top + 5; } if (displayPos.X + sizeRequired.Width > this.Width) { displayPos.X = this.Width - sizeRequired.Width; } if (displayPos.X == 0) { displayPos.X = 5; } // 在指定坐标绘制字体 using (SolidBrush Brush = new SolidBrush(Color.FromArgb(125, 0, 0, 0))) { // 在矩形内部填充半透明黑色 g.FillRectangle(Brush, new Rectangle(displayPos, sizeRequired)); g.DrawString(showText, font, Brushes.Orange, displayPos); } }
// 固定区域截图 private Bitmap FixedScreen() { if (screenRect.Width <= 0 || screenRect.Height <= 0) { throw new Exception("请设置固定截图翻译坐标!"); } IntPtr hwnd = FindWindowHandle(); Api.POINT p = new Api.POINT(); p.X = screenRect.X; p.Y = screenRect.Y; Api.ClientToScreen(hwnd, ref p); return(Screenshot(p.X, p.Y, screenRect.Width, screenRect.Height)); }
// 设置固定翻译坐标 private void button_SetPosition_Click(object sender, EventArgs e) { FrmScreenShot shot = new FrmScreenShot(); try { IntPtr hwnd = FindAndActiveWindow(); shot.WindowHandle = hwnd; shot.CopyScreen(); // 显示截图窗口 DialogResult result = shot.ShowDialog(); if (result == DialogResult.Cancel) { throw new Exception("用户取消截图!"); } Api.POINT p = new Api.POINT(); p.X = shot.StartPos.X; p.Y = shot.StartPos.Y; // 屏幕坐标转为客户端窗口坐标 Api.ScreenToClient(hwnd, ref p); // 保存截图坐标高宽 screenRect = new Rectangle(p.X, p.Y, shot.SelectedArea.Width, shot.SelectedArea.Height); if (shot.SelectedArea.Width > 0 && shot.SelectedArea.Height > 0) { MessageBox.Show("设置成功!\n请点击“保存配置”按钮。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { throw new Exception("设置失败,请重试!"); } } catch (Exception ex) { MessageBox.Show("设置固定翻译坐标失败!\n原因:" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { MinimizeWindow(false); if (shot != null && !shot.IsDisposed) { shot.Dispose(); } } }