Jump to content

Text to .jpg? How?


Recommended Posts

I'm very new at AutoIt and was hoping to get a bit of advice/help from the more advanced users out there. I need a simple script that will output any text I put in an $sString variable as a .jpg image with my choice of fonts and size, color and make the font in the image at least 600 pixels tall.

I've been playing around with the GDIPlus and have been able to figure out how to output text in a specific font, color and size to the screen, but I don't know how I should go about making the output to a .jpg instead.

Any help or suggestions would be appreciated. I've attached my sample script below.

Thanks,

#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
_Main()
Func _Main()
    Local $hGUI, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout
    Local $sString = "83", $aInfo
    ; Create GUI
    $hGUI = GUICreate("GDI+", 860, 605)
    GUISetState()
    ; Draw a string
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, 600, 0)
    $tLayout = _GDIPlus_RectFCreate(-165, -145, 0, 0)
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
      
    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    ; Clean up resources
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>_Main

UPDATE:

Here's where I'm at so far... Still have the image width variable issue...

#NoTrayIcon
#NoAutoIt3Execute
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/so
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
OnAutoItExitRegister( "Terminate" )
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
Global Const $IMAGE_BITMAP = 0
Global Const $STM_SETIMAGE = 0x172
Global $hImage, $Info, $Info2, $Avitar = False
Global $file_text = InputBox("Text Input to .JPG", "Type the text you want converted to a .jpg file below.  The image will be saved to your desktop:", "", "")
Global $text_count = StringLen($file_text)
Global $image_width = 595*($text_count)
$file_text_uppercase = StringUpper($file_text);option to make all text uppercase
Draw($file_text_uppercase,$image_width,600,10,@DesktopDir & "" & $file_text_uppercase & ".jpg")
Func Draw($sString,$iwidth,$iheight,$fontsize, $Path)
Local $hGraphic, $hBrush, $hFormat, _
   $hFamily, $hFont, $tLayout, $aInfo, _
   $hBitmap
   #region - Create Image Signiture -
   _GDIPlus_Startup()
 
   $Font_Size = "600"
   $hImage = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
   $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
   _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush)
   $hFormat = _GDIPlus_StringFormatCreate()
   $hFamily = _GDIPlus_FontFamilyCreate("Arial")
   $hFont = _GDIPlus_FontCreate($hFamily, $Font_Size, 0)
   $tLayout = _GDIPlus_RectFCreate(-134, -147, 0, 0)
   $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
   _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
   $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
   _WinAPI_DeleteObject($hBitmap)
   _GDIPlus_FontDispose($hFont)
   _GDIPlus_FontFamilyDispose($hFamily)
   _GDIPlus_StringFormatDispose($hFormat)
   _GDIPlus_BrushDispose($hBrush)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_ImageSaveToFile($hImage, $path)
   #endregion - Create Image Signiture -
Return 1
EndFunc   ;==>Draw
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
Func Terminate()
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
Exit
EndFunc   ;==>_Exit

gdi font sample script.au3

Edited by tbwalker
Link to comment
Share on other sites

Thanks... Just checked out the link.... guess I've got a lot more to learn.... cant make heads or tails of how to take that code and save text with no image to put it on... My problem is that I want to be able to output text to a .jpg file format in any variable length I desire, ultimately by having the script/program prompt me for the text input I want, the desired font, the font size, the font color.... and then from those inputs create a .jpg image with just the text in it, cropped tightly...

Thanks for the good start John.... I'll keep digging....

TBWalker

Edited by tbwalker
Link to comment
Share on other sites

You will need to just break it down, that link will help you add text to a bitmap.

next you need to know how to create a bitmap of the size you need.

I can only see in gdi funcs to create from file, bitmap and hbitmap, not just create.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

As a dirty suggestion, as part of your app you could have a blank white bitmap as a template.

it could then be resized to your requirements and used accordingly.

I'm not much help as I have no experience in this gdi voodoo stuff.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I'm no pro, but this sounds like what you need. Credits to whoever I took code from to make it.

#NoTrayIcon
#NoAutoIt3Execute
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/so
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
OnAutoItExitRegister( "Terminate" )
#include <GDIPlus.au3>
Global Const $IMAGE_BITMAP = 0
Global Const $STM_SETIMAGE = 0x172
Global $hImage, $Info, $Info2, $Avitar = False
Draw("Hello World.",250,250,10,@DesktopDir & "Imgname.jpg")
Func Draw($sString,$iwidth,$iheight,$fontsize, $Path)
Local $hGraphic, $hBrush, $hFormat, _
   $hFamily, $hFont, $tLayout, $aInfo, _
   $hBitmap
   #region - Create Image Signiture -
   _GDIPlus_Startup()
   $hImage = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
   $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
   _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush)
   $hFormat = _GDIPlus_StringFormatCreate()
   $hFamily = _GDIPlus_FontFamilyCreate("OCR A Extended")
   $hFont = _GDIPlus_FontCreate($hFamily, $FontSize, 0)
   $tLayout = _GDIPlus_RectFCreate(5, 5, 0, 0)
   $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFF37FF00)
   _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
   $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
   _WinAPI_DeleteObject($hBitmap)
   _GDIPlus_FontDispose($hFont)
   _GDIPlus_FontFamilyDispose($hFamily)
   _GDIPlus_StringFormatDispose($hFormat)
   _GDIPlus_BrushDispose($hBrush)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_ImageSaveToFile($hImage, $path)
   #endregion - Create Image Signiture -
Return 1
EndFunc   ;==>Draw
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
Func Terminate()
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
Exit
EndFunc   ;==>_Exit

Maybe you can take code from here to do whatever it is you're doing.

Edited by ApudAngelorum
Link to comment
Share on other sites

Thanks for the starter code ApudAngelorum . I took it and ran... and below is what I have so far. It works to output to JPG, the only snag I’m running into is dynamically calculating the image size based on the width of the text variable, its font and its height. I've hard coded "Arial" with a height of 600 for the font, and, using the uppercase letter H in that size to calculate width, I came up with a per-character width of 595.

I have my code doing a character count of the text input variable, and then I take number of characters and multiply it by 595 to set the image width. It works great when testing with the uppercase letter H, no matter how many H's I put in, but the character width fluctuates with different letters, so I don't know how I should calculate the proper image with since the variable changes and could be any number of characters. If anyone has any suggestions on how I can calculate my $image_width variable with the actual width of my text input it would be greatly appreciated.

If there is no way to calculate the pixel width of my text variable before I output it to an image, might there be code that can look at the image and crop off the excess white at the end of the result?

Thanks again!

#NoTrayIcon
#NoAutoIt3Execute
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/so
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
OnAutoItExitRegister( "Terminate" )
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
Global Const $IMAGE_BITMAP = 0
Global Const $STM_SETIMAGE = 0x172
Global $hImage, $Info, $Info2, $Avitar = False
Global $file_text = InputBox("Text Input to .JPG", "Type the text you want converted to a .jpg file below.  The image will be saved to your desktop:", "", "")
Global $text_count = StringLen($file_text)
Global $image_width = 595*($text_count)
$file_text_uppercase = StringUpper($file_text);option to make all text uppercase

Draw($file_text_uppercase,$image_width,600,10,@DesktopDir & "\" & $file_text_uppercase & ".jpg")
Func Draw($sString,$iwidth,$iheight,$fontsize, $Path)
Local $hGraphic, $hBrush, $hFormat, _
   $hFamily, $hFont, $tLayout, $aInfo, _
   $hBitmap
   #region - Create Image Signiture -
   _GDIPlus_Startup()
  
   $Font_Size = "600"
   $hImage = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
   $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
   _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush)
   $hFormat = _GDIPlus_StringFormatCreate()
   $hFamily = _GDIPlus_FontFamilyCreate("Arial")
   $hFont = _GDIPlus_FontCreate($hFamily, $Font_Size, 0)
   $tLayout = _GDIPlus_RectFCreate(-134, -147, 0, 0)
   $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
   _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
   $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
   _WinAPI_DeleteObject($hBitmap)
   _GDIPlus_FontDispose($hFont)
   _GDIPlus_FontFamilyDispose($hFamily)
   _GDIPlus_StringFormatDispose($hFormat)
   _GDIPlus_BrushDispose($hBrush)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_ImageSaveToFile($hImage, $path)
   #endregion - Create Image Signiture -
