PowerShell Studio: Working with Images in a RichTextBox Control

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: 

## - PowerShell:

Line #3 – This is the line that will paste the image from the clipboard into the RichTextBox area:

// - C# language:
## - PowerShell 

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:

Code for using the OpenFileDialog Control to look for image files
Sample application opening the file browser

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

Sample application viewing full size image

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

Code for displaying the image full size

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%:

Sample application pasting resize image to 50% in the RichTextBox area

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

Code for displaying the image resize to 50%

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

ReadOnly property set to “False” to allow images to be displayed

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)


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