我正在开发一个程序,我需要捕获下图中的红色区域:
问题是无论photoshop窗口的大小如何,它都应捕获该部分,因此我无法对坐标进行硬编码。
我还尝试了inspect和FlaUInspect,它们都在该区域和整个窗口区域之间切换,也有toautomationId,名称是动态的。
由于ClassName是Photoshop,因此我尝试了以下flaui的C#代码,但无法正常工作:
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using FlaUI.UIA3;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Process[] AllProcesslist = Process.GetProcesses();
foreach (Process Proc in AllProcesslist)
{
if (!String.IsNullOrEmpty(Proc.MainWindowTitle) && Proc.MainModule.FileName == @"D:\photoshop\Adobe Photoshop CC 2015\Photoshop.exe")
{
Console.WriteLine("Window Found!");
var app = new FlaUI.Core.Application(Proc);
using (var automation = new UIA3Automation())
{
var window = app.GetMainWindow(automation);
Bitmap image = window.FindFirstDescendant(cf => cf.ByClassName("Photoshop")).Capture();
image.Save("sample.jpg", ImageFormat.Jpeg);
}
}
}
Console.Read();
}
}
}
我的程序是用 Electron 的,但是如果您知道如何用任何编程语言来实现,我都可以在 Electron 程序中轻松实现。
最佳答案
没有用于photoshop的ui自动化框架,flaui和inspect.exe无法与Photoshop一起使用,因此我使用了捕获方法来获取Photoshop窗口的屏幕截图,然后进行裁剪。
这是最终的代码:
using FlaUI.UIA3;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Control
{
class Photoshop
{
public static int processId;
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int processId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public Point ptMinPosition;
public Point ptMaxPosition;
public Rectangle rcNormalPosition;
}
// take a screenshot of the document
public async Task<object> screen(int handle)
{
Bitmap final = null;
GetWindowThreadProcessId((IntPtr)handle, out processId);
Process Proc = Process.GetProcessById(processId);
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
GetWindowPlacement((IntPtr)Proc.MainWindowHandle, ref placement);
var app = new FlaUI.Core.Application(Proc);
var automation = new UIA3Automation();
Bitmap capture = app.GetMainWindow(automation).Capture();
if (placement.showCmd == 1) // 1- normal, 3- maximize
{
final = documentArea(capture);
}
else
{
Bitmap crop = cropImage(capture, new Rectangle(8, 8, capture.Width - 16, capture.Height - 16));
final = documentArea(crop);
}
return ImgtoBase64(final);
}
public static Bitmap documentArea(Bitmap b)
{
int x = 45;
int y = 87;
Rectangle area = new Rectangle(x, y, b.Width - x, b.Height - y);
return cropImage(b, area);
}
public static Bitmap cropImage(Bitmap b, Rectangle r)
{
Bitmap nb = new Bitmap(r.Width, r.Height);
using (Graphics g = Graphics.FromImage(nb))
{
g.DrawImage(b, -r.X, -r.Y);
return nb;
}
}
public static string ImgtoBase64(Bitmap img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Png);
byte[] byteImage = ms.ToArray();
return Convert.ToBase64String(byteImage);
}
}
}
关于c# - 如何捕获Photoshop的文档区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65286374/