RichTextBox: Adding dynamically-generated picture to existing content

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
zmilbourn
Posts: 8
Joined: Thu May 05, 2016 10:37 am

RichTextBox: Adding dynamically-generated picture to existing content

Post by zmilbourn » Thu Mar 14, 2019 2:06 pm

Hello.

I am attempting to add a dynamically generated image (barcode) to an RTF file, and then load it into a RichTextBox.

I have tried using scavenged C# code from Google to convert the image's data to text, and then insert it into the RTF file prior to loading the RTF file into the RichTextBox. This method works when viewing the resulting RTF file within MS Word and WordPad, but the formatting requirements for the RichTextBox must be more strict as the image will not display there.

Am I going about this the wrong way?

Any good method to programmatically load an image file and insert it at the very beginning of the RichTextBox's existing content?



Any help would be greatly appreciated! I've been messing with raw RTF file text for a couple days now and it's driving me crazy trying to figure this out.

Thanks

User avatar
jvierra
Posts: 13506
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by jvierra » Thu Mar 14, 2019 2:58 pm

I don't believe that the RTB control supports images. Try opening a Wordpad rtf document in the control. I think it will not display the image.

User avatar
zmilbourn
Posts: 8
Joined: Thu May 05, 2016 10:37 am

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by zmilbourn » Thu Mar 14, 2019 4:18 pm

Hi jvierra; I can confirm the RTB on PS Studio supports images - which is good! For example, you can CTRL+V to paste an image from your clipboard to the RTB. Then if you save the RTF from the RTB, the image appears OK in RTB/Word/WordPad when loaded up

I'm out of the office right now or I could post you a screenshot.

I was hoping someone might have encountered this and had coded something up already. I'll keep working on it, though!

Thank you.

User avatar
jvierra
Posts: 13506
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by jvierra » Thu Mar 14, 2019 4:58 pm

Sorry but it doesn't work.

User avatar
jvierra
Posts: 13506
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by jvierra » Thu Mar 14, 2019 5:44 pm

Here is one way to load an RTF image into an RTB.

$richtextbox1.Rtf = Get-Content C:\Users\jvier\OneDrive\Pictures\Jim\Document.rtf -raw

User avatar
jvierra
Posts: 13506
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by jvierra » Thu Mar 14, 2019 6:08 pm

The following is how to get the image as a hex string:

Code: Select all

    $f = Get-Item 'd:\Epson\IM_A0037.JPG'
    $s = $f.OpenRead()
    $bytes = [byte[]]::new($s.Length)
    $s.Read($bytes,0,$s.Length)
    $hex = [System.BitConverter]::ToString($bytes) -replace '-'

User avatar
mxtrinidad
Site Admin
Posts: 294
Joined: Sun Mar 03, 2013 12:42 pm

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by mxtrinidad » Mon Mar 18, 2019 7:21 am

Sorry for the late response as I'm also out on a conference.

The following is a code snippet to allow add an image to a richtextbox control. This also use the opendialog control to help search for images.

Code: Select all

$buttonSelectFolder_Click={
	#TODO: Place custom script here
	
	Try
	{
		## - Logic to handle FolderBrowserDialog response:
		if ($openfiledialog1.ShowDialog() -eq 'OK')
		{
			$OpenFileDialog1.Filter = "*.png |*.jpg"
			$Selectfile = $openfiledialog1.FileName
			
			$Foundfiles = "Select file - $Selectfile.`r`n";
			$richtextbox1.AppendText(($Foundfiles | Out-String -Width 1000));
			
		};
				
		$Image = [System.Drawing.Image]::FromFile($Selectfile,$true)
		[System.Windows.Forms.Clipboard]::SetImage($Image)
		
		$ImageSize = @"
Image_Size: 
Width = $($Image.Width)
Height = $($Image.Height)
`r`n
"@
		
		$richtextbox1.AppendText($ImageSize);
		$richtextbox1.Paste();
		
		$richtextbox1.AppendText(("`r`nImage found!`r`n"));
		$richtextbox1.ScrollToCaret();
		
	}
	catch
	{
		## - Catch and create custom err variable:
		$MyError = $($error[0].Exception);
		
	}
	finally
	{
		## - Check for errors:
		if ($MyError -eq $null)
		{
			if ($AbortProcess -ne $true)
			{
				## - Richtextbox Output:
				#$richtextbox1.AppendText(("`r`File found!" | Out-String -Width 1000));
				
			}
			else
			{
				## - Abort backup step notification:
				$richtextbox1.ForeColor = 'Red';
				$richtextbox1.AppendText(($Abortmsg | Out-String -Width 1000));
				
			};
			
		}
		else
		{
			## - Update Richtextbox Output: 
			$richtextbox1.ForeColor = 'Red';
			$richtextbox1.AppendText(("File search failed!" | Out-String -Width 1000));
			$richtextbox1.AppendText("`r`n Error: $($MyError)");
			
		};
		
	}
	
}
Basically, this use the clipboard to copy the image found then paste into the richtextbox.

Hope this helps!

User avatar
jvierra
Posts: 13506
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by jvierra » Mon Mar 18, 2019 7:25 am

Nice Max. Here is what we decided on I think:

https://social.technet.microsoft.com/Fo ... powershell

It is similar but much easier. Sizing the picture takes a few more lines (~10).

The code in the link can be added as a snippet function for convenience.

To position the picture with the text use $richtextbox.Select(…)

User avatar
jvierra
Posts: 13506
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by jvierra » Mon Mar 18, 2019 7:28 am

Here is the code snippet. It places the text after the currently inserted text.

Code: Select all

$button1_Click={
    $f = 'd:\Epson\IM_A0037.JPG'
    $image = [System.Drawing.Bitmap]::FromFile($f)
    [System.Windows.Forms.Clipboard]::SetImage($image)
    $richtextbox1.Select($richtextbox1.TextLength,0)
    $richtextbox1.Paste()
    $image.Dispose()
}
We can also encode the image as UUE and save it in the PS1 using the standard Sapien image encoder method.

User avatar
mxtrinidad
Site Admin
Posts: 294
Joined: Sun Mar 03, 2013 12:42 pm

Re: RichTextBox: Adding dynamically-generated picture to existing content

Post by mxtrinidad » Mon Mar 18, 2019 8:30 am

Awesome JVierra!!

One important thing I forgot to mention... The "ReadOnly" Property need to be set to FALSE or it won't display the image.

:)

Locked