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
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 version with resizing example:

The hard part is learning how to calculate the image size based on the document, text and size desired.
Attachments
Test-RTBImage.psf
(10.77 KiB) Downloaded 118 times
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 »

Thanks all! I had also begun experimenting with this type of functionality

Your code is likely cleaner/more proper, but this is what I had scavenged from Google search results

Code: Select all


	$lBackup = @{ }
	$lBackup.Add('string', 'object')
	$lDataObject = [System.Windows.Forms.Clipboard]::GetDataObject()
	$lFormats = $lDataObject.GetFormats($false)
	foreach ($lFormat in $lFormats)
	{
		
		$lBackup.Add($lFormat, $lDataObject.GetData($lFormat, $false))
	}
	[System.Windows.Forms.Clipboard]::SetImage([System.Drawing.Image]::FromFile("$ScriptDirectory/barcode.png"))
	$richTextBox2.SelectionStart = $richTextBox2.Text.Length
	$richtextbox2.Focus()
	$richtextbox2.Paste()
	foreach ($lFormat in $lFormats)
	{
		
		$lDataObject.SetData($lBackup[$lFormat])
	}
	[System.Windows.Forms.Clipboard]::SetDataObject($lDataObject)	
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 »

Most of the code you posted is unnecessary.

You are trying to add every format to the clipboard but have not really added anything. Just do this:

Code: Select all

[System.Windows.Forms.Clipboard]::SetImage([System.Drawing.Image]::FromFile("$ScriptDirectory/barcode.png"))
$richTextBox2.Select($richTextBox2.Text.Length,0)
$richTextBox2.Paste()
Better this way:

Code: Select all

$image = [System.Drawing.Image]::FromFile("$ScriptDirectory/barcode.png")
[System.Windows.Forms.Clipboard]::SetImage($image)
$richTextBox2.Select($richTextBox2.Text.Length,0)
$richTextBox2.Paste()
$image.Dispose()
Which is exactly what the first code I posted does. Also download the PSF to see how to code it in a form with the ability to control the size of the image.
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 »

You also must add the "Dispose()" or the file will remain open.
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 »

Thanks the Dispose() tip!!


I was trying to emulate the "save and restore clipboard contents" C# code snippet from here :

Code: Select all

public IDictionary<string, object> GetClipboardData()
{
    var dict = new Dictionary<string, object>();
    var dataObject = Clipboard.GetDataObject();
    foreach(var format in dataObject.GetFormats())
    {
        dict.Add(format, dataObject.GetData(format));
    }
    return dict;
}

public void SetClipboardData(IDictionary<string, object> dict)
{
    var dataObject = Clipboard.GetDataObject();
    foreach(var kvp in dict)
    {
        dataObject.SetData(kvp.Key, kvp.Value);
    }
}

...

var backup = GetClipboardData();
// Do something with the clipboard...
...
SetClipboardData(backup);
I don't think I did it right, though
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 »

That is not what you want to do. Just copy the image to the clipboard and paste it into the control. It takes two steps. Copy then paste. The rest of that has nothing to do with what you are trying to do.

And yes, I am fully aware of how to copy and restore the clipboard formats in C/C++/C# and VB. I have been doing it for years. You are not trying to make a backup of the clipboard. You are just adding an image and pasting. It is the same as Ctl-C and Ctl-V to copy an image to the CB and paste it somewhere else.

C# code does provide a model for how to use Net in PowerShell but does not allow for absolute direct translation. PowerShell does much of what we do in C# automatically. Also many things you may find in C# are wrong and since you are not a Windows programmer you will not be able to detect this.

The code I posted just now is the same code you posted minus all of the unnecessary steps. Actually the code you copied is not even the correct way to enumerate the objects on the clipboard. There is no need for a dictionary. The format class is an enum which is pretty much the same as a dictionary already. Also you never ask for the format and retrieve the existing objects. The purpose of the code inits original was to ask for each object by format and then save that object in memory then clear the clipboard and add your new object, use it then remove it and restore the clipboard. This is a very old WSDK sample intended to demonstrate how the clipboard works. What you posted is missing most of the required code.
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 code you posted is titled "How do I backup and restore the system clipboard in C#?" It has nothing to do with adding an image to the clipboard and pasting it into a control.

Also note that none of the answers were marked as correct because the code does not work in C#. The last guess on the page is close to what is needed to backup the data and to restore the clipboard. It does not add any new objects and it does not clear the clipboard. The "Set" will fail on some special formats and the clipboard may have many other formats that are custom and they may need to be specially copied or specially set. In simple cases it will work but it still has nothing to do with the question you originally asked.
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 »

Thanks for clarifying my many errors; I will remove the extra code. I was blindly copying and pasting from old/random articles without a good understanding of the syntax.

At some point in the future I will look into the maintaining clipboard contents so end users don't complain about it, but for today I am good to go on the RichTextBox image insertion.

I am sorry if my thread or my coding ignorance rubbed you the wrong way; I will refrain from asking multiple questions in a single thread.

Much thanks for your support.
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 »

It is not really necessary to restore the clipboard for end users. This would only be done as an example of how the clipboards works. My code is all you need. If users want a clipboard that saves the data per process then the next update to Windows 10 will allow that. It will be the users choice.

Just get the image and paste it. Don't make your job harder than it needs to be.
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 »

Just for the sake of argument here is how the C# could would translate into PowerShell.

Code: Select all

# save clipboard contents
$saved = [System.Windows.Forms.Clipboard]::GetDataObject().GetFormats() | 
    ForEach-Object{
        [System.Windows.Forms.Clipboard]::GetData($_) 
    }

# restore contents
$saved |
    ForEach-Object{
        [System.Windows.Forms.Clipboard]::SetData($_.GetType(), $_)
    }

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