且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从剪贴板保存PngImage

更新时间:2022-03-18 14:54:34

Photoshop的剪贴板格式是可怕的。包含存储在剪贴板中的alpha通道的唯一非常有效的数据是...猜? ...指向阿尔法通道的内存进入Photoshop粘贴到位块....可怕。如果你复制的东西,然后重新启动photoshop,阿尔法是...丢失:)

Photoshop's clipboard format is horrible. The only pretty valid data that contains the alpha channel stored into the clipboard is... guess? ... a pointer to the alpha channel's memory into the "Photoshop Paste In Place" chunk.... HORRIBLE. If you copy something then restart photoshop, the alpha is... lost :)

但是,你可以很容易地理解剪贴板是否包含Photoshop图像。

However, you can easily understand if the clipboard contains Photoshop image.

向剪贴板询问它有哪些块。

Ask the Clipboard what chunks it have.

如果剪贴板有两个块,命名为emoPhotoshop粘贴到位 对象描述符,您可以99.9%确定系统上的Photoshop 正在运行,而剪贴板中包含参考到Photoshop数据。 (当Photoshop退出时,对象描述符块从剪贴板中删除,因此alpha将永远丢失)

If the clipboard have two chunks, named "Photoshop Paste In Place" AND "Object Descriptor", you can be 99.9% sure that Photoshop IS RUNNING on the system AND Clipboard contains reference to Photoshop data. (When Photoshop quits, the Object Descriptor chunk gets removed from the Clipboard, so the alpha is lost forever)

那么,你有两个选择:

选择1(不推荐):打开Photoshop的进程内存,并从指针中读取原始的32位图像数据...这完全是愚蠢的, em>和不安全,

Choice 1 (not recommended): Open Photoshop's Process Memory and read the raw 32-bit image data from the pointer... which is overall idiotic to do and unsecure, or

选择2(推荐):使用COM 来自Photoshop的图像数据。当然COM方法是***的方法。使您的程序生成并运行以下 VBS 脚本:

Choice 2 (recommended): Use COM to extract the image data from Photoshop. Of course, the COM method is the best way. Make your program generate and run the following VBS script:

On Error Resume Next
Set Ps = CreateObject("Photoshop.Application")
Set Shell = CreateObject("WScript.Shell")
Set FileSystem = CreateObject("Scripting.FileSystemObject") 

Dim PNGFileName
PNGFileName = Shell.CurrentDirectory & "\psClipboard.png"

If FileSystem.FileExists(PNGFileName) Then 
    FileSystem.DeleteFile PNGFileName
End If

Set Doc = Ps.Documents.Add(1,1,72,"psClipboard",,3)

Doc.Paste()
Doc.RevealAll()

If Err.Number = 0 Then 
    set PNGSaveOptions = CreateObject("Photoshop.PNGSaveOptions")
    doc.saveAs PNGFileName, PNGSaveOptions
End If

doc.Close()

在脚本的CurrentDirectory中,将生成一个名为 psClipboard.png 的文件。在程序中使用libPng或其他任何内容读取这个文件,就像它来自剪贴板一样。该脚本将删除 psClipboard.png ,然后将要求Photoshop。如果粘贴返回错误,脚本将停止,并且不会生成文件,在这种情况下,剪贴板不包含有效的Photoshop参考数据。

In the script's CurrentDirectory, a file names "psClipboard.png" will be generated. Read this file in your program using libPng or whatever, and treat is as if it was come from the Clipboard. This script will DELETE the psClipboard.png, then will ask Photoshop for it. In case a Paste returns Error, the script will cease and the file will not be generated, in which case, Clipboard didn't contained valid Photoshop reference data.