Save a WMF/EMF file from clipboard to file

Windows specific forum
Lebostein
Addict
Addict
Posts: 829
Joined: Fri Jun 11, 2004 7:07 am

Save a WMF/EMF file from clipboard to file

Post 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?
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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)
Egypt my love
Lebostein
Addict
Addict
Posts: 829
Joined: Fri Jun 11, 2004 7:07 am

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

Post 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_()
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post by jacdelad »

What about RASHADs code?

Aaaaaand you should add a check if OpenClipboard_() fails.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Lebostein
Addict
Addict
Posts: 829
Joined: Fri Jun 11, 2004 7:07 am

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

Post 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?)
Lebostein
Addict
Addict
Posts: 829
Joined: Fri Jun 11, 2004 7:07 am

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

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply