Page 1 of 1

Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 4:40 pm
by Lebostein
Inside Excel I copy some things as vector graphics into the clipboard. Now I want to save the data in the clipboard with PB. I am sure there is an enhanced metafile in the clipboard, because I see the pointer in the debug-Window:

Code: Select all

OpenWindow(0, 0, 0, 0, 0, "EMFCLIP", #PB_Window_Invisible)
OpenClipboard_(WindowID(0))                    
If IsClipboardFormatAvailable_(#CF_ENHMETAFILE)
    *pointer = GetClipboardData_(#CF_ENHMETAFILE)
    Debug *pointer
    ; --> write to file
EndIf
CloseClipboard_()
But how I can save the data to a file?

Re: Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 5:46 pm
by jacdelad
Following this article: https://gist.github.com/istupakov/2b75c513cb6bfc61d44b

You need the API function "GetEnhMetaFileBits". I can't program right now, but the code in the link should be quickly adaptable.

Re: Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 5:58 pm
by RASHAD
Hi
The image will be saved in TEMP folder or You can change the path
Please report back

Code: Select all

OpenWindow(0, 0, 0, 0, 0, "EMFCLIP", #PB_Window_Invisible)
OpenClipboard_(0)                   
If IsClipboardFormatAvailable_(#CF_ENHMETAFILE)   
  *pointer = GetClipboardData_(#CF_ENHMETAFILE)
  Debug *pointer
  size = GetEnhMetaFileBits_(*pointer, 0, 0)   
  h_size = GetEnhMetaFileHeader_(*pointer, 0, 0)
  
  header.ENHMETAHEADER   
  GetEnhMetaFileHeader_(*pointer, h_size, @header)
  CreateImage(10, header\rclbounds\right, header\rclbounds\bottom)
  hdc = StartDrawing(ImageOutput(10))
        r.RECT
        r\left = 0
        r\top = 0
        r\right = header\rclbounds\right
        r\bottom = header\rclbounds\bottom
        PlayEnhMetaFile_(hdc, *pointer, r)
    StopDrawing()
    DeleteEnhMetaFile_(*pointer)
EndIf
CloseClipboard_()

SaveImage(10,GetTemporaryDirectory()+"test.bmp",#PB_ImagePlugin_BMP,0,32)

Re: Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 7:12 pm
by Lebostein
jacdelad wrote: Thu Jul 13, 2023 5:46 pm Following this article: https://gist.github.com/istupakov/2b75c513cb6bfc61d44b

You need the API function "GetEnhMetaFileBits". I can't program right now, but the code in the link should be quickly adaptable.
Thanks!!!! Works perfect!

Code: Select all

windowID = OpenWindow(#PB_Any, 0, 0, 0, 0, "EMFCLIP", #PB_Window_Invisible)
OpenClipboard_(WindowID(windowID))                    
If IsClipboardFormatAvailable_(#CF_ENHMETAFILE)
    *pointer = GetClipboardData_(#CF_ENHMETAFILE)
    size = GetEnhMetaFileBits_(*pointer, 0, nullptr)
    *memory = AllocateMemory(size)
    GetEnhMetaFileBits_(*pointer, size, *memory)
    fileID = OpenFile(#PB_Any, "c:/temp/test.emf")
    WriteData(fileID, *memory, size)
    CloseFile(fileID)
    FreeMemory(*memory)
EndIf
CloseClipboard_()

Re: Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 7:19 pm
by jacdelad
What about RASHADs code?

Aaaaaand you should add a check if OpenClipboard_() fails.

Re: Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 7:41 pm
by Lebostein
jacdelad wrote: Thu Jul 13, 2023 7:19 pm What about RASHADs code?
Not what I need. I don't need the pixel image, I need the original EMF data...
jacdelad wrote: Thu Jul 13, 2023 7:19 pm Aaaaaand you should add a check if OpenClipboard_() fails.
OK (but why should this fail?)

Re: Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 7:47 pm
by Lebostein
OK, now here the final code. Program parameter is the filename to export:

Code: Select all

EnableExplicit
Define size, fileID, *pointer, *memory, filename.s
filename = ProgramParameter(0)
If filename = ""
  filename = GetUserDirectory(#PB_Directory_Downloads) + "clipboard2emf.emf"
EndIf
If OpenClipboard_(0)                  
  If IsClipboardFormatAvailable_(#CF_ENHMETAFILE)
    *pointer = GetClipboardData_(#CF_ENHMETAFILE)
    If *pointer
      size = GetEnhMetaFileBits_(*pointer, 0, 0)
      If size
        *memory = AllocateMemory(size)
        If *memory
          GetEnhMetaFileBits_(*pointer, size, *memory)
          fileID = OpenFile(#PB_Any, filename)
          If fileID
            WriteData(fileID, *memory, size)
            CloseFile(fileID)
          EndIf
          FreeMemory(*memory)
        EndIf
      EndIf
    EndIf
  EndIf
  CloseClipboard_()
EndIf

Re: Save a WMF/EMF file from clipboard to file

Posted: Thu Jul 13, 2023 8:21 pm
by jacdelad
Lebostein wrote: Thu Jul 13, 2023 7:41 pm OK (but why should this fail?)
Hopefully it doesn't, but there's always a chance. And if it fails, your program will certainly crash.