Jump to content

GDI+ Captcha letters missed


Recommended Posts

I tried to write a simple captcha but for some reason, sometime not all letters are displayed. What I miss?

#include <GDIPlus.au3>

MsgBox(0,"",Captcha())

Func Captcha()
    Local $sCaptcha
    Local $aBrush[8] = [0xFFFF0000,0xFFFF8000,0xFFFFFF00,0xFF00FF00,0xFF00FF00,0xFFFF00FF,0xFF0000FF,0xFF008080]
    Local $aStyle[4] = [0,1,2,4]
    Local $aFont[4] = ['Arial','Courier New','Tahoma','MS Sans Serif']
    Local $aBack[4] = [0xFF8080FF,0xFF80FF80,0xFF804000,0xFFC0C0C0]
    Local $CharSet = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','')
    _GDIPlus_Startup()
    Local $hHBITMAP = _WinAPI_CreateBitmap(250,100,1,32)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBITMAP)
    _WinAPI_DeleteObject($hHBITMAP)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local $hPen = _GDIPlus_PenCreate(Random(0xFFBB0000,0xFFFFFFFF,1),Random(1,3,1))
    Local $hBrush = _GDIPlus_BrushCreateSolid($aBrush[Random(0,7,1)])
    Local $hFormat = _GDIPlus_StringFormatCreate()

    _GDIPlus_GraphicsClear($hGraphics,$aBack[Random(0,3,1)])
    For $iX = 0 To 3
        Local $hFamily = _GDIPlus_FontFamilyCreate($aFont[Random(0,3,1)])
        Local $hFont = _GDIPlus_FontCreate($hFamily,Random(22,32,1),$aStyle[Random(0,3,1)])
        _GDIPlus_BrushSetSolidColor($hBrush,$aBrush[Random(0,7,1)])
        _GDIPlus_StringFormatSetAlign($hFormat,Random(0,2,1))

        Local $sLetter = $CharSet[Random(1,$CharSet[0],1)]
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,30,1),Random(0,100,1),Random(200,250,1),Random(0,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFFBB0000,0xFFFFFFFF,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,250,1),Random(0,5,1),Random(0,250,1),Random(90,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFF900000,0xFFAAAAAA,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))

        _GDIPlus_GraphicsDrawStringEx($hGraphics,$sLetter,$hFont,_GDIPlus_RectFCreate(5+$iX*60,Random(0,40,1),60,100),$hFormat,$hBrush)

        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)

        $sCaptcha &= $sLetter
    Next
    _GDIPlus_ImageSaveToFile($hBitmap,@ScriptDir & "\captcha.png")

    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    Return $sCaptcha
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

The problem is the font MS Sans Serif. It will not be displayed.

Here some small modifications to your code:

#include <GDIPlus.au3>

MsgBox(0,"",Captcha())

