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

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

Hello,

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

Bitmap b = new
Bitmap(100,100,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
for (int i=0;i<50;i++)
{
      b.SetPixel(i,i,Color.Red);
}
Clipboard.SetDataObject(b);

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.

Thanks!

-James
0
JamesR219
Asked:
JamesR219
  • 5
1 Solution
 
e1vCommented:
Have you tried using Clipboard.SetImage(b) instead of setDataObject?
0
 
JamesR219Author Commented:
Hello,

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?

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

-jr
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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.

http://www.eggheadcafe.com/forumarchives/netframeworkdrawing/aug2005/post23517574.asp
0
 
JamesR219Author Commented:
Bumping up the points on this one!

-jr
0
 
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();
ms2.Write(b,14,(int)ms.Length-14);
ms.Position = 0;
Clipboard.SetData("DeviceIndependentBitmap", ms2);
0
 
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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