how to convert pBitmap to base64 Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

how to convert pBitmap to base64

04 Nov 2020, 21:05

I did the code bellow that convert a image file (pbitmap2) to base64 format

However, i would like to ask if someone knows how to use a pBitmap created from GDI like:

Code: Select all

pbitmap	    := Gdip_BitmapFromScreen(0)
To base64 format.


code:

Code: Select all

	
	clipboard := ""
	gdipToken := Gdip_Startup()
	
	t1:=A_TickCount
	
	pbitmap	    := Gdip_BitmapFromScreen(0)
	pbitmap2 = C:\Users\Downloads\y.png
	
	; -------------------------------------------------------------------------------------------------------------------
	; Read the image file
	; -------------------------------------------------------------------------------------------------------------------
	File := FileOpen(pbitmap2, "r")
	BinLen := File.Length
	File.RawRead(Bin, BinLen)
	File.Close()
	; -------------------------------------------------------------------------------------------------------------------
	; Encode the image file
	; -------------------------------------------------------------------------------------------------------------------
	DllCall("Crypt32.dll\CryptBinaryToString", "Ptr", &Bin, "UInt", BinLen, "UInt", 0x01, "Ptr", 0, "UIntP", B64Len)
	VarSetCapacity(B64, B64Len << !!A_IsUnicode, 0)
	DllCall("Crypt32.dll\CryptBinaryToString", "Ptr", &Bin, "UInt", BinLen, "UInt", 0x01, "Ptr", &B64, "UIntP", B64Len)
	Bin := ""
	VarSetCapacity(Bin, 0)
	VarSetCapacity(B64, -1)
	B64 := RegExReplace(B64, "\r\n")
	B64Len := StrLen(B64)
	; -------------------------------------------------------------------------------------------------------------------
	
	PartLength := 16000
    CharsRead  := 1

	Part := ""
   While (CharsRead < B64Len) {
	clipboard .= Part . SubStr(b64, CharsRead, PartLength)
      CharsRead += PartLength
   }
	
	tTime := (A_TickCount-t1) 
	FileAppend time: %tTime% `n, *
	FileAppend,  %clipboard% , *
	return
I have found how to do it using C language:
https://en.it1352.com/article/c43b7af5ff9f45169bec20cfda65d48b.html
Currently trying to convert it to AHK
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: how to convert pBitmap to base64

04 Nov 2020, 22:06

Code: Select all

#include q:\vis2\lib\Vis2.ahk ; https://www.autohotkey.com/boards/viewtopic.php?t=36047
base64 = E:\data\temp2\test10.txt
pToken := Gdip_Startup(), snap := Gdip_BitmapFromScreen("0|0|900|900")
FileRecycle, %base64%
FileAppend, % Vis2.stdlib.Gdip_EncodeBitmapTo64string(snap, "JPG"), %base64%
Gdip_DisposeImage(snap), Gdip_Shutdown(pToken)
c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

Re: how to convert pBitmap to base64

04 Nov 2020, 22:16

Ty for your time Mikeyww!
I'm trying to do this conversion without writing any file if possible.

If I would do this saving any file, could be easier to capture the bitmap and use gdip_SaveBitmapToFile and open this new file using the actual code with File.open etc
Last edited by c7aesa7r on 04 Nov 2020, 22:17, edited 1 time in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: how to convert pBitmap to base64  Topic is solved

04 Nov 2020, 22:17

Code: Select all

#include q:\vis2\lib\Vis2.ahk ; https://www.autohotkey.com/boards/viewtopic.php?t=36047
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen("0|0|900|900") ; x|y|width|height
base64 := Vis2.stdlib.Gdip_EncodeBitmapTo64string(snap, "JPG")
Gdip_DisposeImage(snap), Gdip_Shutdown(pToken)
MsgBox, 0, Here is the base64, %base64%
c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

Re: how to convert pBitmap to base64

04 Nov 2020, 22:23

Perfect, thank you so much! :bravo: :thumbup:
@mikeyww do you got any function to also convert bitmap from base64 code?

I was using this code, but I dunno why its not recognizing the b64 generated by the script you send me.

Code: Select all

GDIp_BitmapFromBase64(base64) {
	If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &base64, "UInt", 0, "UInt", 0x01, "Ptr", 0, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
		Return False
	VarSetCapacity(Dec, DecLen, 0)
	If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &base64, "UInt", 0, "UInt", 0x01, "Ptr", &Dec, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
		Return False
  ; Bitmap creation adopted from "How to convert Image data (JPEG/PNG/GIF) to hBITMAP?" by SKAN
  ; -> http://www.autohotkey.com/board/topic/21213-how-to-convert-image-data-jpegpnggif-to-hbitmap/?p=139257
  ;modified by nnnik to return a pBitmap instead
	hData := DllCall("Kernel32.dll\GlobalAlloc", "UInt", 2, "UPtr", DecLen, "UPtr")
	pData := DllCall("Kernel32.dll\GlobalLock", "Ptr", hData, "UPtr")
	DllCall("Kernel32.dll\RtlMoveMemory", "Ptr", pData, "Ptr", &Dec, "UPtr", DecLen)
	DllCall("Kernel32.dll\GlobalUnlock", "Ptr", hData)
	DllCall("Ole32.dll\CreateStreamOnHGlobal", "Ptr", hData, "Int", True, "PtrP", pStream)
	DllCall("Gdiplus.dll\GdipCreateBitmapFromStreamICM",  "Ptr", pStream, "PtrP", pBitmap)
	
	msgbox pbitmap %pbitmap%
	Return pBitmap
}
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: how to convert pBitmap to base64

05 Nov 2020, 07:48

Code: Select all

#include q:\vis2\lib\Vis2.ahk ; https://www.autohotkey.com/boards/viewtopic.php?t=36047
y := x := 100, height := width := 400
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(x "|" y "|" width "|" height) ; x|y|width|height
base64 := Vis2.stdlib.Gdip_EncodeBitmapTo64string(snap, "JPG")
bitmap := GDIp_BitmapFromBase64(base64)
hBitmap := Gdip_CreateHBITMAPFromBitmap(bitmap)
Gdip_DisposeImage(snap), Gdip_DisposeImage(bitmap)
Gdip_Shutdown(pToken)
Gui, New
; https://www.autohotkey.com/boards/viewtopic.php?p=117639#p117639
Gui, Add, Pic, vPic +Border, % "HBITMAP:*" hBitmap
Gui, Show

GDIp_BitmapFromBase64(base64) {
 If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &base64, "UInt", 0, "UInt"
  , 0x01, "Ptr", 0, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
  Return False
 VarSetCapacity(Dec, DecLen, 0)
 If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &base64, "UInt", 0, "UInt"
  , 0x01, "Ptr", &Dec, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
  Return False
 ; Bitmap creation adopted from "How to convert Image data (JPEG/PNG/GIF) to hBITMAP?" by SKAN
 ; -> http://www.autohotkey.com/board/topic/21213-how-to-convert-image-data-jpegpnggif-to-hbitmap/?p=139257
 ;modified by nnnik to return a pBitmap instead
 hData := DllCall("Kernel32.dll\GlobalAlloc", "UInt", 2, "UPtr", DecLen, "UPtr")
 pData := DllCall("Kernel32.dll\GlobalLock", "Ptr", hData, "UPtr")
 DllCall("Kernel32.dll\RtlMoveMemory", "Ptr", pData, "Ptr", &Dec, "UPtr", DecLen)
 DllCall("Kernel32.dll\GlobalUnlock", "Ptr", hData)
 DllCall("Ole32.dll\CreateStreamOnHGlobal", "Ptr", hData, "Int", True, "PtrP", pStream)
 DllCall("Gdiplus.dll\GdipCreateBitmapFromStreamICM",  "Ptr", pStream, "PtrP", pBitmap)
 Return pBitmap
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Hansielein, Lpanatt and 313 guests