FireFox Posted May 28, 2011 Share Posted May 28, 2011 (edited) Hi, I would like to convert the bitmap handle from the function _ScreenCapture_Capture to bytes. Thanks for your help. Br, Firefox. Edited May 29, 2011 by FireFox Link to comment Share on other sites More sharing options...
UEZ Posted May 28, 2011 Share Posted May 28, 2011 Capture the screen, save it, convert saved image to a binary string. Or do you really need it directly from memory? Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Andreik Posted May 28, 2011 Share Posted May 28, 2011 Try this: #include <GDIPlus.au3> #include <Memory.au3> #include <ScreenCapture.au3> $PATH = _ScreenCapture_Capture("") MsgBox(0,"",HBITMAP_To_Bytes($PATH)) Func HBITMAP_To_Bytes($HBITMAP) _GDIPlus_Startup() Local $BITMAP = _GDIPlus_BitmapCreateFromHBITMAP($HBITMAP) Local $JPG_ENCODER = _GDIPlus_EncodersGetCLSID("jpg") Local $TAG_ENCODER = _WinAPI_GUIDFromString($JPG_ENCODER) Local $PTR_ENCODER = DllStructGetPtr($TAG_ENCODER) Local $STREAM = DllCall("ole32.dll","uint","CreateStreamOnHGlobal","ptr",0,"bool",1,"ptr*",0) DllCall($ghGDIPDll,"uint","GdipSaveImageToStream","ptr",$BITMAP,"ptr",$STREAM[3],"ptr",$PTR_ENCODER,"ptr",0) _GDIPlus_BitmapDispose($BITMAP) Local $MEMORY = DllCall("ole32.dll","uint","GetHGlobalFromStream","ptr",$STREAM[3],"ptr*",0) Local $MEM_SIZE = _MemGlobalSize($MEMORY[2]) Local $MEM_PTR = _MemGlobalLock($MEMORY[2]) Local $DATA_STRUCT = DllStructCreate("byte[" & $MEM_SIZE & "]", $MEM_PTR) Local $DATA = DllStructGetData($DATA_STRUCT,1) _MemGlobalFree($MEMORY[2]) _GDIPlus_Shutdown() Return $DATA EndFunc When the words fail... music speaks. Link to comment Share on other sites More sharing options...
UEZ Posted May 28, 2011 Share Posted May 28, 2011 Cool code Andreik! Thanks for sharing. Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
FireFox Posted May 28, 2011 Author Share Posted May 28, 2011 @UEZ That's what I had actually but for security reasons I don't want to do that. @Andreik Thanks for your help it works great Br, FireFox. Link to comment Share on other sites More sharing options...
ProgAndy Posted May 28, 2011 Share Posted May 28, 2011 (edited) Depending on what binary format you need, the result of _WinAPI_GetDIBits could suffice. @Andreik: You have a memory leak since the stream is never deleted. Read the last post of _SetImageBinaryToCtrl in my signature when you want to solve it. Edited May 28, 2011 by ProgAndy *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
FireFox Posted May 28, 2011 Author Share Posted May 28, 2011 (edited) Depending on what binary format you need, the result of _WinAPI_GetDIBits could suffice.@Andreik: You have a memory leak since the stream is never deleted. Read the last post of _SetImageBinaryToCtrl in my signature when you want to solve it.If there is a problem with his function can you give me an alternative? (e.g how to use this winapi func)Br, FireFox. Edited May 28, 2011 by FireFox Link to comment Share on other sites More sharing options...
Andreik Posted May 28, 2011 Share Posted May 28, 2011 @ProgAndy I`m not sure about this. MSDN said the second parameter of CreateStreamOnHGlobal() is fDeleteOnRelease and if is set as True the final release will automatically free the hGlobal parameter but I will take a look at your example maybe I am wrong. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
ProgAndy Posted May 28, 2011 Share Posted May 28, 2011 If there is a problem with his function can you give me an alternative? (e.g how to use this winapi func) Br, FireFox. You said you need a binary representation. In which form do you need it? Just the bytes of the pixels, a complete BMP or JPG file in memory, ... His function is missing these lines to delete the Stream-object: Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr") Local $aCall = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $STREAM[3], "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
FireFox Posted May 28, 2011 Author Share Posted May 28, 2011 You said you need a binary representation. In which form do you need it? Just the bytes of the pixels, a complete BMP or JPG file in memory, ... His function is missing these lines to delete the Stream-object: Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr") Local $aCall = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $STREAM[3], "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) I need a complete JPG file in memory or if there is a way to just save the pixels and then set them to a picture control it will take it. Br, FireFox. Link to comment Share on other sites More sharing options...
FireFox Posted May 28, 2011 Author Share Posted May 28, 2011 (edited) @ProgAndyI got Andreik's function working and I added your 2 lines before the _MemGlobalFree func and I works as I want so for me it's solved Br, FireFox Edited May 28, 2011 by FireFox Link to comment Share on other sites More sharing options...
FireFox Posted May 28, 2011 Author Share Posted May 28, 2011 @ProgAndy In fact I want to "load" the binary from a modified unreadable jpg file, then edit bytes to make it readable and set it to a pic control. I hope it's possible Br, FireFox. Link to comment Share on other sites More sharing options...
Yashied Posted May 28, 2011 Share Posted May 28, 2011 (edited) Why use GDI+? #Include <ScreenCapture.au3> $hBitmap = _ScreenCapture_Capture() $tBITMAP = DllStructCreate('long bmType;long bmWidth;long bmHeight;long bmWidthBytes;ushort bmPlanes;ushort bmBitsPixel;ptr bmBits') DllCall('gdi32.dll', 'int', 'GetObject', 'ptr', $hBitmap, 'int', DllStructGetSize($tBITMAP), 'ptr', DllStructGetPtr($tBITMAP)) $tBits = DllStructCreate('byte[' & DllStructGetData($tBITMAP, 'bmWidth') * DllStructGetData($tBITMAP, 'bmHeight') * DllStructGetData($tBITMAP, 'bmBitsPixel') / 8 & ']') DllCall('gdi32.dll', 'dword', 'GetBitmapBits', 'ptr', $hBitmap, 'dword', DllStructGetSize($tBits), 'ptr', DllStructGetPtr($tBits)) DllCall('gdi32.dll', 'int', 'DeleteObject', 'ptr', $hBitmap) $bBits = DllStructGetData($tBits, 1) ConsoleWrite(BinaryLen($bBits) & @CR) Edited May 28, 2011 by Yashied My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
ProgAndy Posted May 28, 2011 Share Posted May 28, 2011 (edited) When you want to set an image to a picture control (e.g. GUICtrlCreatePic) Then you need a HBITMAP, just as it _ScreeCapture_Capture returns. (use _GUICtrlStatic_SetImage from )If you have an unreadable JPG:- read binary to memory- modify it- use the function from the link to load the jpg from memory with GDI+- convert it to a HBITMAP- delete the GDI+-image- set the HBITMAP to the control- don't delete the HBITMAP, now it belongs to the pic-control Edited May 28, 2011 by ProgAndy *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
UEZ Posted June 13, 2011 Share Posted June 13, 2011 (edited) Here a modification of Andreik's code - now you can select the JPG save quality: expandcollapse popup#include <Memory.au3> #include <ScreenCapture.au3> ;includes GDIPlus.au3 _GDIPlus_Startup() ;don't forget ;-) $HBITMAP = _ScreenCapture_Capture("", 0, 0, 100, 100) $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($HBITMAP) $s = Bitmap2BinaryString($hBitmap) ConsoleWrite(StringLen($s) & @CRLF) _WinAPI_DeleteObject($HBITMAP) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_ShutDown() Exit ;GDIPlus.au3 must be included; JPG quality: 0 - 100 (worst - best) ;$hBitmap must be a GDI+ compatible bitmap format!!! Func Bitmap2BinaryString($hBitmap, $JPEG_Quality = 90) ;code by Andreik, modified by UEZ Local $declared = True If Not $ghGDIPDll Then _GDIPlus_Startup() $declared = False EndIf Local $STREAM = DllCall("ole32.dll", "uint", "CreateStreamOnHGlobal", "ptr", 0, "bool", 1, "ptr*", 0) $STREAM = $STREAM[3] Local $JPG_ENCODER = _GDIPlus_EncodersGetCLSID("JPG") Local $TAG_ENCODER = _WinAPI_GUIDFromString($JPG_ENCODER) Local $PTR_ENCODER = DllStructGetPtr($TAG_ENCODER) Local $tParams = _GDIPlus_ParamInit (1) Local $tData = DllStructCreate("int Quality") DllStructSetData($tData, "Quality", $JPEG_Quality) Local $pData = DllStructGetPtr($tData) _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData) Local $pParams = DllStructGetPtr($tParams) DllCall($ghGDIPDll, "uint", "GdipSaveImageToStream", "ptr", $hBitmap, "ptr", $STREAM, "ptr", $PTR_ENCODER, "ptr", $pParams) $tData = 0 $tParams = 0 Local $MEMORY = DllCall("ole32.dll", "uint", "GetHGlobalFromStream", "ptr", $STREAM, "ptr*", 0) $MEMORY = $MEMORY[2] Local $MEM_SIZE = _MemGlobalSize($MEMORY) Local $MEM_PTR = _MemGlobalLock($MEMORY) Local $DATA_STRUCT = DllStructCreate("byte[" & $MEM_SIZE & "]", $MEM_PTR) Local $DATA = DllStructGetData($DATA_STRUCT, 1) Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr") Local $aCall = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $STREAM, "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) _MemGlobalFree($MEMORY) If Not $declared Then _GDIPlus_Shutdown() Return $DATA EndFunc Br, UEZ Edited June 15, 2011 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now