Skip to content

Instantly share code, notes, and snippets.

@dkarzon
Last active December 8, 2016 00:24
Show Gist options
  • Save dkarzon/a7a7e98a42dde86fca9e to your computer and use it in GitHub Desktop.
Save dkarzon/a7a7e98a42dde86fca9e to your computer and use it in GitHub Desktop.
Powershell script to resize icons for the Windows 8.1 Store and Windows Phone 8.1 Store
# https://gallery.technet.microsoft.com/scriptcenter/Resize-Image-A-PowerShell-3d26ef68
function Resize-Image
{
<#
.SYNOPSIS
Resize-Image resizes an image file
.DESCRIPTION
This function uses the native .NET API to resize an image file, and optionally save it to a file or display it on the screen. You can specify a scale or a new resolution for the new image.
It supports the following image formats: BMP, GIF, JPEG, PNG, TIFF
.EXAMPLE
Resize-Image -InputFile "C:\kitten.jpg" -Display
Resize the image by 50% and display it on the screen.
.EXAMPLE
Resize-Image -InputFile "C:\kitten.jpg" -Width 200 -Height 400 -Display
Resize the image to a specific size and display it on the screen.
.EXAMPLE
Resize-Image -InputFile "C:\kitten.jpg" -Scale 30 -OutputFile "C:\kitten2.jpg"
Resize the image to 30% of its original size and save it to a new file.
.LINK
Author: Patrick Lambert - http://dendory.net
#>
Param([Parameter(Mandatory=$true)][string]$InputFile, [string]$OutputFile, [int32]$Width, [int32]$Height, [int32]$Scale, [Switch]$Display)
# Add System.Drawing assembly
Add-Type -AssemblyName System.Drawing
# Open image file
$img = [System.Drawing.Image]::FromFile((Get-Item $InputFile))
# Define new resolution
if($Width -gt 0) { [int32]$new_width = $Width }
elseif($Scale -gt 0) { [int32]$new_width = $img.Width * ($Scale / 100) }
else { [int32]$new_width = $img.Width / 2 }
if($Height -gt 0) { [int32]$new_height = $Height }
elseif($Scale -gt 0) { [int32]$new_height = $img.Height * ($Scale / 100) }
else { [int32]$new_height = $img.Height / 2 }
# Create empty canvas for the new image
$img2 = New-Object System.Drawing.Bitmap($new_width, $new_height)
# Draw new image on the empty canvas
$graph = [System.Drawing.Graphics]::FromImage($img2)
$graph.DrawImage($img, 0, 0, $new_width, $new_height)
# Create window to display the new image
if($Display)
{
Add-Type -AssemblyName System.Windows.Forms
$win = New-Object Windows.Forms.Form
$box = New-Object Windows.Forms.PictureBox
$box.Width = $new_width
$box.Height = $new_height
$box.Image = $img2
$win.Controls.Add($box)
$win.AutoSize = $true
$win.ShowDialog()
}
# Save the image
if($OutputFile -ne "")
{
$img2.Save($OutputFile);
}
}
New-Item -ErrorAction Ignore -ItemType directory -Path output_phone
New-Item -ErrorAction Ignore -ItemType directory -Path output_windows
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square310x310Logo.scale-100.png -Width 310 -Height 310
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square310x310Logo.scale-140.png -Width 434 -Height 434
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square310x310Logo.scale-180.png -Width 558 -Height 558
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square150x150Logo.scale-100.png -Width 150 -Height 150
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square150x150Logo.scale-140.png -Width 210 -Height 210
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square150x150Logo.scale-180.png -Width 270 -Height 270
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square30x30Logo.scale-100.png -Width 30 -Height 30
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square30x30Logo.scale-140.png -Width 42 -Height 42
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square30x30Logo.scale-180.png -Width 54 -Height 54
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\StoreLogo.scale-100.png -Width 50 -Height 50
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\StoreLogo.scale-140.png -Width 70 -Height 70
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\StoreLogo.scale-180.png -Width 90 -Height 90
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square30x30Logo.scale-100.png -Width 30 -Height 30
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square30x30Logo.scale-140.png -Width 42 -Height 42
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square30x30Logo.scale-180.png -Width 54 -Height 54
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square70x70Logo.scale-100.png -Width 70 -Height 70
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square70x70Logo.scale-140.png -Width 98 -Height 98
Resize-Image -InputFile .\input.png -OutputFile .\output_windows\Square70x70Logo.scale-180.png -Width 126 -Height 126
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\AppIcon.scale-100.png -Width 44 -Height 44
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\AppIcon.scale-140.png -Width 62 -Height 62
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\AppIcon.scale-240.png -Width 106 -Height 106
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\Logo.scale-100.png -Width 150 -Height 150
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\Logo.scale-140.png -Width 210 -Height 210
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\Logo.scale-240.png -Width 360 -Height 360
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\SmallLogo.scale-100.png -Width 71 -Height 71
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\SmallLogo.scale-140.png -Width 99 -Height 99
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\SmallLogo.scale-240.png -Width 170 -Height 170
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\StoreLogo.scale-100.png -Width 50 -Height 50
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\StoreLogo.scale-140.png -Width 70 -Height 70
Resize-Image -InputFile .\input.png -OutputFile .\output_phone\StoreLogo.scale-240.png -Width 120 -Height 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment