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

Posted on 2006-05-03
Last Modified: 2012-06-22

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 to look at the contents of the clipboard.


Question by:JamesR219
    LVL 6

    Expert Comment

    Have you tried using Clipboard.SetImage(b) instead of setDataObject?

    Author Comment


    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?


    Author Comment

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


    Author Comment

    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.

    Author Comment

    Bumping up the points on this one!


    Author Comment

    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);

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now