1

I created a PowerShell form in PSStudio.

I added a custom WPFMessageBox function that I found online to add message boxes for errors and confirmation.

I'd like to add images to these message boxes, like icons for a triangle warning or capital I for information.

Normally I'd just use a relative path but when you build a package in PSStudio it only packages up the main form and does not include any external pathing pictures.

I googled other solutions and it turns out I can add an ImageList object to the form and add pictures there and access them.

The only thing is when I do this:

$Source = $imagelist.Images[3]
$Image = New-Object System.Windows.Controls.Image
$Image.Source = $Source
$Image.Height = [System.Drawing.Image]::FromFile($Source).Height
$Image.Width = [System.Drawing.Image]::FromFile($Source).Width
$Image.Margin = 10

It throws this error:

ERROR: Exception setting "Source": "Cannot convert the "System.Drawing.Bitmap" value of type "System.Drawing.Bitmap" to type "System.Windows.Media.ImageSource"."
MainForm.psf (2306, 2): ERROR: At Line: 2306 char: 2
ERROR: +         $Image.Source = $Source
ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
ERROR:     + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:
ERROR: Exception calling "FromFile" with "1" argument(s): "System.Drawing.Bitmap"

I'm wondering if there's a better way for me to include images into the form or if there's a way to convert a bitmap to an imagesource?

Thanks in advance!

Trubble
  • 11
  • 1
  • 1
    according to .net docs, you should use `$source = [System.Windows.Media.Imaging.BitmapImage]::new('path\to\image')` as your source – Santiago Squarzon Apr 04 '23 at 15:07
  • [This post](https://stackoverflow.com/q/94456/45375) has a C# solution, but it shouldn't be hard to adapt it to PowerShell. – mklement0 Apr 04 '23 at 15:21

0 Answers0