This example shows the shortest code you need to draw vector graphics in PowerShell. However, this code demonstrates a problem rather than a solution. The .NET Framework wants you to inherit a Form class when you describe a picture, because you will need to be able to access protected properties and to respond to events. But since the CreateGraphics function is public, you can still do some hacking with an instance of a Form class. This code creates a window, draws an X on it, and leaves it visible for 5 seconds.
#Load the Windows Forms library. [string]$WindowsFormsLibrary = "System.Windows.Forms,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" [System.Reflection.Assembly]::Load($WindowsFormsLibrary) #Load the Drawing library. [string]$DrawingLibrary = "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [System.Reflection.Assembly]::Load($DrawingLibrary) #Create a window, and set its properties. [System.Windows.Forms.Form]$f = New-Object System.Windows.Forms.Form $f.Width = 500 $f.Height = 300 #Display the form. $f.Show() #Force a complete draw before we add any graphics. $f.Refresh() #Acquire the graphics interface. [System.Drawing.Graphics]$g = $f.CreateGraphics() #Draw anything you like here using GDI+. $g.DrawLine([System.Drawing.Pens]::Black, 50, 20, 450, 250) $g.DrawLine([System.Drawing.Pens]::Black, 450, 20, 50, 250) #Dispose the graphics interface. $g.Dispose() #Wait for 5 seconds before closing it all down. Start-Sleep -s 5 $f.Close() $f.Dispose()
If we take the trouble to create a custom type based on the Form class, we can create form has double buffering enabled by definition, and is of correct size by definition. The size can be set from outside the form as shown above, but the DoubleBuffered property that controls double buffering could not. So by just replacing the predefined Form class with a custom extension, we are slightly closer to our goal.
#Load the Windows Forms library.
[string]$WindowsFormsLibrary =
"System.Windows.Forms,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"
[System.Reflection.Assembly]::Load($WindowsFormsLibrary)
#Load the Drawing library.
[string]$DrawingLibrary =
"System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
[System.Reflection.Assembly]::Load($DrawingLibrary)
#Create a custom form with double buffering.
$Refs = @("System.Windows.Forms", "System.Drawing")
Add-Type -ReferencedAssemblies $Refs @'
public class MyGraphicsForm:System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new System.Drawing.Size(500, 300);
this.DoubleBuffered = true;
this.Text = "My graphics form";
this.ResumeLayout(false);
}
}
'@
#Create our custom window.
[System.Windows.Forms.Form]$f = New-Object MyGraphicsForm
#Display the form.
$f.Show()
#Force a complete draw before we add any graphics.
$f.Refresh()
#Acquire the graphics interface.
[System.Drawing.Graphics]$g = $f.CreateGraphics()
#Draw anything you like here using GDI+.
$g.DrawLine([System.Drawing.Pens]::Black, 50, 20, 450, 250)
$g.DrawLine([System.Drawing.Pens]::Black, 450, 20, 50, 250)
#Dispose the graphics interface.
$g.Dispose()
#Wait for 5 seconds before closing it all down.
Start-Sleep -s 5
$f.Close()
$f.Dispose()
To be continued.




Converting from JComments to Disqus
This is not a complete solution, but it is a good start. When you do an export from JComments that you intend to import to Disqus, a format change is required. This exe is designed to read one XML-file and write the data of that file in Disqus XML file.
Start from cmd.exe and pass a source file (must exist) and a destination file (will be overwritten) as arguments. Example:
The program will ask you about a post link. Type something like this:
Two escape codes are accepted. [ID] is replaced with the ID of the post and [CAT] is replaced with the parent ID of the post.
If you need to do adjustments, the Visual Basic source code can be found here.
1 Comment
Categories: General.
Tags: Disqus, JComments