Return 1
EndFunc   ;==>Draw
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
Func Terminate()
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

You might also try using ImageMagick. It has very good text annotaion functions. For one example, I use it to create temporary book cover images for my ebook collection. It takes an existing image of a book with no title, then adds the title text ($Caption), resized to fit within the image size, then saves it as a .JPG file ($TargetFile).

Func MakeCover($TargetFile, $Caption)
    Local Const $ImageMagick = "C:Program FilesImageMagickconvert.exe" ;not the way I really point to it, but simplified for this example
    If FileExists($ImageMagick) Then
        Local $BaseBook = "BlankBook.jpg"
        RunWait($ImageMagick & ' -background none -font Arial -gravity center -size 135x250 caption:"' & $Caption & '" +size "' & @TempDir & "" & $BaseBook & '" +swap -gravity East -geometry +5+0 -composite "' & $TargetFile & '"', @ScriptDir, @SW_HIDE)
    Else
        FileInstall("noimage.jpg", $TargetFile)
    EndIf
EndFunc   ;==>MakeCover

This example actually chains multiple operations to copy the image, limit the "text" area to a smaller region, then shift it to an offset, the merge the text with the original image then save to the target file. Your operation will probably be a much simpler one.

Edited by willichan
Link to comment
Share on other sites

You might also try using ImageMagick.

Unless the user plans on distributing this script, that wouldn't be a good idea unless he doesn't mind having to tell users that they will need to install a 20+ mb com object or somehow include the imagemagic installer into his app, I tried and failed, I also didn't really try too hard to make a portable version since it was going to increase the size of the app by about 10 mb.

But don't get me wrong, image magic is pretty cool.

Edited by ApudAngelorum
Link to comment
Share on other sites

I'm digging through the info right now in hopes of seeing how i can get it to measure the pixel length of my text variable. In a nutshell, I want to be able to type a word or phrase into my script inputbox and have the script calculate the maximum image size needed (in pixels) for the text variable (currently set at Arial with size 600).

I ultimately plan on giving this script to my brother to use in his business. He does bulk ordering of sports jersey's and has a vendor cut and ship him vinyl transfers for the jersey's that he uses on his heat press. His vendor requires that he email him the vinyl cutting orders in the font of his choosing and at least 600 pixels tall, so right now, when he has a bulk order, I go into a graphics editing program, create the text and number layouts with the font set at 600, then crop and export the text as .jpgs.

I have this program doing 90% of what I ultimately want, which is outputting the text directly to a .jpg in the 600 font size i want, it's just setting the width of the output image to match the actual pixel size of the text variable at size 600 font that I’m lacking.

hopefully I'll be able to learn something by digging through the StringSize code.

Thanks again for everyone’s help!

TBWalker

#NoTrayIcon
#NoAutoIt3Execute
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/so
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
OnAutoItExitRegister( "Terminate" )
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
Global Const $IMAGE_BITMAP = 0
Global Const $STM_SETIMAGE = 0x172
Global $hImage, $Info, $Info2, $Avitar = False
Global $file_text = InputBox("Text Input to .JPG", "Type the text you want converted to a .jpg file below.  The image will be saved to your desktop:", "", "")
Global $text_count = StringLen($file_text)
Global $image_width = 595*($text_count)
$file_text_uppercase = StringUpper($file_text);option to make all text uppercase

