Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have an image in a picture box. I need to put a copy of the image on the clipboard as a JPEG. I figured out how to do it as a PNG, but the JPEG version doesn't work.

This works:
'Put the image in a memorystream. VpaResult.VpaImage is a picturebox.
Dim ms As IO.MemoryStream = New IO.MemoryStream
Call VpaResult.VpaImage.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Dim m_data As DataObject = New DataObject
Call m_data.SetData("PNG", True, ms)
Call Clipboard.SetDataObject(m_data, True)
'The image is on the clip board and can be pasted.
'TestReport is an Excel worksheet.
Call TestReport.PasteSpecial("Picture (PNG)")


This does not work:
'Put the image in a memorystream. VpaResult.VpaImage is a picturebox.
Dim ms As IO.MemoryStream = New IO.MemoryStream
Call VpaResult.VpaImage.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim m_data As DataObject = New DataObject
'I tried both "JPEG" and "JPG". Neither works.
Call m_data.SetData("JPEG", True, ms)
Call Clipboard.SetDataObject(m_data, True)
'The image does not appear to be on the clip board and so cannot be pasted.
'TestReport is an Excel worksheet.
Call TestReport.PasteSpecial("Picture (JPEG)")


If I can get the images in as jpeg the resulting file is 250 KB instead of 606 KB.
Can you explain how to get the image on the clipboard as a jpeg?
Posted

1 solution

This should provide the answer: Clipboard handling with .NET[^].

Sorry, it is C#, but if you want to get some good help or code samples, you need to understand at least some of C#; just VB.NET won't help you much.

After all, all you need to know is this: use System.Windows.Forms.Clipboard.SetImage (for System.Windows.Forms). For WPF, this is System.Windows.Clipboard.SetImage.

[EDIT]

The methods SetImage adds data in the Bitmap format (CF_BITMAP in windows, public static readonly string System.Windows.Forms.DataFormats.Bitmap). This format is common for all bitmaps and has nothing to do with bitmap files and their details, such as compression, quality loss, etc. So the clipboard contains data about bitmap and not about bitmap file; this is irrelevant to the difference between JPEG, PNG and BMP.

About clipboard formats, see http://msdn.microsoft.com/en-us/library/ms649013(v=vs.85).aspx[^].

As you can see from this link, alternatively you can create (unregistered) private format, or register your custom format in the system. For more information, see: http://msdn.microsoft.com/en-us/library/y444e6db.aspx[^]. I do not recommend it; dealing with CB_BITMAP format is much better; it makes it possible to exchange images and image fragments between all applications in the system no matter what formats they support.

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 24-Mar-11 3:56am    
Good link, well worth reading, 5ed!
Sergey Alexandrovich Kryukov 24-Mar-11 4:00am    
Thank you, Espen.
--SA
Dave_6 24-Mar-11 11:31am    
Thank for responding. I'd already read the article. Well done.
Perhaps I am having trouble understanding... I need to put a jpeg on the clipboard. SetImage will only put a bitmap on the clipboard. SetDataObject has many image format options. That's how I worked out getting the png format. However, the jpeg version of my image is less than half the size of the png. If I paste the image into Excel, then manually copy it, pastespecial will show many differnt image formats available, including jpeg. How can I get my image onto the clipboard as jpeg?
Sergey Alexandrovich Kryukov 24-Mar-11 12:02pm    
There is something you don't understand. You put bitmap data in the clipboard, not bitmap files, which is much better. You need to create your own format to do something else.
Please see my updated Answer.
--SA
Dave_6 24-Mar-11 15:56pm    
Are you saying that the image format I selected is not relevant? When I put png data on the clipboard it is really bitmap data?
I do not put bitmap data on the clipboard. I put png data on the clipboard. And I want to put jpeg data on the clipboard. Once I have the png image data on the clipboard, I switch to Excel and select pastespecial. The only paste option is PNG. Bitmap is not an option.
No other image format is available to paste. However, I if paste the png image into Excel, then copy it from within Excel, the new pastespecial list shows many different image formats available to paste, including jpeg. So I know it is possible to paste jpeg data from the clipboard, just as it was possible to paste png data from the clipboard. If I use Paint instead of Excel the png image is not available to paste. Paint only accepts bitmap data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900