簡體   English   中英

如何捕捉Photoshop的文檔區域

[英]how to capture the document area of Photoshop

我正在開發一個程序,我需要捕獲下圖中的紅色區域:

在此處輸入圖像描述

問題是無論Photoshop window 的大小如何,它都應該捕獲該部分,因此我無法對坐標進行硬編碼。

我還嘗試了檢查和 FlaUInspect,它們都可以在該區域和完整的 window 區域之間切換,還有automationId 並且名稱是動態的。

由於 ClassName 是 Photoshop,我嘗試了下面的 c# 代碼和 flaui 並且不起作用:

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 window的截圖然后裁剪。

這是最終代碼:

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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM