Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, February 10, 2016

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 ______________________________________________________________________

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

#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.

No comments: