Adding an image & text into the clipboard

Good Day Experts...

How do i go about adding an image and text into the clip board at the same time?

Currently i have a RTE & an Image...
The image gets the companies logo and the RTE has the companies description

When the user clicks "Copy to clipboard" button, I want to copy the description and the image to the clip board so they can paste it in word or excel...

When i try add both i get an error saying, Cannot convert image to type string, I've tried to convert the string to an object...but no can do...

Please Please Please help me!
Who is Participating?
DanRollinsConnect With a Mentor Commented:
So the purpose is to attach a logo image to the text.  You will expect the user to be able to modify the text but also immediately see your logo image embedded in the pasted text (and then probably delete it, i assume...).  I also now know that it is not just any graphic, but a specific image that will be added each time.

Thanks.  That clarifies things.

I agree that one solution might be to place both items into a separate RichEdit control then copy the entire content of that control to the clipboard.  The target app should know how to handle the combined data.

I came across this interesting item:
    How To Add Custom Table to the Clipboard to be Pasted into Word

...which shows a step-by-step of pasting "create your own RTF data" into Word.  That presents an interesting possibility:

Create a small RTF file that contains your logo and the words "Hi there"  Read that file as text -- eg, into Notepad (not WordPad or Word.  It will look something like:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20{\object\objemb{\*\objclass Paint.Picture}\objw1409\objh524{\*\objdata
... lots of lines removed...
Hi there\par

(note the "Hi there" is right at the end).

Save that text as a resource.  When the user copies the text, read that resource, then replace the "Hi There" with the text that user has selected.  Then use the technique described in that article to put the data on the clipboard.  When the user Pastes it into Word, you will see exactly what you want to see.

-- Dan
Guy Hengel [angelIII / a3]Billing EngineerCommented:
skrombeenAuthor Commented:

The link just shows how to add images into the clipboard, but does not show on how to solve my problem...

As said, I need to add an image and text simultaneously from 2 different locations into the clipboard...

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Bob LearnedCommented:

I don't understand what you are looking for?  Are you looking to set multiple formats for the Clipboard?

skrombeenAuthor Commented:

Yes, I would need to do that, I need to add an image & text into the clipboard
I don't think it's possible.

The clipboard can hold just one type of format: Text or Graphics.

skrombeenAuthor Commented:
but if i copy this entire page into my clip board and paste it into word, it will give me the text & images?
Bob LearnedCommented:
Also, what version of VB.NET do you have?  2005?

skrombeenAuthor Commented: 2003
Bob LearnedCommented:
Here is a 2003 example of multiple formats, but it won't work for pasting into Word or Excel:

    Dim data As New DataObject
    Dim bmp As New Bitmap(100, 100)
    Dim gr As Graphics = Graphics.FromImage(bmp)
    Dim rect As New Rectangle(0, 0, 100, 100)
    gr.FillRectangle(Brushes.AliceBlue, rect)
    rect.Inflate(-1, -1)
    gr.DrawRectangle(Pens.Red, rect)

    data.SetData(DataFormats.Text, "Test")
    data.SetData("System.Drawing.Bitmap", bmp)

    Clipboard.SetDataObject(data, True)

There are the formats present when you copy an image from Word:

? clipboard.GetDataObject.GetFormats
    (0): "Object Descriptor"
    (1): "Rich Text Format"
    (2): "HTML Format"
    (3): "EnhancedMetafile"
    (4): "MetaFilePict"
    (5): "PNG"
    (6): "GIF"
    (7): "JFIF"
    (8): "PNG+Office Art"
    (9): "GIF+Office Art"
    (10): "JFIF+Office Art"
    (11): "Office Drawing Shape Format"
    (12): "ActiveClipboard"
    (13): "DeviceIndependentBitmap"
    (14): "System.Drawing.Bitmap"
    (15): "Bitmap"
    (16): "Embed Source"
    (17): "Link Source"
    (18): "Link Source Descriptor"
    (19): "ObjectLink"

Bob LearnedCommented:
These are the formats present when you copy an image + text in Word:

    (0): "Object Descriptor"
    (1): "Rich Text Format"
    (2): "HTML Format"
    (3): "System.String"
    (4): "UnicodeText"
    (5): "Text"
    (6): "EnhancedMetafile"
    (7): "MetaFilePict"
    (8): "Embed Source"
    (9): "Link Source"
    (10): "Link Source Descriptor"
    (11): "ObjectLink"

The System.Drawing.Bitmap is conspicuously missing.

skrombeenAuthor Commented:
but the main feature im going to need to to be able to copy & paste this into ms word/ms excel
  (1): "Rich Text Format"

supports both pictures and text ... so does html? I think its just a matter of formatting your data into one of these formats before you put it in the clip board.
skrombeenAuthor Commented:
thanks, do you perhaps have a snippet of code? that could help me?
Bob LearnedCommented:
That was a great suggestion, Greg!!!

Here is some pseudo-code that might help with copying RichText to the clipboard:

1) Get an image
2) Copy it to the clipboard
3) Create a RichTextBox control
4) Append text to the RTB control
5) Paste the image into the RTB control
6) Copy the rich text from the RTB control to the clipboard in the RichText format.

Bob is right on. Using a hidden richtextbox will make this easy .. there are also libraries that can do this without needing a hidden control but if you are already using winforms this is probably not too big of a deal.


>> but if i copy this entire page into my clip board and paste it into word, it will give me the text & images?

Have you tried that?  
Word will grind away for quite a while, converting the HTML of this page into Word objects.  The result is rather unpleasant.

You can add both types of data to the clipboard, but programs will always grab one or the other when the end-user uses the paste command.

One option would be to render the text as graphics (say, in a white box that would appear beneath the image), then create a new image composed of the original image plus the "text-as-graphics" image below it.  

This would work, but it would depend on your ultimate goal whether it would do the job you need done.  For instance, if you expected the end-user to be able to later modify the text, it's no-go, but if you expect the user to just view or print the result, it would be fine.

If you expect the end user to be able to manipulate both the text and the graphics, then creating a combined object (say, an RTF or HTML document) as described above, would seem to be your only option.
Dan: I thought pasting RTF was pretty quick in word?
Bob LearnedCommented:
It is.

I read the comment "but if i copy this entire page... " as  referring to this web page on EE.
When I tried that, (Ctrl+A, Ctrl+C; switch to Word; Ctrl+V) Word behaved as I described.  WordPad seemed to malfunction.

Another thought:
It would be possible to create a Metafile document that includes both the image and the text and place that on the clipboard.  Perhaps Word / Excel could break out the image from the text.

We will need to know the ultimate purpose of this question in order to divine the best answer.

-- Dan
*gets a Y shaped stick*

its a chart!
skrombeenAuthor Commented:
Dan: The purpose of this question is explained on the first question...

Basically what i have is a win form (,
On this form their is an image(logo) of a company and a description of the selected company...
Their is also a button labelled "Copy to clipboard"

When the user clicks this copy to clipboard button, the application should copy the selected text and then append the companies logo in the clipboard, so the user can paste this information into excel/word, as my client is currently putting a document together containing information and needs to know the source of these descriptions.
All Courses

From novice to tech pro — start learning today.