More fun with Bitmaps in Powershell ( PoSH Challenge part 10 )
As I entered another level about bitmaps, I will reflect a bit more about the way of working with bitmaps in PowerShell instead of posting the solution only. ( if this info is to much of a spoiler for this level you cheated some levels anyway ;-) )
The level was called odd even and involved a distorted bitmap ( paste in solution from Posh Challenge Part 9 into console for the bitmap )
So as in Level 7 ( PoSH Challenge part 6 ) and level 9 ( PoSH Challenge part 8 ) I started by loading the drawing namespace as it is not loaded by default ( actually I have it in my profile already, but for the example I ignored this )
And loaded the bitmap into a variable ( $cave in this case) , and I also made a new empty bitmap,in this case half the size of the original (See Title of level) for the result, you can see in the session below I got this information by looking at the contents of the $cave variable.
PoSH> [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
GAC Version Location
--- ------- --------
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll
PoSH> $Cave = [System.Drawing.Bitmap]::FromFile( 'C:\PowerShell\PoshChallenge\cave.jpg' )
PoSH> $cave
Tag :
PhysicalDimension : {Width=640, Height=480}
Size : {Width=640, Height=480}
Width : 640
Height : 480
HorizontalResolution : 72
VerticalResolution : 72
Flags : 77960
RawFormat : [ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]
PixelFormat : Format24bppRgb
Palette : System.Drawing.Imaging.ColorPalette
FrameDimensionsList : {7462dc86-6180-4c7e-8e3f-ee7333a7a483}
PropertyIdList : {20624, 20625}
PropertyItems : {20624, 20625}
PoSH> $Cave2 = New-Object System.Drawing.Bitmap(320 , 240)
PoSH>
To select only the even pixels I use a double for loop for the $x and $y , I use two extra counter here $i and $j , I could also have have divided the X and Y by 2 but this was handier as I had to do some testing before I got to the final answer.
$i = 0
for ($x = 0;$x -lt 640;$x+=2) {
$j = 0
for ($y = 0;$y -lt 480;$y+=2) {
$Cave2.SetPixel($i,$j,$Cave.GetPixel($x,$y))
$j ++
}
$i ++
}
In this loop the only thing I do is copying the even pixels to the new bitmap
And what was left is saving the new bitmap and using the invoke-Item (ii) cmdlet to launce the default viewer to look at the results :
$Cave2.Save("C:\PowerShell\PoshChallenge\CAVE2.jpg")
ii CAVE2.jpg
Making the complete solution for Level 10 :
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Cave = [System.Drawing.Bitmap]::FromFile( 'C:\PowerShell\PoshChallenge\cave.jpg' )
$Cave2 = New-Object System.Drawing.Bitmap(320 , 240)
$i = 0
for ($x = 0;$x -lt 640;$x+=2) {
$j = 0
for ($y = 0;$y -lt 480;$y+=2) {
$Cave2.SetPixel($i,$j,$Cave.GetPixel($x,$y))
$j ++
}
$i ++
}
$Cave2.Save("C:\PowerShell\PoshChallenge\CAVE2.jpg")
ii CAVE2.jpg
In the next level more bitmaps, but then not bitmaps yet ;-)
Enjoy,
Greetings /\/\o\/\/
PS. No one the answer yet to my "Piano Challenge" in PoSH Challenge part 6 ?