View Single Post
Old 08-09-2007   #3 (permalink)
Jeff
Guest


 
 

Re: Viewing an image from within Powershell

On Aug 10, 10:08 am, Luke <L...@discussions.microsoft.com> wrote:
> I've found a way to draw the image. Does anyone know how to get the image to
> update at an interval?
>
> "Luke" wrote:
> > I want to create a separate window (with the system.windows.forms class) and
> > view images from a URL through the new window. As my .NET skills are lacking,
> > this is what I've managed to whip up so far:

>
> > [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
> > $form = new-object System.Windows.Forms.form
> > $form.MaximizeBox = $false;
> > $form.MinimizeBox = $false;
> > $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
> > $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
> > $form.ShowDialog()

>
> > I'm still uncertain how I would go about pulling images from the web and
> > viewing it from this window (System.Windows.Media.Imaging maybe?). Also how
> > do I set the autosize property correctly so that the window resizes itself
> > according to the image size?


[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = new-object System.Windows.Forms.form
$form.MaximizeBox = $false;
$form.MinimizeBox = $false;
$form.StartPosition =
[System.Windows.Forms.FormStartPosition]::CenterScreen
$form.FormBorderStyle =
[System.Windows.Forms.FormBorderStyle]::FixedDialog

$urls = "http://www.google.com/intl/en_ALL/images/logo.gif", `
"http://groups.google.com/groups/img/3nb/groups_medium.gif", `
"http://www.google.com/ig/images/igoogle_logo_sm.gif", `
"http://docs.google.com/images/doclist/logo_docs.gif"
$script:urlIndex = 0

function NextImage
{
if ( $script:urlIndex -gt $urls.Length - 1 )
{
$script:urlIndex = 0
}

$webRequest =
[System.Net.WebRequest]::Create( $urls[ $script:urlIndex ] )
$webResponse = $webRequest.GetResponse()
$image = New-Object System.Drawing.Bitmap
$webResponse.GetResponseStream()
$webResponse.Close()

$imageLabel.Image = $image
$imageLabel.Size = $image.Size
$form.ClientSize = $image.Size

$script:urlIndex++
}

$imageLabel = New-Object System.Windows.Forms.Label
$imageLabel.add_Click( [System.EventHandler] { NextImage } )

$form.Controls.Add( $imageLabel )

NextImage

$imageTimer = New-Object System.Windows.Forms.Timer
$imageTimer.Interval = 2000
$imageTimer.add_Tick( [System.EventHandler] { NextImage } )
$imageTimer.Start()

$form.ShowDialog()

This will move to the next image when you click on the label, or every
two seconds on a timer.

Good luck.

Jeff

My System SpecsSystem Spec