Home > database >  Powershell insert text in Hebrew to an image
Powershell insert text in Hebrew to an image

Time:11-21

I’m trying to make a signature maker and I want to write name and job in Hebrew but it starts from the left like English. There is a way that the text will start from the right?

This is my code for example:

Add-Type -AssemblyName System.Drawing
$uname='סער'#Read-Host "insert name:"
$job='יינחןינצל'#Read-Host "insert job:"
$filename = "$home\desktop\sign.png" 
$bmp = new-object System.Drawing.Bitmap 600,200

#Get the image
#$source=Get-Item 
#$img = [System.Drawing.Image]::FromFile($_.FullName)
#Create a bitmap
#$bmp = new-object System.Drawing.Bitmap([int]($img.width)),([int]($img.height))

$font = new-object System.Drawing.Font Consolas,18
$brushBg = [System.Drawing.Brushes]::White 
$brushFg = [System.Drawing.Brushes]::Black 
$graphics = [System.Drawing.Graphics]::FromImage($bmp) 
$graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height) 
$graphics.DrawString($uname,$font,$brushFg,10,10)
$graphics.DrawString($job,$font,$brushFg,90,100)
$graphics.Dispose() 
$bmp.Save($filename) 

Invoke-Item $filename

CodePudding user response:

As commented, the DrawString() method has a constructor with which you can give it a StringFormat object where you can set RightToLeft text direction.

In your code try:

$rtlFormat = [System.Drawing.StringFormat]::new([System.Drawing.StringFormatFlags]::DirectionRightToLeft)
$graphics.DrawString($uname, $font, $brushFg, [System.Drawing.PointF]::new(10,10), $rtlFormat)
$graphics.DrawString($job, $font, $brushFg, [System.Drawing.PointF]::new(90,100), $rtlFormat)

If you are using a PowerShell version that is too old for the [type]::new() syntax, use

$rtlFormat = New-Object -TypeName System.Drawing.StringFormat('DirectionRightToLeft')
$graphics.DrawString($uname, $font, $brushFg, (New-Object -TypeName System.Drawing.PointF(10,10)), $rtlFormat)
$graphics.DrawString($job, $font, $brushFg, (New-Object -TypeName System.Drawing.PointF(90,100)), $rtlFormat)

CodePudding user response:

$sf = [System.Drawing.StringFormat]::New()
$sf.Alignment = 'far'
$sf.LineAlignment = 'far'
$graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height) 
$graphics.DrawString($uname,$font,$brushFg,500,100,$sf)
$graphics.DrawString($job,$font,$brushFg,500,200,$sf)

This is what I did in the end Thaks Theo

  • Related