I am trying to convert the C# code of the link below to PowerShell code, and I can't process the C# symbol【 $" 】 in PowerShell.
https://github.com/dlemstra/Magick.NET/blob/master/docs/Drawing.md
All related files:
https://www.upload.ee/files/12630601/File.zip.html

Console prompt error:
ERROR: Exception calling "Format" with "1" argument(s): "Input string was not in a correct format."
Adding Text To Existing Image.ps1 (15, 1): ERROR: At Line: 15 char: 1
ERROR: + $caption = New-Object ImageMagick.MagickImage([string]::format("capti ...
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : FormatException
ERROR:
Buggy code line for PS:
$caption = New-Object ImageMagick.MagickImage([string]::format("$caption:{$textToWrite}"), $readSettings)Source code line for C#:
using (var caption = new MagickImage($"caption:{textToWrite}", readSettings))
Powershell Code:
Add-Type -Path ".\Magick.NET-Q16-AnyCPU.dll"
$image = New-Object ImageMagick.MagickImage logo.png
$pathToBackgroundImage = "bg.jpg";
$pathToNewImage = "img_ok.png";
$textToWrite = "Insert This Text Into Image";
$readSettings = New-Object -TypeName ImageMagick.MagickReadSettings
$readSettings.Font = "Calibri"
$readSettings.TextGravity = "Center"
$readSettings.BackgroundColor = New-Object ImageMagick.MagickColor("Transparent")
$readSettings.Height = 250
$readSettings.Width = 680
$image = New-Object ImageMagick.MagickImage($pathToBackgroundImage)
$caption = New-Object ImageMagick.MagickImage([string]::format("caption:{$textToWrite}"), $readSettings)
$image.Composite($caption, 590, 450, [ImageMagick.CompositeOperator]::Over)
$image.Write($pathToNewImage)
$image.Dispose()
C# source code:
var pathToBackgroundImage = "path/to/background.png";
var pathToNewImage = "path/to/newImage.png";
var textToWrite = "Insert This Text Into Image";
// These settings will create a new caption
// which automatically resizes the text to best
// fit within the box.
var readSettings = new MagickReadSettings
{
Font = "Calibri",
TextGravity = Gravity.Center,
BackgroundColor = MagickColors.Transparent,
Height = 250, // height of text box
Width = 680 // width of text box
};
using (var image = new MagickImage(pathToBackgroundImage))
{
using (var caption = new MagickImage($"caption:{textToWrite}", readSettings))
{
// Add the caption layer on top of the background image
// at position 590,450
image.Composite(caption, 590, 450, CompositeOperator.Over);
image.Write(pathToNewImage);
}
}