Draw($file_text_uppercase,$image_width,600,10,@DesktopDir & "\" & $file_text_uppercase & ".jpg")
Func Draw($sString,$iwidth,$iheight,$fontsize, $Path)
Local $hGraphic, $hBrush, $hFormat, _
   $hFamily, $hFont, $tLayout, $aInfo, _
   $hBitmap
   #region - Create Image Signiture -
   _GDIPlus_Startup()
  
   $Font_Size = "600"
   $hImage = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
   $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
   _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush)
   $hFormat = _GDIPlus_StringFormatCreate()
   $hFamily = _GDIPlus_FontFamilyCreate("Arial")
   $hFont = _GDIPlus_FontCreate($hFamily, $Font_Size, 0)
   $tLayout = _GDIPlus_RectFCreate(-134, -147, 0, 0)
   $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
   $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
   _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
   $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
   _WinAPI_DeleteObject($hBitmap)
   _GDIPlus_FontDispose($hFont)
   _GDIPlus_FontFamilyDispose($hFamily)
   _GDIPlus_StringFormatDispose($hFormat)
   _GDIPlus_BrushDispose($hBrush)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_ImageSaveToFile($hImage, $path)
   #endregion - Create Image Signiture -
Return 1
EndFunc   ;==>Draw
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
Func Terminate()
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

I'm digging through the info right now in hopes of seeing how i can get it to measure the pixel length of my text variable. In a nutshell,

$mySize = _StringSize("This Text in a rectangle")

ConsoleWrite("Width = " & $mySize[2] & @LF)

ConsoleWrite("Height = " & $mySize[3] & @LF)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Just a thing to note, there are functions to make the graphic quality of the image look better, but why does it have to be a jpg?

Do this, make an image.jpg and another one image.png and look at the difference.

The jpg looks like crap.

Edited by ApudAngelorum
Link to comment
Share on other sites

I'm good with a PNG or a JPG. Image format is really not my problem at this point (I do agree that PNG is higher quality, but the vendor's equipment is a razor vinyl cutter that rasterizes the image we send to do a cut, so as long as the image has somewhat of a straight line, his software will clean the line and do a near perfect cut). I really want to get this image output width dynamic setting thingy fixed... thats my sticking point right now.... once I get past that, I'll probably add options like font selection, font size selection, image output format selection, etc.... just this one stumbling block....

Thanks,

TBWalker

Link to comment
Share on other sites

I do not know about a font size of 600, is there even that size, fonts in that UDF are in points.

The default is 8.5

so if you wanted 12 it would be

$mySize = _StringSize("This Text in a rectangle", 12)

EDIT:

If you wanted size 14 using the wibble font then

$mySize = _StringSize("This Text in a rectangle", 14, 400, 0, "wibble")

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Just for the heck of it, here is another one that lets you control the quality of the jpg images created.

#NoTrayIcon
#NoAutoIt3Execute

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/so
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

OnAutoItExitRegister("Terminate")

#include <GDIPlus.au3>

_GDIPlus_Startup()

Draw("Hello World.", 250, 250, 10, @DesktopDir & "Test Image.jpg",100); 0 minimum 100 maximum

Func Draw($sString, $iwidth, $iheight, $fontsize, $Path, $Quality = 100)

Local $hImage = _GDIPlus_BitmapCreateFromScan0($iwidth, $iheight)
Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
_GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iwidth, $iheight, $hBrush)
Local $hFormat = _GDIPlus_StringFormatCreate()
Local $hFamily = _GDIPlus_FontFamilyCreate("OCR A Extended")
Local $hFont = _GDIPlus_FontCreate($hFamily, $fontsize, 0)
Local $tLayout = _GDIPlus_RectFCreate(5, 5, 0, 0)
Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF37FF00)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
Local $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_GraphicsDispose($hGraphic)

Local $Split = StringSplit($Path,".")
Local $Param
Local $sCLSID

