[R] HBitmap <-> String

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
Avatar du membre
matwachich
Membre émérite
Membre émérite
Messages : 986
Enregistré le : lun. 19 oct. 2009 04:04
Localisation : Algérie
Status : Hors ligne

[R] HBitmap <-> String

#1

Message par matwachich »

Alors voila, je reviens avec une nouvelle question.

J'arrive à convertir un bitmap (capture d'écran) en Chaîne de caractères (représentant les pixels), mais pas le contraire.
Mon but est de pourvoir transmettre une image par TCP sans passer par le disque dur (pour faire une sorte de VNC).

J'imagine qu'il faudra utiliser les fonction d'allocation dynamique de mémoire. Avec un autre langage, j'aurai pu faire quelque chose, mais la, j'ai du mal! (C'est la qu'on se rend compte de l'importance du typage!)

Je vous montre mon code:

Code : Tout sélectionner

#include <GDIPlus.au3>
#include <ScreenCapture.au3>

_GDIPlus_Startup()

; ##############################################################

$hBitmap = _ScreenCapture_Capture()
$bitmap = _GDIPlus_BitmapCreateFromHBitmap($hBitmap)

$bitmapData = _GDIPlus_BitmapLockBits($bitmap, 0, 0, @DesktopWidth, @DesktopHeight, $GDIP_ILMREAD, $GDIP_PXF24RGB)
$Scan0 = DllStructGetData($bitmapData, "Scan0")
_GDIPlus_BitmapUnlockBits($bitmap, $bitmapData)

$v_BufferA = DllStructCreate("byte[" & @DesktopWidth * @DesktopHeight * 3 & "]", $Scan0) ; Create DLL structure for all pixels
$AllPixels = DllStructGetData($v_BufferA, 1)

ConsoleWrite(StringLen($AllPixels) & @CRLF) ; Final pixels string (Binary) de forme: 0x111111222222333333444444 ... (RGB)

_GDIPlus_BitmapDispose($bitmap)
_WinApi_DeleteObject($hBitmap)

; ##############################################################


; ##############################################################

_GDIPlus_Shutdown()
Modifié en dernier par matwachich le dim. 04 sept. 2011 12:00, modifié 1 fois.
Sortons VW du coté obscure! - La curiosité est un vilain défaut! Cliquez ici
Avatar du membre
matwachich
Membre émérite
Membre émérite
Messages : 986
Enregistré le : lun. 19 oct. 2009 04:04
Localisation : Algérie
Status : Hors ligne

Re: [..] HBitmap <-> String

#2

Message par matwachich »

Voila ce que j'arrive a bricoler avec un peut de temps, et de surf sur le forum anglais, mais ça marche pas.

Code : Tout sélectionner

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#Include <Memory.au3>

#include <perso\ImageResize.au3>

Global Const $InterpolationModeInvalid = -1
Global Const $InterpolationModeDefault = 0
Global Const $InterpolationModeLowQuality = 1
Global Const $InterpolationModeHighQuality = 2
Global Const $InterpolationModeBilinear = 3
Global Const $InterpolationModeBicubic = 4
Global Const $InterpolationModeNearestNeighbor = 5
Global Const $InterpolationModeHighQualityBilinear = 6
Global Const $InterpolationModeHighQualityBicubic = 7

_GDIPlus_Startup()

; ##############################################################

$hBitmap = _ScreenCapture_Capture()

$bitmap = _HBitmapResize($hBitmap, 800, 600)

$bitmapData = _GDIPlus_BitmapLockBits($bitmap, 0, 0, 800, 600, $GDIP_ILMREAD, $GDIP_PXF24RGB)
$Scan0 = DllStructGetData($bitmapData, "Scan0")
_GDIPlus_BitmapUnlockBits($bitmap, $bitmapData)
$bitmapData = 0

$v_BufferA = DllStructCreate("byte[" & 800 * 600 * 3 & "]", $Scan0) ; Create DLL structure for all pixels
$AllPixels = DllStructGetData($v_BufferA, 1)
$v_BufferA = 0

ConsoleWrite(StringLen($AllPixels) & @CRLF) ; Final pixels string (Binary) de forme: 0x111111222222333333444444 ... (RGB)

_GDIPlus_BitmapDispose($bitmap)
_WinApi_DeleteObject($hBitmap)

; ##############################################################

$bin = Binary($AllPixels)
$binLen = BinaryLen($bin)

