Jump to content

Recommended Posts

Posted (edited)

Can you please let me know how to covert a BufferedImage to a byte array???

What is a buffered image and a byte array?

Anyway, here a version which takes a screenshot, convert it to greyscale, save it as a jpg image and finally convert that image to a binary string.

Convert to greyscale is done with GDI+ but can be done with less code using e.g. inline ASM code:

;code by UEZ 2011
#include <ScreenCapture.au3>

_GDIPlus_Startup ()
$HBITMAP = _ScreenCapture_Capture("", 0, 0, 200, 200)
$hBmp = _GDIPlus_BitmapCreateFromHBITMAP($HBITMAP)
$hContext = _GDIPlus_ImageGetGraphicsContext($hBmp)
_Greyscale($hBmp, $hContext)
_GDIPlus_ImageSaveToFile($hBmp, @ScriptDir & "\Test_Grey.jpg")
_GDIPlus_GraphicsDispose($hContext)
_WinAPI_DeleteObject($HBITMAP)
_GDIPlus_BitmapDispose ($hBmp)
_GDIPlus_Shutdown()
Convert2BinString(@ScriptDir & "\Test_Grey.jpg", @ScriptDir & "\Test_Grey_BinString.au3")
Exit

Func Convert2BinString($FileName, $SaveName)
    Local $VarName = "BinString"
    Local $Handle = FileOpen($FileName, 16)
    Local $BinaryString = FileRead($Handle)
    FileClose($Handle)

    Local $LineLen = 1024
    Local $DllString = String($BinaryString)

    Local $Script = "Func " & $VarName & "()" & @CRLF & @TAB & "Local _" & @CRLF & @TAB & "$" & $VarName & " = '" & StringLeft($DllString, $LineLen) & "'" & @CRLF
    $DllString = StringTrimLeft($DllString, $LineLen)


    While StringLen($DllString) > $LineLen
        $Script &= "    $" & $VarName & " &= '" & StringLeft($DllString, $LineLen) & "'" & @CRLF
        $DllString = StringTrimLeft($DllString, $LineLen)
    WEnd

    If StringLen($DllString) <> 0 Then $Script &= "    $" & $VarName & " &= '" & $DllString & "'" & @CRLF
     $Script &= @TAB & "Return Binary($" & $VarName & ")" & @CRLF & "EndFunc"

    Local $hFile = FileOpen($SaveName, 2)
    FileWrite($hFile, $Script)
    FileClose($hFile)
EndFunc

Func _Greyscale($hImage, $hImageContext)
    Local $tNegMatrix, $pNegMatrix, $hIA
    $hIA = _GDIPlus_ImageAttributesCreate()
    $aImageSize = _GDIPlus_ImageGetDimension($hImage)
    If $hImage Then
        $tNegMatrix = _GDIPlus_ColorMatrixCreateGrayScale()
        $pNegMatrix = DllStructGetPtr($tNegMatrix)
        _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pNegMatrix)
        _GDIPlus_GraphicsDrawImageRectRectIA($hImageContext, $hImage, 0, 0, $aImageSize[0], $aImageSize[1], 0, 0, $aImageSize[0], $aImageSize[1], $hIA)
        _GDIPlus_ImageAttributesDispose($hIA)
    EndIf
EndFunc

Func _GDIPlus_ImageGetDimension($hImage)
    Local $aSize[2], $aResult
    $aResult = DllCall($ghGDIPDll, "uint", "GdipGetImageDimension", "hwnd", $hImage, "float*", 0, "float*", 0)
    If @error Then Return SetError(@error, @extended, -1)
    $GDIP_STATUS = $aResult[0]
    If $GDIP_STATUS Then Return -1
    $aSize[0] = $aResult[2]
    $aSize[1] = $aResult[3]
    Return $aSize
EndFunc   ;==>_GDIPlus_ImageGetDimension

Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateImageAttributes", "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[1]
EndFunc   ;==>_GDIPlus_ImageAttributesCreate

Func _GDIPlus_ColorMatrixCreateGrayScale()
    Local Const $GDIP_RLUM = 0.3086
    Local Const $GDIP_GLUM = 0.6094
    Local Const $GDIP_BLUM = 0.0820
    Local Const $tagGDIPCOLORMATRIX = "float m[25];"
    Local $iI, $iJ, $tCM, $aLums[4] = [$GDIP_RLUM, $GDIP_GLUM, $GDIP_BLUM, 0]
    $tCM = DllStructCreate($tagGDIPCOLORMATRIX)
    For $iI = 0 To 3
        For $iJ = 1 To 3
            DllStructSetData($tCM, "m", $aLums[$iI], $iI * 5 + $iJ)
        Next
    Next
    DllStructSetData($tCM, "m", 1, 19)
    DllStructSetData($tCM, "m", 1, 25)
    Return $tCM
EndFunc   ;==>_GDIPlus_ColorMatrixCreateGrayScale

Func _GDIPlus_ImageAttributesSetColorMatrix($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $pClrMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetImageAttributesColorMatrix", "hwnd", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "ptr", $pClrMatrix, "ptr", $pGrayMatrix, "int", $iColorMatrixFlags)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesSetColorMatrix

Func _GDIPlus_GraphicsDrawImageRectRectIA($hGraphics, $hImage, $nSrcX, $nSrcY, $nSrcWidth, $nSrcHeight, $nDstX, $nDstY, $nDstWidth, $nDstHeight, $hImageAttributes = 0, $iUnit = 2)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRect", "hwnd", $hGraphics, "hwnd", $hImage, "float", $nDstX, "float", _
            $nDstY, "float", $nDstWidth, "float", $nDstHeight, "float", $nSrcX, "float", $nSrcY, "float", $nSrcWidth, "float", _
            $nSrcHeight, "int", $iUnit, "hwnd", $hImageAttributes, "int", 0, "int", 0)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectIA

Func _GDIPlus_ImageAttributesDispose($hImageAttributes)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipDisposeImageAttributes", "hwnd", $hImageAttributes)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesDispose

Look in script dir for the results.

Br,

UEZ

Edited 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...