If StringLower($Split[$Split[0]]) == "jpg" Then ; Set JPG quality
Local $tData = DllStructCreate("int Data")
DllStructSetData($tData, "Data", $Quality)
Local $tParams = _GDIPlus_ParamInit(1)
_GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Data"))
$Param = DllStructGetPtr($tParams)
$sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
Else
$sCLSID = _GDIPlus_EncodersGetCLSID($Split[$Split[0]])
$Param = 0
EndIf

_GDIPlus_ImageSaveToFileEx($hImage, $Path, $sCLSID, $Param)
_GDIPlus_ImageDispose($hImage)

Return 1
EndFunc   ;==>Draw

; #FUNCTION# ====================================================================================================================
; Name...........: _GDIPlus_BitmapCreateFromScan0
; Description ...: Creates a Bitmap object based on an array of bytes along with size and format information
; Syntax.........: _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight[, $iStride = 0[, $iPixelFormat = 0x0026200A[, $pScan0 = 0]]])
; Parameters ....: $iWidth      - The bitmap width, in pixels
;                  $iHeight     - The bitmap height, in pixels
;                  $iStride     - Integer that specifies the byte offset between the beginning of one scan line and the next. This
;                  +is usually (but not necessarily) the number of bytes in the pixel format (for example, 2 for 16 bits per pixel)
;                  +multiplied by the width of the bitmap. The value passed to this parameter must be a multiple of four
;                  $iPixelFormat - Specifies the format of the pixel data. Can be one of the following:
;                  |$GDIP_PXF01INDEXED   - 1 bpp, indexed
;                  |$GDIP_PXF04INDEXED   - 4 bpp, indexed
;                  |$GDIP_PXF08INDEXED   - 8 bpp, indexed
;                  |$GDIP_PXF16GRAYSCALE - 16 bpp, grayscale
;                  |$GDIP_PXF16RGB555    - 16 bpp; 5 bits for each RGB
;                  |$GDIP_PXF16RGB565    - 16 bpp; 5 bits red, 6 bits green, and 5 bits blue
;                  |$GDIP_PXF16ARGB1555  - 16 bpp; 1 bit for alpha and 5 bits for each RGB component
;                  |$GDIP_PXF24RGB       - 24 bpp; 8 bits for each RGB
;                  |$GDIP_PXF32RGB       - 32 bpp; 8 bits for each RGB. No alpha.
;                  |$GDIP_PXF32ARGB      - 32 bpp; 8 bits for each RGB and alpha
;                  |$GDIP_PXF32PARGB     - 32 bpp; 8 bits for each RGB and alpha, pre-mulitiplied
;                  $pScan0      - Pointer to an array of bytes that contains the pixel data. The caller is responsible for
;                  +allocating and freeing the block of memory pointed to by this parameter.
; Return values .: Success      - Returns a handle to a new Bitmap object
;                  Failure      - 0 and either:
;                  |@error and @extended are set if DllCall failed
;                  |$GDIP_STATUS contains a non zero value specifying the error code
; Remarks .......: After you are done with the object, call _GDIPlus_ImageDispose to release the object resources
; Related .......: _GDIPlus_ImageDispose
; Link ..........; @@MsdnLink@@ GdipCreateBitmapFromScan0
; Example .......; Yes
; ===============================================================================================================================
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0

Func Terminate()
_GDIPlus_Shutdown()
Exit
EndFunc   ;==>Terminate
Edited by ApudAngelorum
Link to comment
Share on other sites

  • Moderators

tbwalker,

You asked me to look in re using StringSize to size your 600 point text. The UDF seems to work fine with a such a large font size - this script:

#include <StringSize.au3>
#include <Array.au3>

$aArray = _StringSize("Testing_Long", 12)

_ArrayDisplay($aArray)

$aArray = _StringSize("Testing_Long", 600)

_ArrayDisplay($aArray)

gives sizes of 92x21 and 4667x1064 - both dimensions are pretty close to 50 times bigger which is what you would expect. So you should be able to use the output with some confidence. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...