Func Captcha()
    Local $sCaptcha
    Local $aBrush[8] = [0xFFFF0000,0xFFFF8000,0xFFFFFF00,0xFF00FF00,0xFF00FF00,0xFFFF00FF,0xFF0000FF,0xFF008080]
    Local $aStyle[4] = [0,1,2,4]
    Local $aFont[4] = ['Arial','Courier New','Tahoma','Comic Sans MS']
    Local $aBack[4] = [0xFF8080FF,0xFF80FF80,0xFF804000,0xFFC0C0C0]
    Local $CharSet = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','')
    _GDIPlus_Startup()
    Local $hHBITMAP = _WinAPI_CreateBitmap(250,100,1,32)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBITMAP)
    _WinAPI_DeleteObject($hHBITMAP)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
    DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hGraphics, "int", 4)
    Local $hPen = _GDIPlus_PenCreate(Random(0xFFBB0000,0xFFFFFFFF,1),Random(1,3,1))
    Local $hBrush = _GDIPlus_BrushCreateSolid($aBrush[Random(0,7,1)])
    Local $hFormat = _GDIPlus_StringFormatCreate()

    _GDIPlus_GraphicsClear($hGraphics,$aBack[Random(0,3,1)])
    Local $hFamily, $hFont
    Local $tLayout = _GDIPlus_RectFCreate(0,0,60,100)
    For $iX = 0 To 3
        $hFamily = _GDIPlus_FontFamilyCreate($aFont[Random(0,3,1)])
        $hFont = _GDIPlus_FontCreate($hFamily,Random(22,32,1),$aStyle[Random(0,3,1)])
        _GDIPlus_BrushSetSolidColor($hBrush,$aBrush[Random(0,7,1)])
        _GDIPlus_StringFormatSetAlign($hFormat,Random(0,2,1))

        Local $sLetter = $CharSet[Random(1,$CharSet[0],1)]
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,30,1),Random(0,100,1),Random(200,250,1),Random(0,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFFBB0000,0xFFFFFFFF,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,250,1),Random(0,5,1),Random(0,250,1),Random(90,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFF900000,0xFFAAAAAA,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))
        DllStructSetData($tLayout, "x",5+$iX*60)
        DllStructSetData($tLayout, "y",Random(0,40,1))
        _GDIPlus_GraphicsDrawStringEx($hGraphics,$sLetter,$hFont,$tLayout,$hFormat,$hBrush)
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        $sCaptcha &= $sLetter
    Next
    _GDIPlus_ImageSaveToFile($hBitmap,@ScriptDir & "captcha.png")
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    $tLayout = 0
    Return $sCaptcha
EndFunc

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

Link to comment
Share on other sites

It sets the anti aliasing for font drawings when using Aero?!? -> Graphics.SetTextRenderingHint method

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

Link to comment
Share on other sites

  • 1 month later...

Hi UEZ,

I'd like your improved code so i'm try to make a little Captcha Form:

#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GDIPlus.au3>

Global $Captcha = Captcha()

Func Captcha()
    Local $sCaptcha
    Local $aBrush[8] = [0xFFFF0000,0xFFFF8000,0xFFFFFF00,0xFF00FF00,0xFF00FF00,0xFFFF00FF,0xFF0000FF,0xFF008080]
    Local $aStyle[4] = [0,1,2,4]
    Local $aFont[4] = ['Arial','Courier New','Tahoma','Comic Sans MS']
    Local $aBack[4] = [0xFF8080FF,0xFF80FF80,0xFF804000,0xFFC0C0C0]
    Local $CharSet = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','')
    _GDIPlus_Startup()
    Local $hHBITMAP = _WinAPI_CreateBitmap(250,100,1,32)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBITMAP)
    _WinAPI_DeleteObject($hHBITMAP)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
    DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hGraphics, "int", 4)
    Local $hPen = _GDIPlus_PenCreate(Random(0xFFBB0000,0xFFFFFFFF,1),Random(1,3,1))
    Local $hBrush = _GDIPlus_BrushCreateSolid($aBrush[Random(0,7,1)])
    Local $hFormat = _GDIPlus_StringFormatCreate()
    _GDIPlus_GraphicsClear($hGraphics,$aBack[Random(0,3,1)])
    Local $hFamily, $hFont
    Local $tLayout = _GDIPlus_RectFCreate(0,0,60,100)
    For $iX = 0 To 3
        $hFamily = _GDIPlus_FontFamilyCreate($aFont[Random(0,3,1)])
        $hFont = _GDIPlus_FontCreate($hFamily,Random(22,32,1),$aStyle[Random(0,3,1)])
        _GDIPlus_BrushSetSolidColor($hBrush,$aBrush[Random(0,7,1)])
        _GDIPlus_StringFormatSetAlign($hFormat,Random(0,2,1))
        Local $sLetter = $CharSet[Random(1,$CharSet[0],1)]
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,30,1),Random(0,100,1),Random(200,250,1),Random(0,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFFBB0000,0xFFFFFFFF,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,250,1),Random(0,5,1),Random(0,250,1),Random(90,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFF900000,0xFFAAAAAA,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))
        DllStructSetData($tLayout, "x",5+$iX*60)
        DllStructSetData($tLayout, "y",Random(0,40,1))
        _GDIPlus_GraphicsDrawStringEx($hGraphics,$sLetter,$hFont,$tLayout,$hFormat,$hBrush)
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        $sCaptcha &= $sLetter
    Next
    _GDIPlus_ImageSaveToFile($hBitmap,@ScriptDir & "captcha.jpg")
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    $tLayout = 0
    Return $sCaptcha
EndFunc

$Form = GUICreate("Captcha Test", 221, 192, -1, -1)
$Pic = GUICtrlCreatePic(@WorkingDir & "captcha.jpg", 8, 8, 201, 97)
$Label = GUICtrlCreateLabel("Insert Captcha", 8, 112, 73, 17)
$Input = GUICtrlCreateInput("", 8, 128, 201, 21)
$ButtonOk = GUICtrlCreateButton("OK", 8, 152, 97, 33)
$ButtonGenerate = GUICtrlCreateButton("Generate", 112, 152, 97, 33)
GUISetState(@SW_SHOW)

While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $ButtonOk
   If GUICtrlRead($Input) = $Captcha Then
    MsgBox(0,0,"OK")
   Else
    MsgBox(0,0,"Wrong!")
   EndIf
  Case $ButtonGenerate
   Captcha()
   GUICtrlSetImage($Pic, @WorkingDir & "captcha.jpg")
 EndSwitch
WEnd

I have two problems ( i need to learn GDI+ a day ):

1) Captcha is always wrong, everytime :)

2) How to make more letter? What is the string for make more then 4?

Thanks ;)

Link to comment
Share on other sites

Try this:

#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GDIPlus.au3>

Global $Captcha = Captcha()

$Form = GUICreate("Captcha Test", 221, 192, -1, -1)
$Pic = GUICtrlCreatePic(@WorkingDir & "captcha.jpg", 8, 8, 201, 97)
$Label = GUICtrlCreateLabel("Insert Captcha", 8, 112, 73, 17)
$Input = GUICtrlCreateInput("", 8, 128, 201, 21)
$ButtonOk = GUICtrlCreateButton("OK", 8, 152, 97, 33)
$ButtonGenerate = GUICtrlCreateButton("Generate", 112, 152, 97, 33)
GUISetState(@SW_SHOW)

While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $ButtonOk
   If GUICtrlRead($Input) == $Captcha Then
    MsgBox(0,0,"OK")
   Else
    MsgBox(0,0,"Wrong!")
   EndIf
  Case $ButtonGenerate
   $Captcha = Captcha()
   GUICtrlSetImage($Pic, @WorkingDir & "captcha.jpg")
 EndSwitch
WEnd

Func Captcha($characters = 6)
    Local $sCaptcha
    Local $aBrush[8] = [0xFFFF0000,0xFFFF8000,0xFFFFFF00,0xFF00FF00,0xFF00FF00,0xFFFF00FF,0xFF0000FF,0xFF008080]
    Local $aStyle[4] = [0,1,2,4]
    Local $aFont[4] = ['Arial','Courier New','Tahoma','Comic Sans MS']
    Local $aBack[4] = [0xFFA0A0FF,0xFFA0FFA0,0xFFA08000,0xFFC0C0C0]
    Local $CharSet = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','')
    _GDIPlus_Startup()
    Local $hHBITMAP = _WinAPI_CreateBitmap(250,100,1,32)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBITMAP)
    _WinAPI_DeleteObject($hHBITMAP)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
    DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hGraphics, "int", 4)
    Local $hPen = _GDIPlus_PenCreate(Random(0xFFBB0000,0xFFFFFFFF,1),Random(1,3,1))
    Local $hBrush = _GDIPlus_BrushCreateSolid($aBrush[Random(0,7,1)])
    Local $hFormat = _GDIPlus_StringFormatCreate()
    _GDIPlus_GraphicsClear($hGraphics,$aBack[Random(0,3,1)])
    Local $hFamily, $hFont
    Local $tLayout = _GDIPlus_RectFCreate(0,0,60,100)
    For $iX = 0 To $characters - 1
        $hFamily = _GDIPlus_FontFamilyCreate($aFont[Random(0,3,1)])
        $hFont = _GDIPlus_FontCreate($hFamily,Random(22,32,1),$aStyle[Random(0,3,1)])
        _GDIPlus_BrushSetSolidColor($hBrush,$aBrush[Random(0,7,1)])
        _GDIPlus_StringFormatSetAlign($hFormat,Random(0,2,1))
        Local $sLetter = $CharSet[Random(1,$CharSet[0],1)]
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,30,1),Random(0,100,1),Random(200,250,1),Random(0,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFFBB0000,0xFFFFFFFF,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))
        _GDIPlus_GraphicsDrawLine($hGraphics,Random(0,250,1),Random(0,5,1),Random(0,250,1),Random(90,100,1),$hPen)
        _GDIPlus_PenSetColor($hPen,Random(0xFF900000,0xFFAAAAAA,1))
        _GDIPlus_PenSetWidth($hPen,Random(1,3,1))
        DllStructSetData($tLayout, "x",10-$characters+$iX*(60 - $characters * 4))
        DllStructSetData($tLayout, "y",Random(0,40,1))
        _GDIPlus_GraphicsDrawStringEx($hGraphics,$sLetter,$hFont,$tLayout,$hFormat,$hBrush)
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        $sCaptcha &= $sLetter
    Next
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sCaptcha = ' & $sCaptcha & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    _GDIPlus_ImageSaveToFile($hBitmap,@ScriptDir & "captcha.jpg")
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    $tLayout = 0
    Return $sCaptcha
EndFunc

1) you called the Captcha function but didn't save the return value

2) modified function to print more characters

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

I have tested it and there is only a problem. When using 6 or more there is an overlap of letters as you can see:

Posted Image

Maybe for the total dimesion of the image, is too little for more than 4/5 letters

If I make the _WinAPI_CreateBitmap bigger only the background change. If you can resolve this ( overlapping of letters caused by the size of the bitmap? Please add an easy option for change the total image dimension including lines/text/background, like the one you put for the number of letters) i think it's really perfect and useful.

Thanks for your time ;)

Edited by johnmcloud
Link to comment
Share on other sites

I have make some change but the letters area are the same:

Func Captcha($characters = 8, $width = 500, $height = 500)
Local $sCaptcha
Local $aBrush[8] = [0xFFFF0000, 0xFFFF8000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FF00, 0xFFFF00FF, 0xFF0000FF, 0xFF008080]
Local $aStyle[4] = [0, 1, 2, 4]
Local $aFont[4] = ['Arial', 'Courier New', 'Tahoma', 'Comic Sans MS']
Local $aBack[4] = [0xFFA0A0FF, 0xFFA0FFA0, 0xFFA08000, 0xFFC0C0C0]
Local $CharSet = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', '')
_GDIPlus_Startup()
Local $hHBITMAP = _WinAPI_CreateBitmap($width, $height, 1, 32)
Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBITMAP)
_WinAPI_DeleteObject($hHBITMAP)
Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hGraphics, "int", 4)
Local $hPen = _GDIPlus_PenCreate(Random(0xFFBB0000, 0xFFFFFFFF, 1), Random(1, 3, 1))
Local $hBrush = _GDIPlus_BrushCreateSolid($aBrush[Random(0, 7, 1)])
Local $hFormat = _GDIPlus_StringFormatCreate()
_GDIPlus_GraphicsClear($hGraphics, $aBack[Random(0, 3, 1)])
Local $hFamily, $hFont
Local $tLayout = _GDIPlus_RectFCreate(0, 0, 60, $height)
For $iX = 0 To $characters - 1
  $hFamily = _GDIPlus_FontFamilyCreate($aFont[Random(0, 3, 1)])
  $hFont = _GDIPlus_FontCreate($hFamily, Random(22, 32, 1), $aStyle[Random(0, 3, 1)])
  _GDIPlus_BrushSetSolidColor($hBrush, $aBrush[Random(0, 7, 1)])
  _GDIPlus_StringFormatSetAlign($hFormat, Random(0, 2, 1))
  Local $sLetter = $CharSet[Random(1, $CharSet[0], 1)]
  _GDIPlus_GraphicsDrawLine($hGraphics, Random(0, 30, 1), Random(0, $height, 1), Random(200, $width, 1), Random(0, $height, 1), $hPen)
  _GDIPlus_PenSetColor($hPen, Random(0xFFBB0000, 0xFFFFFFFF, 1))
  _GDIPlus_PenSetWidth($hPen, Random(1, 3, 1))
  _GDIPlus_GraphicsDrawLine($hGraphics, Random(0, $width, 1), Random(0, 5, 1), Random(0, $width, 1), Random(90, $height, 1), $hPen)
  _GDIPlus_PenSetColor($hPen, Random(0xFF900000, 0xFFAAAAAA, 1))
  _GDIPlus_PenSetWidth($hPen, Random(1, 3, 1))
  DllStructSetData($tLayout, "x", 10 - $characters + $iX * (60 - $characters * 4))
  DllStructSetData($tLayout, "y", Random(0, 40, 1))
  _GDIPlus_GraphicsDrawStringEx($hGraphics, $sLetter, $hFont, $tLayout, $hFormat, $hBrush)
  _GDIPlus_FontDispose($hFont)
  _GDIPlus_FontFamilyDispose($hFamily)
  $sCaptcha &= $sLetter
Next
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sCaptcha = ' & $sCaptcha & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "captcha.jpg")
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()
$tLayout = 0
Return $sCaptcha
EndFunc   ;==>Captcha

Posted Image

Some advice?

Edited by johnmcloud
Link to comment
Share on other sites

Ok, I found some minutes.

What about this version?

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>

_GDIPlus_Startup()
Global Const $STM_SETIMAGE = 0x0172
Global Const $hGUI = GUICreate("Captcha Test", 221, 192, -1, -1)
Global Const $idPic = GUICtrlCreatePic("", 8, 8, 201, 97)
Global Const $Label = GUICtrlCreateLabel("Insert Captcha", 8, 112, 73, 17)
Global Const $Input = GUICtrlCreateInput("", 8, 128, 201, 21)
Global Const $ButtonOk = GUICtrlCreateButton("OK", 8, 152, 97, 33)
Global Const $ButtonGenerate = GUICtrlCreateButton("Generate", 112, 152, 97, 33)
GUISetState(@SW_SHOW)
Global $Captcha = Captcha($idPic, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _GDIPlus_Shutdown()
            GUIDelete($hGUI)
            Exit
        Case $ButtonOk
            If GUICtrlRead($Input) == $Captcha Then
                MsgBox(0, 0, "OK")
            Else
                MsgBox(0, 0, "Wrong!")
            EndIf
        Case $ButtonGenerate
            $Captcha = Captcha($idPic, $hGUI)
    EndSwitch
WEnd

Func Captcha($iCtrlID, $hGUI, $characters = 7)
    Local $aPos = ControlGetPos($hGUI, "", $iCtrlID)
    If @error Then Return SetError(1, 0, 0)
    Local $iW = $aPos[2], $iH = $aPos[3]
    Local $aBrush[8] = [0xFFFF0000, 0xFFFF8000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FF00, 0xFFFF00FF, 0xFF0000FF, 0xFF008080]
    Local $aStyle[4] = [0, 1, 2, 4]
    Local $aFont[4] = ['Arial', 'Courier New', 'Tahoma', 'Comic Sans MS']
    Local $aBack[5] = [0xFFA0A0FF, 0xFFA0FFA0, 0xFFA08000, 0xFFC0C0C0, 0xFFFFFFFF]
    Local $CharSet = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', '')
    Local $sCaptcha, $aResult, $i, $sNewChar, $iSize, $iPrevSize

    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    Local Const $hBitmap = $aResult[6]
    Local Const $hGfxContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
     _GDIPlus_GraphicsSetSmoothingMode($hGfxContext, 2)
     DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hGfxContext, "int", 4)
    Local $hPen = _GDIPlus_PenCreate(Random(0xA0BB0000, 0xC0FFFFFF, 1), Random(1, 3, 1))
    Local Const $iMaxFontWidth = Int($iW / (($characters + 4) * 0.8))
    Local Const $iMinFontWidth = Int($iW / (($characters + 7) * 0.8))
    _GDIPlus_GraphicsClear($hGfxContext, $aBack[Random(0, UBound($aBack) - 1, 1)])
    $iPrevSize = $iMinFontWidth
    For $i = 1 To $characters
        _GDIPlus_GraphicsDrawLine($hGfxContext, Random(0, 30, 1), Random(0, 100, 1), Random(200, 250, 1), Random(0, 100, 1), $hPen)
        _GDIPlus_PenSetColor($hPen, Random(0xA0BB0000, 0xC0FFFFFF, 1))
        _GDIPlus_PenSetWidth($hPen, Random(1, 3, 1))
        _GDIPlus_GraphicsDrawLine($hGfxContext, Random(0, $iW, 1), Random(0, 4, 1), Random(0, $iW, 1), Random($iH / 3, $iH, 2), $hPen)

        $sNewChar = $CharSet[Random(1, $CharSet[0], 1)]
        $iSize = Random($iMinFontWidth, $iMaxFontWidth, 1)
        _GDIPlus_GraphicsDrawString2($hGfxContext,  $sNewChar, _
                                                                                            $aFont[Random(0, UBound($aFont) - 1, 1)], _
                                                                                            $iSize, _
                                                                                            0, _
                                                                                            $aBrush[Random(0, UBound($aBrush) - 1, 1)], _
                                                                                            0, _
                                                                                            ($i - 1) * $iMaxFontWidth + $iPrevSize, _
                                                                                            Random(4, $iH - $iSize * 1.5, 1))

        $iPrevSize = $iSize
        $sCaptcha &= $sNewChar
    Next

    Local Const $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    Local Const $hHBmp = GUICtrlSendMsg($iCtrlID, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap)
    If $hHBmp Then _WinAPI_DeleteObject($hHBmp)
    _WinAPI_DeleteObject($hHBitmap)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hGfxContext)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sCaptcha = ' & $sCaptcha & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    Return $sCaptcha
EndFunc   ;==>Captcha

Func _GDIPlus_GraphicsDrawString2($hGraphics, $sString, $sFont = "Arial", $nSize = 10, $iFormat = 0, $iColor = 0xFFFF0000, $iAlign = 1, $nX = 0, $nY = 0)
    Local $hBrush = _GDIPlus_BrushCreateSolid($iColor)
    Local $hFormat = _GDIPlus_StringFormatCreate($iFormat)
    _GDIPlus_StringFormatSetAlign($hFormat, $iAlign)
    Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    Local $hFont = _GDIPlus_FontCreate($hFamily, $nSize)
    Local $tLayout = _GDIPlus_RectFCreate($nX, $nY, 0, 0)
    Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $tLayout, $hFormat)
    Local $aResult = _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
    Local $iError = @error
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    Return SetError($iError, 0, $aResult)
EndFunc   ;==>_GDIPlus_GraphicsDrawString2

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

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
 Share

  • Recently Browsing   0 members

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