0

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 enter image description here

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);
    }
}
2
  • $caption = New-Object ImageMagick.MagickImage([string]::format("$caption:{$textToWrite}"), $readSettings) you are trying to define a variable...but in the constructor reference that same variable...that doesnt make sense...the C# code the caption string is not interpolated...its just a string...equivalent code would instead be just "caption:{$textToWrite}" Commented Dec 12, 2020 at 1:26
  • This line of C # code is turned into a PS script, a bit difficult, multiple attempts are not successful Commented Dec 12, 2020 at 2:26

3 Answers 3

3

The problem you're seeing in the error is that you are using a Powershell interpolated string. It thinks you are injecting the variable "$caption" into the string.

This is because in Powershell, strings that are enclosed in double quotes " are treated as interpolated strings. Where as strings enclosed in single quotes ' are treated as literal strings.

In C#, all strings are enclosed in double quotes. But if you want to use interpolated strings, then you preface it with a $ sign, and then you inject your code by using {curly brackets}.

Powershell Example:

$name = "Chad"
Write-Host "Hello $name!" // Interpolated
Write-Host 'Hello $name!' // Literal

Powershell sees the double quotes, and knows to look for variable names inside of the string. In this case, it sees $name and swaps it out with its value. But when single quotes are used, it outputs the literal string.

C# example:

var name = "chad";
Console.WriteLine($"Hello {name}!"); // Interpolated
Console.WriteLine("Hello {name}!");  // Literal

Whereas in C#, this is how interpolated and literal strings work...


So you need to convert the C# interpolated string into a PowerShell interpolated string:

From (C#):

$"caption:{textToWrite}"

To (PowerShell):

"caption:$textToWrite"
Sign up to request clarification or add additional context in comments.

2 Comments

The problem has been resolved,thanks$caption = New-Object ImageMagick.MagickImage([string]::format("caption:$textToWrite"), $readSettings)
Do you have a good perspective? about using and new stackoverflow.com/questions/65261346/…
1

You can do it :

 $Arg1="caption:{0}" -f $textToWrite
 $caption = New-Object ImageMagick.MagickImage($Arg1, $readSettings)

1 Comment

also successfully execute, the same effects above,Do you have a good perspective? about using and new, stackoverflow.com/questions/65261346/…
0

$caption = New-Object ImageMagick.MagickImage([string]::format("$caption:{$textToWrite}"), $readSettings)

you are trying to define a variable...but in the constructor reference that same variable ($caption)that doesnt make sense

the C# code the caption string is not interpolated...its just a string...equivalent code would instead be just "caption:$textToWrite"

 $caption = New-Object ImageMagick.MagickImage([string]::format("caption:$textToWrite"), $readSettings)

2 Comments

ERROR: Exception calling "Format" with "1" argument(s): "Input string was not in a correct format."
In the problem description, all related files have just been uploaded, wow,This line of code is converted into PS, quite difficult

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.