gpt4 book ai didi

POWERSHELL:$Image.DrawString 居中字符串,左右填充

转载 作者:行者123 更新时间:2023-12-02 05:16:19 26 4
gpt4 key购买 nike

我正在尝试在垂直中间的图像上绘制字符串,但我不知道如何从该文本的左侧和右侧填充。如果我有很大的字符串,它不适合图像,而不是换行......

我现在有了第二个 powershell 代码:

$swp_curr_dir = split-path -parent $MyInvocation.MyCommand.Definition
[void][reflection.assembly]::loadwithpartialname("system.windows.forms")

Function AddTextToImage {
# Orignal code from http://www.ravichaganti.com/blog/?p=1012
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true)][String] $sourcePath,
[Parameter(Mandatory=$true)][String] $destPath,
[Parameter(Mandatory=$true)][String] $Title,
[Parameter()][String] $Description = $null
)

Write-Verbose "Load System.Drawing"
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

Write-Verbose "Get the image from $sourcePath"
$srcImg = [System.Drawing.Image]::FromFile($sourcePath)

Write-Verbose "Create a bitmap as $destPath"
$outputIImg = new-object System.Drawing.Bitmap([int]($srcImg.width)),([int]($srcImg.height))

Write-Verbose "Intialize Graphics"
$Image = [System.Drawing.Graphics]::FromImage($outputIImg)
$Image.SmoothingMode = "AntiAlias"

$Rectangle = New-Object Drawing.Rectangle 0, 0, $srcImg.Width, $srcImg.Height
$Image.DrawImage($srcImg, $Rectangle, 0, 0, $srcImg.Width, $srcImg.Height, ([Drawing.GraphicsUnit]::Pixel))

Write-Verbose "Draw title: $Title"
$Font = new-object System.Drawing.Font("Bauhaus 93", 130, "Bold","Pixel")

#get font size
$font_size = [System.Windows.Forms.TextRenderer]::MeasureText($Title, $Font)
$font_size_width = $font_size.Width
$font_size_height = $font_size.Height

# calc text in middle
$text_in_middle_top_offset = ($srcImg.Height - $font_size_height) / 2
$text_in_middle_left_offset = ($srcImg.Width - $font_size_width) / 2

#styling font
$Brush = New-Object Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 255, 255, 255))

#lets draw font
$Image.DrawString($Title, $Font, $Brush, $text_in_middle_left_offset, $text_in_middle_top_offset)

Write-Host $text_in_middle_left_offset



Write-Verbose "Save and close the files"
$outputIImg.save($destPath, [System.Drawing.Imaging.ImageFormat]::jpeg)
$outputIImg.Dispose()
$srcImg.Dispose()
}

AddTextToImage -sourcePath ($swp_curr_dir + "\image.jpg") -destPath ($swp_curr_dir + "\output.jpg") -Title "Some long long long long long long long long long long long long string here"

cmd /c pause

最佳答案

我稍微改变了你的例子:

[void][reflection.assembly]::loadwithpartialname("system.windows.forms")

Function AddTextToImage {
# Orignal code from http://www.ravichaganti.com/blog/?p=1012
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true)][String] $sourcePath,
[Parameter(Mandatory=$true)][String] $destPath,
[Parameter(Mandatory=$true)][String] $Title,
[Parameter()][String] $Description = $null
)

Write-Verbose "Load System.Drawing"
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

Write-Verbose "Get the image from $sourcePath"
$srcImg = [System.Drawing.Image]::FromFile($sourcePath)

Write-Verbose "Create a bitmap as $destPath"
$outputIImg = new-object System.Drawing.Bitmap([int]($srcImg.width)),([int]($srcImg.height))

Write-Verbose "Intialize Graphics"
$Image = [System.Drawing.Graphics]::FromImage($outputIImg)
$Image.SmoothingMode = "AntiAlias"

$Rectangle = New-Object Drawing.Rectangle 0, 0, $srcImg.Width, $srcImg.Height
$Image.DrawImage($srcImg, $Rectangle, 0, 0, $srcImg.Width, $srcImg.Height, ([Drawing.GraphicsUnit]::Pixel))

Write-Verbose "Draw title: $Title"
$Font = new-object System.Drawing.Font("Bauhaus 93", 130, "Bold","Pixel")

#get font size
$font_size = [System.Windows.Forms.TextRenderer]::MeasureText($Title, $Font)
$font_size_width = $font_size.Width
$font_size_height = $font_size.Height

$rect = [System.Drawing.RectangleF]::FromLTRB(0, 0, $srcImg.Width, $srcImg.Height)
$format = [System.Drawing.StringFormat]::GenericDefault
$format.Alignment = [System.Drawing.StringAlignment]::Center
$format.LineAlignment = [System.Drawing.StringAlignment]::Center

#styling font
$Brush = New-Object Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 255, 255, 255))

#lets draw font
$Image.DrawString($Title, $Font, $Brush, $rect, $format)

Write-Verbose "Save and close the files"
$outputIImg.save($destPath, [System.Drawing.Imaging.ImageFormat]::jpeg)
$outputIImg.Dispose()
$srcImg.Dispose()
}

AddTextToImage -sourcePath D:\Temp\Koala.jpg -destPath D:\Temp\Koala2.jpg -Title "Some long long long long long long long long long long long long string here"

关键是调用DrawString有不同的论据。

关于POWERSHELL:$Image.DrawString 居中字符串,左右填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39011252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com