#---8<---Out-Image---PS Filter---Jean-JMST-Belgium--- #=10/2006===================================Version 1.1========# #============ Creates an image from input (string) ============# # # To install: # # Install filter by executing this script globally. # So, prefix this script full path with a dot followed by a space. # ie : # . "c:\my scripts\Out-Image.ps1" # # To use filter: # # *Returns an Drawing.Image Bitmap object* # # -filepath parameter contains path where to save the image. # Extension (bmp,jpg,png,gif, ...) defines the format # # -fname parameter defines the font name to use in image. # For console outputs it's better to use a fixed font. # # -fsize parameter is the font size in pixel # # -fgcolor parameter is text's foreground color # # -bgcolor parameter is the image's background color # # -fstyle parameter is an enum containing font styles # ie: -fstyle "bold","italic" # # -pixformat parameter is image's pixel format # # -inputobject defines an input object # # -autoload loads system.drawing if not already loaded # without asking the user to confirm # # -nooutput if this switch is present, image object isn't # returned # (+/- same as cast [void], > $null or |Out-Null) # # To test : # # [void]("Windows`nPowerShell"|Out-Image 'w:\image.png' -bg 'Tan') # °creates w:\image.png, an image # containing text "Windows Powershell" on 2 lines # # Get-Process|Out-String|Out-Image -file 'z:\process.gif' -no # °creates z:\process.gif, an image containing process list # # $img=Get-Date|Out-Image -fstyle 'bold' # $img.RotateFlip('Rotate90FlipXY') # $img.Width,$img.Height # $img.Save('k:\date.jpg') # °Creates an image containing current date, rotates # image, shows image's width and height, and saves it # to "k:\date.jpg" # # $m=mem|Out-String # Out-Image "u:\mem.bmp" -inputobject $m|Out-Null # °Creates image "u:\mem.bmp" containing mem result # # REMARK : # # Use absolute paths for images. # #=================Tested on PowerShell Version 1===================# filter Out-Image( [string]$FILEPATH, $FNAME="Courier New", [single]$FSIZE=12, $FGCOLOR="White", $BGCOLOR="Black", $FSTYLE="Regular", $PIXFORMAT="Format16bppRgb555", [PSObject]$INPUTOBJECT, [switch]$AUTOLOAD, [switch]$NOOUTPUT ) { #### Functions used later #### function has-assembly($name){ ##Verify of $name assembly is loaded## [appdomain]::CurrentDomain.GetAssemblies()| %{ if( ($_.GlobalAssemblyCache) ` -and ` ($_.GetName().Name -eq $name) ) {return $true} } } function load-assembly($name){ [void][System.Reflection.Assembly]::` LoadWithPartialName($name) } function message($varname){ write-host "`nOut-Image * VALUES ALLOWED FOR " ` "$varname ARE :`n" ` -BackgroundColor 'yellow' ` -ForegroundColor 'red' } function valuesof($enum){ ## outputs enumeration values ## [enum]::GetValues($enum) } #### Loads [system.Drawing] if not already done #### if(!(has-assembly("System.Drawing"))) { if($AUTOLOAD){load-assembly("System.Drawing")} elseif(( $host.UI.PromptForChoice( "Load System.Drawing ?", "", [Management.Automation.Host.ChoiceDescription[]]` ("&No","&Yes"), $false ) )) { load-assembly("System.Drawing") } else { write-host 'Out-Image needs System.Drawing' ` -BackgroundColor 'yellow' ` -ForegroundColor 'red' return } } #### Tests variant variables conformity #### $script:stop=$false $fonts=New-Object "Drawing.Text.InstalledFontCollection" if(!($fonts.Families -contains $FNAME)) { message('$FNAME') $fonts.Families|%{Write-Host $_.Name} $script:stop=$true } trap [InvalidCastException] { message($curvar[1]) $select='.' if($curvar[1] -eq '$PIXFORMAT'){$select='rgb'} Write-Host ( valuesof($curvar[0])| Select-String $select| Out-String ) $script:stop=$true continue } $curvar=[Drawing.KnownColor],'$FGCOLOR' [void][Drawing.KnownColor]$FGCOLOR $curvar=[Drawing.KnownColor],'$BGCOLOR' [void][Drawing.KnownColor]$BGCOLOR $curvar=[Drawing.FontStyle],'$FSTYLE' [void][Drawing.FontStyle]$FSTYLE $curvar=[Drawing.Imaging.Pixelformat],'$PIXFORMAT' [void][Drawing.Imaging.Pixelformat]$PIXFORMAT if($script:stop -eq $true){return} #### Builds the image from input string #### ## Builds a font object with font parameters## $font=new-object ` "Drawing.Font" ` ($FNAME,$FSIZE,$FSTYLE,[Drawing.GraphicsUnit]::Pixel) ## Creates a as little as possible bitmap ## $bitmap=new-object ` "Drawing.Bitmap" ` (1,1) ## Gets graphics from bitmap ## $graphic=[Drawing.Graphics]::FromImage($bitmap) ## Gets string from pipeline or $inputobject ## if($INPUTOBJECT){$text=$INPUTOBJECT}else{$text=$_} ## Measures the string physical size relative ## ## to font attributes ## $stringsize=$graphic.MeasureString($text,$font) ##Creates a bitmap sized to the string physical size## $bitmap=new-object ` "Drawing.Bitmap" ` ( $stringsize.Width, $stringsize.Height, [Drawing.Imaging.PixelFormat]$PIXFORMAT ) ## Gets graphics from bitmap ## $graphic=[Drawing.Graphics]::FromImage($bitmap) ## Creates a brush and set its color to $BGCOLOR ## $brush=New-Object ` "Drawing.SolidBrush" ` ($BGCOLOR) ## Paints graphics with $BGCOLOR to cover bitmap's ## ## background ## $graphic.FillRectangle( $brush, 0, 0, $stringsize.Width, $stringsize.Height ) ## Changes brush color to $FGCOLOR ## $brush.Color=$FGCOLOR ## Paints the string with font and $FGCOLOR ## $graphic.DrawString($text,$font,$brush,0.0,0.0) ## Saves the bitmap to $FILEPATH if $FILEPATH exists ## if($FILEPATH) {$bitmap.Save($FILEPATH)} ## Returns image's bitmap if $NOOUTPUT isn't present ## if(!$NOOUTPUT) {return $bitmap} }#end filter Out-Image #---8<---Out-Image---PS Filter---Jean-JMST-Belgium---