Skip to main content

Draw Using Transparent Colors with PowerShell

Have you ever had to walk an end user through some PowerShell commands?  It is not pretty.  We all have different skill sets. As IT Professionals we need to remember that our skill set does not easily translate well to our users.  They want pretty pictures to click on and simplistic processes.  One area of PowerShell actually involves creating GUIs.  I do not recommend GUIs for everything.  Only if you need to put your code in front of someone else.

The most important part of developing a GUI is to make sure your PowerShell code runs first.  Without solid code doing the intended task, your GUI will just be a waste of time.  Developing GUIs can be time consuming without the right tools.

This post is about how to draw with transparent colors.  To set this up, I used SAPIEN PowerShell Studio 2015 as my tool to help accelerate my GUI development. 

To set this up, I created an empty form.  Next I added a PictureBox control which I renamed PB1 and also set PB1’s size property to 256, 256.

This is what our final product will look like:



Here is the code:
<#
===============================================================================
Transparent Color Demonstration
Jason A. Yoder
Twitter: @JasonYoder_MCT

Created with SAPIEN PowerShell Studio
===============================================================================
#>

# Create your brushes with their colors
$RBrush = [System.Drawing.SolidBrush]::New([System.Drawing.Color]::FromArgb(100, 255, 0, 0))
$GBrush = [System.Drawing.SolidBrush]::New([System.Drawing.Color]::FromArgb(100, 0, 255, 0))
$BBrush = [System.Drawing.SolidBrush]::New([System.Drawing.Color]::FromArgb(100, 0, 0, 255))

# Create the rectangles that the circles will fit inside of.
$RRec = [System.Drawing.Rectangle]::New(78, 50, 100, 100)
$GRec = [System.Drawing.Rectangle]::New(50, 95, 100, 100)
$BRec = [System.Drawing.Rectangle]::New(106, 95, 100, 100)

# Events ______________________________________________________________________

$form1_Load={
# No activities need to take place in the load event.
      
}#end form1_Load

$PB1_Paint=[System.Windows.Forms.PaintEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
      
       # Paint the circles
       $_.Graphics.FillEllipse($RBrush, $RRec)
       $_.Graphics.FillEllipse($GBrush, $GRec)
       $_.Graphics.FillEllipse($BBrush, $BRec)
      
      
}#end PB1_Paint


Let’s talk about what is going on in this code.

We use System.Drawing.SolidBrush objects to create the brushes to use to paint our ellipses.  Ellipses are used to draw circles.  We also use System.Drawing.Color objects to define our colors.  When we define our colors, we called the FromArgb method. ARGB stands for Alpha (Transparency), Red, Green, and Blue.  For the Alpha channel, 255 is opaque while 0 is transparent.  For the three color values, 255 is that channel all the way on while 0 is off.

The System.Drawing.Rectangle objects define where to draw our ellipses.  The first 2 numbers are the upper left corner position.  The next two values are the width and height.

We then call the Paint event for our PictureBox.  This event creates a temporary variable $_.  Using  $_ to render our drawings allow for the PictureBox controls double buffering to be used.  This prevents flickering if you are doing any animation.  Using $_ we call the graphics object and then the FillEllipse method.  We provide the brush to use and the location to draw. 

The rest is just running the code.

If you picked up on the hint about animation, that is tomorrow.






Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.