Home » General Scripting and Coding » PowerShell » Embedding a base64 image string inside a Powershell Application

Embedding a base64 image string inside a Powershell Application

Posted on by

Description:

Base64 is a binary-to-text encoding scheme – in other words, we can represent binary data (for example, a JPG or a PNG) as an ASCII string.

For me it was useful when I was compiling a PS1 script into an executable.  The tool I was using to compile it with didn’t have support for associated files, so I needed to include everything in the PS1 script.  When used as part of a webpage, it can reduce load time due to limiting the amount of HTTP request/responses.

In this script, I demonstrate embedding a base64 image string inside a Powershell application how we can use base64 encoding to add an icon, a picture and a memory stream email attachment in our Powershell application.

To convert an image to a base64 string, I use this website: http://www.base64-image.de

Source:

N/A

Script:

#create form
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size @(220,150)
$form.StartPosition = "CenterScreen"
$form.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$form.Text = "Alkane Test"
#$form.ShowInTaskbar = $False
$form.Name = "Alkane Test"
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false

#get base64 string from here: http://www.base64-image.de

#set icon for form using Base64
$base64IconString = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAT6SURBVFhHvZcPbBNVHMe/1+tdu26l3R+3zmHt2ABF2TTqcNFgNhN0glXUKIbEaCBqjMZoTJyRIDGOhAiJiqKJI5iJGMmUsIyoaEDHIpapiWRjEGBL9rfAtnZr17W93p3v2lfWXu/2Bxc+6Uvf33u/936/+/1+xzy/0yljARAEkdb0MfEcDAYGMXF6roH+/284jp21iJIIIRajKxIsmABzgWGYeEnlugqgxZxtQJYERIUQBCkGSSZLEr+rJE5mAMvy4HkLONVJ9ZhVAFkWEY34EEEuiopWwJFth4k1ko1YJLeQJZnMI4JJYUxODWBwqBs+KQvZWRawGk9niCEmmVEAWSanDoeQ51qPypL7UHXnM6i0W+moNtHQ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3XxAxjj6B9rRe+UsgsaluCk/n6hGjYjA0H40t+/AIU8T/ujxoKTqNZSrJrKL1qB0/CN4RmlHCjMGI5lkuVEhCi67FAXWXGQpIZkakpIti1IIU5MD8E6MQWCyYeFkWGwrUWhKVwJv4iEH/8VFf+ZWs0ZDJSiJ4lT8k0oi9enJSoZjIBmuGSaj4hMSggkRPyLqvI/MYzkbzOp0iTCrAAuBTC1P/VmmoGmEC008F9TYXOG6CKAP8B8y8tsX2eJriwAAAABJRU5ErkJggg=="
$iconimageBytes = [Convert]::FromBase64String($base64IconString)
$ims = New-Object IO.MemoryStream($iconimageBytes, 0, $iconimageBytes.Length)
$ims.Write($iconimageBytes, 0, $iconimageBytes.Length);
$alkIcon = [System.Drawing.Image]::FromStream($ims, $true)
$Form.Icon = [System.Drawing.Icon]::FromHandle((new-object System.Drawing.Bitmap -argument $ims).GetHIcon())

#add an image to the form using Base64
$base64ImageString = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAT6SURBVFhHvZcPbBNVHMe/1+tdu26l3R+3zmHt2ABF2TTqcNFgNhN0glXUKIbEaCBqjMZoTJyRIDGOhAiJiqKJI5iJGMmUsIyoaEDHIpapiWRjEGBL9rfAtnZr17W93p3v2lfWXu/2Bxc+6Uvf33u/936/+/1+xzy/0yljARAEkdb0MfEcDAYGMXF6roH+/284jp21iJIIIRajKxIsmABzgWGYeEnlugqgxZxtQJYERIUQBCkGSSZLEr+rJE5mAMvy4HkLONVJ9ZhVAFkWEY34EEEuiopWwJFth4k1ko1YJLeQJZnMI4JJYUxODWBwqBs+KQvZWRawGk9niCEmmVEAWSanDoeQ51qPypL7UHXnM6i0W+moNtHQP/D82YRTg7+j43wPjDk2sHQsiYGdqwAREc6KV/FUbT0qLLRzroRPovWXbfju9BkYzfqmpjsiywHwy+qxae01bK5grsa6R3fjMedkQkUpJRX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akpIti1IIU5MD8E6MQWCyYeFkWGwrUWhKVwJv4iEH/8VFf+ZWs0ZDJSiJ4lT8k0oi9enJSoZjIBmuGSaj4hMSggkRPyLqvI/MYzkbzOp0iTCrAAuBTC1P/VmmoGmEC008F9TYXOG6CKAP8B8y8tsX2eJriwAAAABJRU5ErkJggg=="
$imageBytes = [Convert]::FromBase64String($base64ImageString)
$ms = New-Object IO.MemoryStream($imageBytes, 0, $imageBytes.Length)
$ms.Write($imageBytes, 0, $imageBytes.Length);
$alkanelogo = [System.Drawing.Image]::FromStream($ms, $true)

