Jump to content

generate a picture from text


Kilhian
 Share

Recommended Posts

Hi,

I'm trying to generate a jpg file, filed from text typed in the gui..

preview works.. but it doesn't generate the file...

Thanks to those could help me :)

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#include <GUIConstants.au3>

;Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)
$iWidth="200"
$iHeight="300"
$List_Titre = ("Avocat à la Cour|Avocat - Associé|Assistante|Consultant")
$adress="61,street name"&@CRLF&"city"
$Fax="Fax."&Chr(09)&"555 x xx xx xx xx"

$GUI = GUICreate("Signature Generator", 179, 313, 193, 125)
$Prenom = GUICtrlCreateLabel("Prénom", 8, 64, 40, 17)
$nom = GUICtrlCreateLabel("Nom", 8, 96, 26, 17)
$tel = GUICtrlCreateLabel("Tél", 8, 128, 19, 17)
$input_prenom = GUICtrlCreateInput("", 48, 60, 121, 21)
$Input_nom = GUICtrlCreateInput("", 48, 92, 121, 21)
$Input_tel = GUICtrlCreateInput("555 x xx xx xx xx", 48, 124, 121, 21)
$choix_titre = GUICtrlCreateCombo("...", 8, 32, 161, 25)
GUICtrlSetData($choix_titre, $List_Titre, "...")
$Button_prev = GUICtrlCreateButton("Preview", 8, 244, 75, 25, 0)
$Button_cancel = GUICtrlCreateButton("Cancel", 96, 244, 75, 25, 0)
$Checkbox_bb = GUICtrlCreateCheckbox("Blackberry", 8, 156, 73, 17)
$Input_BB = GUICtrlCreateInput("+33 6", 48, 188, 113, 21)
$Label5 = GUICtrlCreateLabel("N°", 16, 192, 16, 17)
$Titre = GUICtrlCreateLabel("Title", 8, 10, 36, 20)
$Button_launch = GUICtrlCreateButton("Launch", 8, 276, 163, 25, 0)
$Checkbox_sign = GUICtrlCreateCheckbox("Signature manuelle", 8, 224, 113, 17)
GUISetState(@SW_SHOW)


GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
GUICtrlSetOnEvent($Button_launch, "Button_launchClick")
GUICtrlSetOnEvent($Checkbox_sign, "Checkbox_signClick")
GUICtrlSetOnEvent($Checkbox_bb, "Checkbox_bbClick")
GUICtrlSetOnEvent($Button_cancel, "Button_cancelClick")
GUICtrlSetOnEvent($Button_prev, "Button_prevClick")
GUICtrlSetOnEvent($choix_titre, "choix_titreChange")


While 1
    Sleep(100)
WEnd

Func Button_cancelClick()

EndFunc
Func Checkbox_bbClick()

EndFunc
Func Checkbox_signClick()

EndFunc
Func choix_titreChange()

EndFunc
Func Form1Close()
    SplashOff()
    Exit
EndFunc
Func Form1Maximize()

EndFunc
Func Form1Minimize()

EndFunc
Func Form1Restore()

EndFunc



Func Button_prevClick()
    Global $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout,$hImage
    $sign=GUICtrlRead($input_prenom)&" "&GUICtrlRead($Input_nom)&@CRLF&GUICtrlRead($choix_titre)&@CRLF&@CRLF&$adress&@CRLF&@CRLF&"Tél."&Chr(09)&GUICtrlRead($Input_tel)&@CRLF&$Fax

    ; Create GUI
    $hGUI = GUICreate("Preview", $iWidth, $iHeight)
    $hWnd = WinGetHandle("Preview")
    GUISetOnEvent($GUI_EVENT_CLOSE, "close")

    GUISetState()

    ; Draw a string
    _GDIPlus_Startup ()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    $Bitmap=_GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphic)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($Bitmap)
    $hBrush = _GDIPlus_BrushCreateSolid (0xFFAA0000)
    $hFormat = _GDIPlus_StringFormatCreate ()
    $hFamily = _GDIPlus_FontFamilyCreate ("Arial")
    $hFont = _GDIPlus_FontCreate ($hFamily, 12, 0)
    $tLayout = _GDIPlus_RectFCreate (5, 5, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sign, $hFont, $tLayout, $hFormat, $hBrush)
