How to save PngImage from clipboard

后端 未结 3 1312
日久生厌
日久生厌 2021-02-10 05:25

How can i save the pngimage to file copied form AdobeFirewoks(Clipboard) or Photoshop without losing the transparency.

i am using delphi2009.

thank you in advan

3条回答
  •  广开言路
    2021-02-10 06:16

    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 :)

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

    Ask the Clipboard what chunks it have.

    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)

    So then, you have two choices:

    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

    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()
    

    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.

提交回复
热议问题