我正在尝试制作一个签名生成器,我想用希伯来语写名字和工作,但它像英语一样从左边开始。有一种方法可以让文本从右边开始?
这是我的代码,例如:
Add-Type -AssemblyName System.Drawing
$uname='סער'#Read-Host "insert name:"
$job='יינחןינצל'#Read-Host "insert job:"
$filename = "$homedesktopsign.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
如前所述,DrawString()
方法有一个构造函数,使用该构造函数可以为其提供StringFormat对象,在该对象中可以设置RightToLeft文本方向。
在您的代码中尝试:
$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)
如果您使用的PowerShell版本对于[type]::new()
语法来说太旧,请使用
$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)
$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)
这就是我最终所做的Thaks Theo