$hData  = _MemGlobalAlloc($binLen, $GMEM_MOVEABLE) ;allocates movable memory  ($GMEM_MOVEABLE = 0x0002)
$pData = _MemGlobalLock($hData)  ;translate the handle into a pointer
$tMem =  DllStructCreate("byte[" & $binLen & "]", $pData) ;create struct
DllStructSetData($tMem, 1, $bin) ;fill struct with image data
_MemGlobalUnlock($hData)

$newHB = _WinApi_CreateBitmap(800, 600)
$newbitmap = _HBitmapResize($newHB, 800, 600)

$bitmapData = _GDIPlus_BitmapLockBits($newbitmap, 0, 0, 800, 600, $GDIP_ILMWRITE, $GDIP_PXF24RGB)
DllStructSetData($bitmapData, "Scan0", $pData)
_GDIPlus_BitmapUnlockBits($newbitmap, $bitmapData)

_GDIPlus_ImageSaveToFile($newbitmap, @ScriptDir & "\test.jpg")

_GDIPlus_BitmapDispose($newbitmap)
_WinApi_DeleteObject($newHB)

$tMem = 0

; ##############################################################

Func _HBitmapResize($hbitmap, $iWidth, $iHeight)
    $bitmap = _GDIPlus_BitmapCreateFromHBITMAP($hbitmap)
    ; ---
    $graphics = _GDIPlus_ImageGetGraphicsContext($bitmap)
    $resizedbitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $graphics)
    $graphics2 = _GDIPlus_ImageGetGraphicsContext($resizedbitmap)
    _GDIPLUS_GraphicsSetInterpolationMode($graphics2, $InterpolationModeHighQualityBicubic)
    _GDIPlus_GraphicsDrawImageRect($graphics2, $bitmap, 0, 0, $iWidth, $iHeight)
    ;_GDIPlus_ImageSaveToFile($resizedbitmap, @ScriptDir & "\test.jpg")
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_GraphicsDispose($graphics2)
    _GDIPlus_BitmapDispose($bitmap)
   ; _GDIPlus_BitmapDispose($resizedbitmap)
   Return $resizedbitmap
EndFunc  ;==>_SavehBitmap

Func _GDIPLUS_GraphicsSetInterpolationMode($hGraphics, $iMode)
    DllCall($ghGDIPDll, "int", "GdipSetInterpolationMode", "hwnd", $hGraphics,"int",$iMode)
EndFunc  ;==>_GDIPLUS_GraphicsSetInterpolationMode

_GDIPlus_Shutdown()
Sortons VW du coté obscure! - La curiosité est un vilain défaut! Cliquez ici
Avatar du membre
mikell
Spammer !
Spammer !
Messages : 6292
Enregistré le : dim. 29 mai 2011 17:32
Localisation : Deep Cévennes
Status : Hors ligne

Re: [..] HBitmap <-> String

#3

Message par mikell »

Heu si le but est de convertir une image en string pourquoi ne pas utiliser l'UDF ConvertImageToBinary.au3 ?
:shock:
" L'échec est le fondement de la réussite. " (Lao-Tseu )
" Plus ça rate, plus on a de chances que ça marche " (les Shadoks )
Avatar du membre
matwachich
Membre émérite
Membre émérite
Messages : 986
Enregistré le : lun. 19 oct. 2009 04:04
Localisation : Algérie
Status : Hors ligne

Re: [..] HBitmap <-> String

#4

Message par matwachich »

Merci de la réponse mais:
1- Je le trouve pas cet UDF
2- Je ne crois pas que ce soit ce que je cherche car Il faut avoir un fichier image source, ce que je ne veut pas, car je dois transférer des HBITMAP issus de _ScreenCapture_Capture (sans passer par le disque dur)

Edit: je crois que j'ai trouver: http://www.autoitscript.com/forum/topic ... _p__901439, coupler ça avec la fonction qui charge un binary string comme image et c'est bon!

Je posterai les fonction une fois qu'elles seront opérationnelles.
Sortons VW du coté obscure! - La curiosité est un vilain défaut! Cliquez ici
Avatar du membre
matwachich
Membre émérite
Membre émérite
Messages : 986
Enregistré le : lun. 19 oct. 2009 04:04
Localisation : Algérie
Status : Hors ligne

Re: [..] HBitmap <-> String

#5

Message par matwachich »

Sortons VW du coté obscure! - La curiosité est un vilain défaut! Cliquez ici
Répondre