EndFunc   ;==>_Main
Func Button_launchClick()
    _GDIPlus_ImageSaveToFile ($hImage, @MyDocumentsDir & "\test.jpg")
EndFunc   ;==>_Main

Func Close()
        ; Clean up resources
    _GDIPlus_FontDispose ($hFont)
    _GDIPlus_FontFamilyDispose ($hFamily)
    _GDIPlus_StringFormatDispose ($hFormat)
    _GDIPlus_BrushDispose ($hBrush)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()
    $hnd2close = WinGetHandle("[active]", "")
    GUIDelete($hnd2close)
EndFunc
Link to comment
Share on other sites

I'm not sure what you're trying to do (I didn't run your script).

-Are you trying to make a .jpg that has text written into it, as if you'd typed the text into MSPaint and saved the picture? ImageMagick is the only way I know of to do this in AutoIt (though I'm sure there are others) - I've got a 12mb udf that lets you plug it into AutoIt for download in this topic. (when you compile your script, it compresses to about 30% of this size) imagemagick.org will give you the syntax and examples you need - if you have

#include ImageMagickObject.dll.au3
Global $oIM=ObjCreate("ImageMagickObject.MagickImage.1")
at the top of your script, then you can use $oIM.Convert("annotate") to actually write the text into the file - use the examples at imagemagick.org to learn how to use gravity to 'justify' and place your text as you want.

-Are you trying to make a kind of asciimator? There are many examples of doing that on the forums.

Hope this helps.

Edit: I read the code a little more closely, and realized that you weren't trying to find out HOW to do this, you were trying to troubleshoot something that should already be working. Sorry for misunderstanding. I didn't even realize the GDI stuff could do this, so I learned something too! I'll step aside and let someone who knows those functions help you now.

Edited by james3mg
"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
Link to comment
Share on other sites

Hi, I'm trying to make a .jpg that has text written into it, as if I'd typed the text into MSPaint and saved the picture.

GDI component seems to do this... and it works if I use a blank jpg file as template... but i'm trying to do without the template file

Link to comment
Share on other sites

I tried to use GDI+ commands and gave up. I am still learning.

I cheated and used _ScreenCapture_CaptureWnd. See your modified script.

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

 Global $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout,$hImage, $Bitmap, $iWidth=200, $iHeight= 300
;Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)

$List_Titre = ("Avocat à la Cour|Avocat - Associé|Assistante|Consultant")
$adress="61,street name"&@CRLF&"city"
$Fax="Fax."&Chr(09)&"555 x xx xx xx xx"

$GUI = GUICreate("Signature Generator", 179, 313, 193, 125)
$Prenom = GUICtrlCreateLabel("Prénom", 8, 64, 40, 17)
$nom = GUICtrlCreateLabel("Nom", 8, 96, 26, 17)
$tel = GUICtrlCreateLabel("Tél", 8, 128, 19, 17)
$input_prenom = GUICtrlCreateInput("", 48, 60, 121, 21)
$Input_nom = GUICtrlCreateInput("", 48, 92, 121, 21)
$Input_tel = GUICtrlCreateInput("555 x xx xx xx xx", 48, 124, 121, 21)
$choix_titre = GUICtrlCreateCombo("...", 8, 32, 161, 25)
GUICtrlSetData($choix_titre, $List_Titre, "...")
$Button_prev = GUICtrlCreateButton("Preview", 8, 244, 75, 25, 0)
$Button_cancel = GUICtrlCreateButton("Cancel", 96, 244, 75, 25, 0)
$Checkbox_bb = GUICtrlCreateCheckbox("Blackberry", 8, 156, 73, 17)
$Input_BB = GUICtrlCreateInput("+33 6", 48, 188, 113, 21)
$Label5 = GUICtrlCreateLabel("N°", 16, 192, 16, 17)
$Titre = GUICtrlCreateLabel("Title", 8, 10, 36, 20)
$Button_launch = GUICtrlCreateButton("Launch", 8, 276, 163, 25, 0)
$Checkbox_sign = GUICtrlCreateCheckbox("Signature manuelle", 8, 224, 113, 17)
GUISetState(@SW_SHOW)


GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
GUICtrlSetOnEvent($Button_launch, "Button_launchClick")
GUICtrlSetOnEvent($Checkbox_sign, "Checkbox_signClick")
GUICtrlSetOnEvent($Checkbox_bb, "Checkbox_bbClick")
GUICtrlSetOnEvent($Button_cancel, "Button_cancelClick")
GUICtrlSetOnEvent($Button_prev, "Button_prevClick")
GUICtrlSetOnEvent($choix_titre, "choix_titreChange")


While 1
    Sleep(100)
WEnd

Func Button_cancelClick()

EndFunc
Func Checkbox_bbClick()

EndFunc
Func Checkbox_signClick()

EndFunc
Func choix_titreChange()

EndFunc
Func Form1Close()
    SplashOff()
    Exit
EndFunc
Func Form1Maximize()

EndFunc
Func Form1Minimize()

EndFunc
Func Form1Restore()

EndFunc



Func Button_prevClick()
   
    $sign=GUICtrlRead($input_prenom)&" "&GUICtrlRead($Input_nom)&@CRLF&GUICtrlRead($choix_titre)&@CRLF&@CRLF&$adress&@CRLF&@CRLF&"Tél."&Chr(09)&GUICtrlRead($Input_tel)&@CRLF&$Fax

    ; Create GUI
    $hGUI = GUICreate("Preview", $iWidth, $iHeight)
    GUISetBkColor(0xFFFFFF)
    $hWnd = WinGetHandle("Preview")
    GUISetOnEvent($GUI_EVENT_CLOSE, "close")

    GUISetState()

    ; Draw a string
    _GDIPlus_Startup ()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    $Bitmap =_GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphic)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($Bitmap)
    $hBrush = _GDIPlus_BrushCreateSolid (0xFFAA0000)
    $hFormat = _GDIPlus_StringFormatCreate ()
    $hFamily = _GDIPlus_FontFamilyCreate ("Arial")
    $hFont = _GDIPlus_FontCreate ($hFamily, 12, 0)
    $tLayout = _GDIPlus_RectFCreate (6, 6, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sign, $hFont, $tLayout, $hFormat, $hBrush)
    
EndFunc   ;==>_Main
Func Button_launchClick()
    
    ;_ScreenCapture_CaptureWnd($sFileName, $hWnd[, $iLeft = 0[, $iTop = 0[, $iRight = -1[, $iBottom = -1[, $fCursor = True]]]]])
     _ScreenCapture_CaptureWnd (@MyDocumentsDir & "\GDIPlus_Image.jpg", $hGUI,2,22,    200,            300,            False)
    
    ;_GDIPlus_ImageSaveToFile ($hImage, @MyDocumentsDir & "\test.jpg")
EndFunc   ;==>_Main

Func Close()
        ; Clean up resources
    _GDIPlus_FontDispose ($hFont)
    _GDIPlus_FontFamilyDispose ($hFamily)
    _GDIPlus_StringFormatDispose ($hFormat)
    _GDIPlus_BrushDispose ($hBrush)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_ImageDispose ($hImage)
    _GDIPlus_ImageDispose ($Bitmap)
    _GDIPlus_Shutdown ()
    $hnd2close = WinGetHandle("[active]", "")
    GUIDelete($hnd2close)
EndFunc

Hope you succeed.

If you do, please post the solution

Link to comment
Share on other sites

just a quick example;

#include <Prospeed.au3>

Opt("GUIOnEventMode", 1)
HotKeySet("{Esc}","_Exit")