$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $alkanelogo.Size.Width;
$pictureBox.Height =  $alkanelogo.Size.Height; 
$pictureBox.Location = New-Object System.Drawing.Size(85,20) 
$pictureBox.Image = $alkanelogo;
$form.Controls.Add($pictureBox)

$btn = New-Object System.Windows.Forms.Button
$btn.AutoSize = $True
$btn.Text = "Send Email"
$btn.Location = New-Object System.Drawing.Size(65,60)
$btn.add_click(
{

	Try
	{
		#SMTP server name
		$smtpServer = "xxx.yyy.zzz";

		#Creating a Mail object
		$msg = new-object Net.Mail.MailMessage;

		#Creating SMTP server object
		$smtp = new-object Net.Mail.SmtpClient($smtpServer);

		#create a content type object for the attachment
		$ct = new-object Net.Mime.ContentType

		#create Alkane icon in memory stream
		$AlkaneBase64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAT6SURBVFhHvZcPbBNVHMe/1+tdu26l3R+3zmHt2ABF2TTqcNFgNhN0glXUKIbEaCBqjMZoTJyRIDGOhAiJiqKJI5iJGMmUsIyoaEDHIpapiWRjEGBL9rfAtnZr17W93p3v2lfWXu/2Bxc+6Uvf33u/936/+/1+xzy/0yljARAEkdb0MfEcDAYGMXF6roH+/284jp21iJIIIRajKxIsmABzgWGYeEnlugqgxZxtQJYERIUQBCkG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8fMb4Q5eUvoCI388HTnMfxxhrsDxrjoUAU07fTFECSAii+vRFb657AfFMBJelOt8UwLrQvQ8NfBjDx1I12U7RVYLBi+PRWfH6sBT7aNysjX6HpSCOG09N+ghlO19MkjisHy7Qt3XxAxjj6B9rRe+UsgsaluCk/n6hGjYjA0H40t+/AIU8T/ujxoKTqNZSrJrKL1qB0/CN4RmlHCjMGI5lkuVEhCi67FAXWXGQpIZkakpIti1IIU5MD8E6MQWCyYeFkWGwrUWhKVwJv4iEH/8VFf+ZWs0ZDJSiJ4lT8k0oi9enJSoZjIBmuGSaj4hMSggkRPyLqvI/MYzkbzOp0iTCrAAuBTC1P/VmmoGmEC008F9TYXOG6CKAP8B8y8tsX2eJriwAAAABJRU5ErkJggg=="
		$AlkaneImageBytes = [Convert]::FromBase64String($AlkaneBase64String)
		$AlkaneMs = New-Object IO.MemoryStream($AlkaneImageBytes, 0, $AlkaneImageBytes.Length)
		$AlkaneMs.Write($AlkaneImageBytes, 0, $AlkaneImageBytes.Length);
		$AlkaneMs.Seek(0, [System.IO.SeekOrigin]::Begin)

		#Configure attachment				
		$att1 = new-object Net.Mail.Attachment($AlkaneMs, $ct);
		$att1.ContentType.MediaType = “image/png”;
		$att1.ContentId = "Attachment";
		$att1.ContentDisposition.Inline = $True;
		$att1.ContentDisposition.DispositionType = "Inline";

		#Add attachment to the mail
		$msg.Attachments.Add($att1);

		#get current users email address and display name, based on the USERNAME environment variable		
		$searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
		$currentEmail = $searcher.FindOne().Properties.mail.Item(0).ToString();
		$currentName = $searcher.FindOne().Properties.displayname.Item(0).ToString();

		#Configure email sender, recipients and message
		$MessFrom = New-Object System.Net.Mail.MailAddress $currentEmail, $currentName
		$msg.From = $MessFrom;									
		#$msg.ReplyTo = $currentEmail;
		$msg.To.Add("xxx@yyy.zzz");
		$msg.subject = "Example Email";	
		$msg.IsBodyHTML = $true
		$msg.body = "<div style='font-family:calibri;font-size:10pt; font-weight:bold;'>Alkane Support Team</div>
		<div style='font-family:calibri;font-size:10pt;'>Web: alkanesolutions.co.uk</div>
		<div style='font-family:calibri;font-size:10pt;'>Mail: <a href='support@alkanesolutions.co.uk'>support@alkanesolutions.co.uk</a></div><br />
		<img src=cid:Attachment />"				

		#Sending email 
		$smtp.Send($msg)

		[System.Windows.Forms.MessageBox]::Show("Email Sent!" , "Status") 

		#Dispose objects
		$att1.dispose()
		$AlkaneMs.dispose()

	}
	Catch
	{
		[System.Windows.Forms.MessageBox]::Show($_ , "Status") 				
	}

})
$form.Controls.Add($btn)

#show the form
$drc = $form.ShowDialog()

 

5 thoughts on “Embedding a base64 image string inside a Powershell Application

Comments have now been disabled. If you have a question to ask about this post please ask the community!