• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 848
  • Last Modified:

C# copying bitmap to clipboard results in 32bpp BITFIELDS image rather then format of bitmap (24bpp RGB).


When copying a bitmap to the clipboard using the below code it shows up as a 32bpp DIB..

Bitmap b = new
for (int i=0;i<50;i++)

The above code does work for many applications (mspaint, word, wordpad), but I have an application which is failing to retrieve a 32bpp BITFIELDS bitmap from the clipboard. The only difference I can tell between what my C# application puts on the clipboard and what mspaint puts on the clipboard is the format of the DIB stored in the clipboard. When mspaint puts an image on the clipboard it comes up as a 24bpp RGB image.  When I put a Bitmap on the clipboard via C# it shows up as a 32bpp BITFIELDS image.  I am thinking that the third party application I am attemping to paste into doesn't like this format. If I have no DIB on the clipboard the application correctly reports "No image on clipboard".

 I've been using http://www.codeguru.com/cpp/w-p/clipboard/externallinks/article.php/c9155/ to look at the contents of the clipboard.


  • 5
1 Solution
Have you tried using Clipboard.SetImage(b) instead of setDataObject?
JamesR219Author Commented:

Thanks for the comment. I don't see a method called SetImage(Bitmap) in the Clipboard class. Is it in System.Windows.Forms.Clipboard or somewhere else?

JamesR219Author Commented:
I see that is a 2.0 method, I am using VS 2003 so I only have 1.1

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

JamesR219Author Commented:
Hello e1v,

I've tried building it with VS 2005 and I get the same results, even using the SetImage call..

I've found a link which kind of explains, but I haven't figured it out. Something about creating the DIB manually.

JamesR219Author Commented:
Bumping up the points on this one!

JamesR219Author Commented:
I found the solution. You must manually put the DIB onto the clipboard after first removing the file header.

Here is the code which works.

MemoryStream ms = new MemoryStream();
MemoryStream ms2 = new MemoryStream();
objBitmap.Save(ms, ImageFormat.Bmp);
byte[] b = ms.GetBuffer();
ms.Position = 0;
Clipboard.SetData("DeviceIndependentBitmap", ms2);
PAQed with points refunded (500)

Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now