GUICreate("", 800, 600)
$input = GUICtrlCreateInput("",50,50,200,20)
GUICtrlCreateButton("Generate",50,100,60,30)
GUICtrlSetOnEvent(-1,"generate")
GUISetState()

GetHDC()

While 1
    Sleep(1000)
WEnd

Func generate()
    $getInput = GUICtrlRead($input)
    $bitmap = CreateExtBmp(100, 20)
    SetText($bitmap, $getInput, 0, 0, 0xFFFFFF, 0x000000, 20, 500, "Arial")
    
    CopyExtBmp($hdc, 50, 150, 100, 20, $bitmap, 0, 0, 0)
    SaveExtImage($bitmap, "C:\text_bitmap.bmp", 0, 0)
    FreeExtBmp($bitmap)
EndFunc

Func SetText($S_Destination, $S_Text, $S_x, $S_y, $S_TextColor, $S_BKColor, $Font_H, $Font_W, $Font_Name)
    $font = DllCall("gdi32.dll", "int", "CreateFontA", _
                                 "int", $Font_H, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", $Font_W, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", 0, _
                                 "int", 0, _
                                 "str", $Font_Name)
    $oldfont = DllCall("gdi32.dll", "int", "SelectObject", _
                                    "hwnd", $S_Destination, _
                                    "int",  $font[0])   
    DllCall("gdi32.dll", "int",  "SetTextColor", _
                         "hwnd", $S_Destination, _
                         "int",  $S_TextColor)
    DllCall("gdi32.dll", "int",  "SetBkColor", _
                         "hwnd", $S_Destination, _
                         "int",  $S_BKColor)
    DllCall("gdi32.dll", "int",  "TextOutA", _
                         "hwnd", $S_Destination, _
                         "int",  $S_x, _
                         "int",  $S_y, _
                         "str",  $S_Text, _
                         "int",  StringLen($S_Text))    
    DllCall("gdi32.dll", "int",  "selectObject", _
                         "hwnd", $S_Destination, _
                         "int",  $oldfont[0])
    DllCall("gdi32.dll", "int", "DeleteObject", _
                         "hwnd", $font[0])
EndFunc
                     
Func _exit()
    Exit
EndFunc
Link to comment
Share on other sites

just a quick example;

I could not get your example to work.

Fatal Error window appears with "AVector:[]:Out of bounds."

In the Scite debug window appears:

>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Program Files\Autoio\Examples\Prospeed\textinput.au3"

C:\Program Files\AutoIt3\Include\Prospeed.au3 (210) : ==> Subscript used with non-Array variable.:

Return $C_createBMP[0]

Return $C_createBMP^ ERROR

->10:11:03 AutoIT3.exe ended.rc:1

+>10:11:04 AutoIt3Wrapper Finished

>Exit code: 1 Time: 136.682

Any ideas?

I have downloaded the new Prospeed.au3 @

http://www.autoitscript.com/forum/index.ph...st&p=480706

Edited by Malkey
Link to comment
Share on other sites

I could not get your example to work.

Fatal Error window appears with "AVector:[]:Out of bounds."

In the Scite debug window appears:

>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Program Files\Autoio\Examples\Prospeed\textinput.au3"

C:\Program Files\AutoIt3\Include\Prospeed.au3 (210) : ==> Subscript used with non-Array variable.:

Return $C_createBMP[0]

Return $C_createBMP^ ERROR

->10:11:03 AutoIT3.exe ended.rc:1

+>10:11:04 AutoIt3Wrapper Finished

>Exit code: 1 Time: 136.682

Any ideas?

I have downloaded the new Prospeed.au3 @

http://www.autoitscript.com/forum/index.ph...st&p=480706

i've put prospeed.dll in @dirscript and it works fine.

colors are in BGR

change 0 to 1 in the line below

SaveExtImage($bitmap, "C:\text_bitmap.bmp", 1, 0)

@jpam Thanks a lot :)

Edited by Kilhian
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...