No announcement yet.

Clipboard to bmp file ?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Clipboard to bmp file ?

    How to direct transfer a image from Clipboard
    to .bmp file ?

  • #2
    Get a handle to the bmp with the Windows API call "GetClipboardData," then use any of the methods posted here to save as a file.

    GetClipboardData ==> lots of examples here, but that's pretty easy if you read the documentation for thar function in your Windows' API reference.

    I know there are several examples here of saving a BMP file, both PB/Win 8+ (with GRAPHICS commands) and from prior versions (the long way).

    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]


    • #3
            '  *******************
                  '  code to create the temp file to store the bmp into
                  '  obtain a temporary bmp file holder (not currently working)
                  dim szprefix      as asciiz * 04       : szprefix = "bmp"
                  dim sztempfile    as asciiz * %max_path
                  if gettemporaryfilename(szprefix, sztempfile) = 00 then
                     '  load the temporary file name
                     s_filename_bmpthumbnail = sztempfile
                     msgbox "temporyary bitmap file could not be created", %mb_iconerror, $s_headercode
                     exit function
                  end if
                  '  now paste the buffer to the temporary file that was created
                  local l_errordetection_clipboardpast   as long
                  call bitmap_clipboardpaste(s_filename_bmpthumbnail, l_errordetection_clipboardpast)
            '  *******************
      sub bitmap_clipboardpaste(s_filename as string, l_errordetection as long)
         '  sub to take the clip board and paste to the temp file created above
         '  derived from [url=""][/url] 
         local di&
         local hbm&
         local mem&
         local sz&
         local path$
         local fmem&
         local bi                      as bitmapinfo
         local bih                     as bitmapinfoheader
         local bfh                     as bitmapfileheader
         local bm                      as bitmap
         local u_freefileunitnumber    as long
         local hwnd&
         local hdc&
         '  modified per the above post
         hwnd  = getdesktopwindow(    )
         hdc   = getdc           (hwnd)
         di& = openclipboard(hwnd&)
         hbm& = getclipboarddata(%cf_bitmap)
         '  reset error detection
         reset l_errordetection
         if (hbm&= 0) then
            di& = closeclipboard()
           'msgbox "error pasting bitmap",,"paste error"
            l_errordetection = -1
            exit sub
         end if
         di&   = getobject(hbm&,len(bm),bm)
         if (di& = 00) then
           'msgbox "error obtaining bitmap structure", %mb_iconerror, "paste error bitmap_clipboardpaste()"
            l_errordetection = -1
         end if
         sz&                     = bm.bmheight*bm.bmwidthbytes
         bi.bmiheader.bisize     = sizeof(bih)
         bi.bmiheader.bibitcount = bm.bmbitspixel
         bi.bmiheader.biwidth    = bm.bmwidth
         bi.bmiheader.biheight   = bm.bmheight
         bi.bmiheader.biplanes   = 01
         di& = closeclipboard()
         if (di&= 00) then
           'msgbox "error obtaining bitmap information", %mb_iconerror, "paste error bitmap_clipboardpaste()"
            l_errordetection = -1
         end if
         mem&  = globalalloc(%gmem_fixed,sz&)
         di&   = getdibits(hdc&, hbm&, 00, bm.bmheight, byval mem&, bi, %dib_rgb_colors)
         bfh.bftype        = cvwrd("bm")
         bfh.bfsize        = sizeof(bfh) + sizeof(bi) + sz&
         bfh.bfoffbits     = sizeof(bfh) + sizeof(bi)
         fmem&             = globalalloc(%gmem_fixed,bfh.bfsize)
         copymemory fmem&,varptr(bfh),sizeof(bfh)
         copymemory fmem&+sizeof(bfh),varptr(bi),len(bi)
         copymemory fmem&+sizeof(bfh)+sizeof(bi),mem&,sz&
         u_freefileunitnumber = freefile
         open s_filename$ for output as #u_freefileunitnumber
         print #u_freefileunitnumber, peek$(fmem&, bfh.bfsize);
         close #u_freefileunitnumber
         di&   = globalfree(mem& )
         di&   = globalfree(fmem&)
      end sub
      function gettemporaryfilename(szprefix as asciiz * 4, sztempfile as asciiz * %max_path) as long
         '  derives from [url=""][/url] 
         '  returns: 00     = success, the temp file name is in sztempfile (fully qualified)
         '           other  = system error number
         local sztempprefix   as asciiz * 4     '  3 + null
         local sztemppath     as asciiz * %max_path
         local stat           as long
         sztempprefix   = szprefix
         stat           = gettemppath(sizeof(sztemppath), sztemppath)
         if istrue stat then        '  gettemppathsucceeded
             stat = gettempfilename (sztemppath, sztempprefix, 0&, sztempfile)
         end if
         if istrue stat then        '  function succeeded, return zero
            function = 00
            function = getlasterror  ' gettempfilename returns non-zero on success
         end if
      end function


      • #4
        At W95 the bmp file presents all area dark.
        Any ideas ?

        At W98SE works, but a original black&white bmp
        with 2 KB, is saved with more than 50 KB . Why ?

        [This message has been edited by Paulo Ferreira (edited February 05, 2006).]


        • #5
          sorry, I'm not much help here.

          I use this chuck of code for a digitizer that I wrote. Most of
          my tooling is applied in XP which seems to have no issues.