Solved

Adding an image & text into the clipboard

Posted on 2006-07-20
22
1,700 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:skrombeen
  • 7
  • 6
  • 4
  • +3
22 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17159481
0
 
LVL 1

Author Comment

by:skrombeen
ID: 17159493
Hi

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...

Tx
Shaun
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17192593
Shaun,

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

Bob
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:skrombeen
ID: 17192651
Hi

Yes, I would need to do that, I need to add an image & text into the clipboard
0
 
LVL 27

Expert Comment

by:Tolomir
ID: 17192844
I don't think it's possible.

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

Tolomir
0
 
LVL 1

Author Comment

by:skrombeen
ID: 17192912
but if i copy this entire page into my clip board and paste it into word, it will give me the text & images?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17192938
Also, what version of VB.NET do you have?  2005?

Bob
0
 
LVL 1

Author Comment

by:skrombeen
ID: 17193043
Vb.net 2003
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17193196
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)
    gr.Clear(Color.White)
    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
{Length=20}
    (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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17193239
These are the formats present when you copy an image + text in Word:

 clipboard.GetDataObject.GetFormats
{Length=12}
    (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.

Bob
0
 
LVL 1

Author Comment

by:skrombeen
ID: 17193263
thanks...
but the main feature im going to need to to be able to copy & paste this into ms word/ms excel
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17193692
  (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.
0
 
LVL 1

Author Comment

by:skrombeen
ID: 17194102
thanks, do you perhaps have a snippet of code? that could help me?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17194276
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
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17194663
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.

Cheers,

Greg
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 17194724
>> 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.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17195920
Dan: I thought pasting RTF was pretty quick in word?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17196076
It is.

Bob
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 17196249
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
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17197163
*gets a Y shaped stick*

its a chart!
0
 
LVL 1

Author Comment

by:skrombeen
ID: 17198773
Dan: The purpose of this question is explained on the first question...

Basically what i have is a win form (vb.net),
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.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 17199069
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.

However...
I came across this interesting item:
    How To Add Custom Table to the Clipboard to be Pasted into Word
    http://support.microsoft.com/kb/q216676/

...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
01050000
02000000
07000000
50427275736800
00000000
00000000
20270000
424d0a2700000000000036000000280000005e000000230000000100180000000000d4260000c4
0e0000c40e00000000000000000000000000000000000000000000000000000000000000000000
... lots of lines removed...
f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7
f7f7f7f7f7f7ffffffffffffffffffffffffffffff0000030000000000
}}}\par
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
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rebind a grid after user clicks on node in treeview 1 41
Expression Evaluater 3 37
VB.NET 2008 - SQL Timeout 9 34
Groupbox Control ? 2 16
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question