In this blog post we take a look at how to include images in a RichTextBox Control for a GUI application. This may be helpful if you have a scenario where you want to display a selected image with the form.
Some examples include displaying images and their associated information for topics such as computer assets, users, or your favorite vacation destination photos.

If you find the code in C# language, there’s a strong chance it can be done in PowerShell.
I found the following C# code and tested the application to make sure it is working.

After analyzing the C# code I identified three lines that need to be converted to PowerShell:
Line #1 – This line will create the image object:
// - C# Language:
Image img = Image.FromFile(openFileDialog1.FileName);
## - PowerShell:
$Image = [System.Drawing.Image]::FromFile($Selectfile, $true);
Line #2 – This line will copy the image object onto the Clipboard:
// - C# language:
Clipboard.SetImage(img);
## - PowerShell:
[System.Windows.Forms.Clipboard]::SetImage($Image);
Line #3 – This is the line that will paste the image from the clipboard into the RichTextBox area:
// - C# language:
richTextBox1.Paste();
## - PowerShell
$richtextbox1.Paste();
Now I can proceed with building my application and adding a few more components.
Sample Display Image Application
In addition to displaying the full size view of the image I included the ability to resize the image to 50%, and also an open file folder control to help search for the images.
The application will allow you to pick the image file using the OpenFileDialog control:


After the image file is selected then it will be displayed in the RichTextBox area:

For displaying the full size image the code is simple enough:

This application has a Checkbox control labeled “Reduce 50%” which is used to resize the image. The if-statement will verify if the checkbox has been checked – making the condition “True” – and then proceed to run the code to resize the image.
This time the checkbox is checked, so it will display the image resize to 50%:

If the checkbox is selected, then the next time the image is selected it will be resized by executing the following code:

Note: In order to have images in the RichTextBox area you need to have the “ReadOnly” property set to “False”.

Change Text Color
In this sample application the things you can do with the RichTextBox control text properties are limited. But one in particular allows you to change the text color when displaying the result. Use the following code:
## - Code to change the font color to "Magenta":
$richtextbox1.SelectionColor = 'Magenta';

Sample application showing changing text color in the RichTextBox area
As shown, you can be creative when creating rich text responses in a GUI Application.
Sample Application
Download the sample application: RichTextBoxImageSample_01a.zip (05/29/2019 – Sample updated with disposing the image after use)
Feedback
As always, if you have any ideas, comments, or feedback, please visit our feedback forum and reference this post.
Max Trinidad is a Technology Evangelist at SAPIEN Technologies Inc., and a Microsoft PowerShell MVP. You can reach him at maxt@sapien.com
You need to dispose of all images after you have loaded them from a file. The file will remain locked until the image is disposed:
$Image = [System.Drawing.Image]::FromFile($Selectfile, $true)
… other code
# dispose image after use.
$image.Dispose()