autohotke Save any bitmaps on the clipboard to file

 

; Save any bitmaps on the clipboard to file

; isolated by impecco@gmail.com from Sean's script "ScreenCapture.ahk" posted on

;    http://www.autohotkey.com/forum/topic18146.html

; needs GDI+ libaray "gdiplus.dll" which can be obtained from

;    http://www.microsoft.com/downloads/details.aspx?FamilyID=6a63ab9c-df12-4d41-933c-be590feaa05a&displaylang=en

 

SaveClipBoardToJPG()   ; example

SaveClipBoardToBMP()   ; example

 

SaveClipBoardToBMP() {

   Convert("", "clip.bmp")

}

 

SaveClipBoardToJPG() {

   Convert("", "clip.jpg")

}

 

Convert(sFileFr = "", sFileTo = "")

{

   If   sFileTo  =

      sFileTo := A_ScriptDir . "/screen.bmp"

   SplitPath, sFileTo, , sDirTo, sExtTo, sNameTo

 

   If Not   hGdiPlus := DllCall("LoadLibrary", "str", "gdiplus.dll")

      Return   sFileFr+0 ? SaveHBITMAPToFile(sFileFr, sDirTo . "/" . sNameTo . ".bmp") : ""

   VarSetCapacity(si, 16, 0), si := Chr(1)

   DllCall("gdiplus/GdiplusStartup", "UintP", pToken, "Uint", &si, "Uint", 0)

 

   If   !sFileFr

   {

      DllCall("OpenClipboard", "Uint", 0)

      If    DllCall("IsClipboardFormatAvailable", "Uint", 2) && (hBM:=DllCall("GetClipboardData", "Uint", 2)) {

         DllCall("gdiplus/GdipCreateBitmapFromHBITMAP", "Uint", hBM, "Uint", 0, "UintP", pImage)

      }

      DllCall("CloseClipboard")

   }

   Else If   (sFileFr Is Integer) {

      DllCall("gdiplus/GdipCreateBitmapFromHBITMAP", "Uint", sFileFr, "Uint", 0, "UintP", pImage)

   }

   Else {

      DllCall("gdiplus/GdipLoadImageFromFile", "Uint", Unicode4Ansi(wFileFr,sFileFr), "UintP", pImage)

   }

 

   DllCall("gdiplus/GdipGetImageEncodersSize", "UintP", nCount, "UintP", nSize)

   VarSetCapacity(ci, nSize)

   DllCall("gdiplus/GdipGetImageEncoders", "Uint", nCount, "Uint", nSize, "Uint", &ci)

   Loop,   %nCount%

   {

      If   !InStr(Ansi4Unicode(NumGet(ci, 76 * (A_Index - 1) + 44)), "." . sExtTo)

         Continue

      pCodec := &ci + 76 * (A_Index - 1)

         Break

   }

 

   If   pImage

      pCodec   ? DllCall("gdiplus/GdipSaveImageToFile", "Uint", pImage, "Uint", Unicode4Ansi(wFileTo,sFileTo), "Uint", pCodec, "Uint", 0) : DllCall("gdiplus/GdipCreateHBITMAPFromBitmap", "Uint", pImage, "UintP", hBitmap, "Uint", 0) . SetClipboardData(hBitmap), DllCall("gdiplus/GdipDisposeImage", "Uint", pImage)

 

   DllCall("gdiplus/GdiplusShutdown" , "Uint", pToken)

   DllCall("FreeLibrary", "Uint", hGdiPlus)

}

 

SaveHBITMAPToFile(hBitmap, sFile)

{

   DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi)

   hFile:=   DllCall("CreateFile", "Uint", &sFile, "Uint", 0x40000000, "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 0, "Uint", 0)

   DllCall("WriteFile", "Uint", hFile, "int64P", 0x4D42|14+40+NumGet(oi,44)<<16, "Uint", 6, "UintP", 0, "Uint", 0)

   DllCall("WriteFile", "Uint", hFile, "int64P", 54<<32, "Uint", 8, "UintP", 0, "Uint", 0)

   DllCall("WriteFile", "Uint", hFile, "Uint", &oi+24, "Uint", 40, "UintP", 0, "Uint", 0)

   DllCall("WriteFile", "Uint", hFile, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44), "UintP", 0, "Uint", 0)

   DllCall("CloseHandle", "Uint", hFile)

}

 

Unicode4Ansi(ByRef wString, sString)

{

   nSize := DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", 0, "int", 0)

   VarSetCapacity(wString, nSize * 2)

   DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", &wString, "int", nSize)

   Return   &wString

}

 

Ansi4Unicode(pString)

{

   nSize := DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "Uint", 0, "int",  0, "Uint", 0, "Uint", 0)

   VarSetCapacity(sString, nSize)

   DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "str", sString, "int", nSize, "Uint", 0, "Uint", 0)

   Return   sString

}

 

SetClipboardData(hBitmap)

{

   DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi)

   hDIB :=   DllCall("GlobalAlloc", "Uint", 2, "Uint", 40+NumGet(oi,44))

   pDIB :=   DllCall("GlobalLock", "Uint", hDIB)

   DllCall("RtlMoveMemory", "Uint", pDIB, "Uint", &oi+24, "Uint", 40)

   DllCall("RtlMoveMemory", "Uint", pDIB+40, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44))

   DllCall("GlobalUnlock", "Uint",hDIB)

   DllCall("DeleteObject", "Uint", hBitmap)

   DllCall("OpenClipboard", "Uint", 0)

   DllCall("EmptyClipboard")

   DllCall("SetClipboardData", "Uint", 8, "Uint", hDIB)

   DllCall("CloseClipboard")

}

 

[/size][/color]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值