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
RichTextBox: Adding dynamically-generated picture to existing content
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.
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.
Re: RichTextBox: Adding dynamically-generated picture to existing content
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.
Re: RichTextBox: Adding dynamically-generated picture to existing content
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.
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.
Re: RichTextBox: Adding dynamically-generated picture to existing content
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
$richtextbox1.Rtf = Get-Content C:\Users\jvier\OneDrive\Pictures\Jim\Document.rtf -raw
Re: RichTextBox: Adding dynamically-generated picture to existing content
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 '-'
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: RichTextBox: Adding dynamically-generated picture to existing content
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.
Basically, this use the clipboard to copy the image found then paste into the richtextbox.
Hope this helps!
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)");
};
}
}
Hope this helps!
Re: RichTextBox: Adding dynamically-generated picture to existing content
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(…)
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(…)
Re: RichTextBox: Adding dynamically-generated picture to existing content
Here is the code snippet. It places the text after the currently inserted text.
We can also encode the image as UUE and save it in the PS1 using the standard Sapien image encoder method.
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()
}
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: RichTextBox: Adding dynamically-generated picture to existing content
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.
One important thing I forgot to mention... The "ReadOnly" Property need to be set to FALSE or it won't display the image.