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.
This topic is 5 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
zmilbourn
Posts: 8
Last visit: Tue Dec 14, 2021 10:48 am

RichTextBox: Adding dynamically-generated picture to existing content

Post by zmilbourn »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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
Last visit: Tue Dec 14, 2021 10:48 am

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

Post by zmilbourn »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Sorry but it doesn't work.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post by mxtrinidad »

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!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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(…)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post by mxtrinidad »

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.

:)
This topic is 5 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked