none
How do you put a Bitmap on the Clipboard using win32 API from a C# program?

    Question

  • How do you put a Bitmap on the Clipboard using win32 API from a C# program?

     

    public partial class Form1 : Form

    {

     public Form1()

     {

       InitializeComponent();

       Bitmap bitmap = new Bitmap(50,50, this.CreateGraphics());

       

       using (Graphics g = Graphics.FromImage(bitmap))

       {

         g.Clear(Color.White);

         g.FillRectangle(Brushes.Yellow, 10, 10, 30, 30);

       }

       MyClipboard.PutOnClipboard(this.Handle, bitmap);

      }

    }

     

    public class MyClipboard

    {

      [DllImport("user32.dll")]

      static extern bool OpenClipboard(IntPtr hWndNewOwner);

      [DllImport("user32.dll")]

      static extern bool EmptyClipboard();

      [DllImport("user32.dll")]

      static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);

      [DllImport("user32.dll")]

      static extern bool CloseClipboard();

     

      const int CF_BITMAP = 2;

     

      static public void PutOnClipboard(IntPtr hWnd, Bitmap bitmap)

      {

         if (OpenClipboard(hWnd))

         {

            if (EmptyClipboard())

            {

                //What goes here?????

     

                SetClipboardData(CF_BITMAP, ?);

                CloseClipboard();

            }

         }

      }

    }

    Friday, May 12, 2006 5:03 PM

Answers

  • public static void SetClipboardData(Bitmap bitmap, IntPtr hDC)

            {

     

                IntPtr memDC = CreateCompatibleDC(hDC);

                IntPtr memBM = CreateCompatibleBitmap(hDC, bitmap.Width, bitmap.Height);

                SelectObject(memDC, memBM);

     

                using (Graphics g = Graphics.FromImage(bitmap))

                {

                    IntPtr hBitmapDC = g.GetHdc();

                    IntPtr hBitmap = bitmap.GetHbitmap();

                    SelectObject(hBitmapDC, hBitmap);

     

                    BitBlt(memDC, 0, 0, bitmap.Width, bitmap.Height, hBitmapDC, 0, 0, SRCCOPY);

     

                    SetClipboardData(CF_BITMAP, memBM);

     

                    g.ReleaseHdc(hBitmapDC);

                }  

            }

     

    The IntPtr hDC passed in should be:

    using (Graphics g = this.CreateGraphics())

    {

          IntPtr hDC = g.GetHdc();

          ClipboardClass.SetClipbaodData(bitmap, hDC);

          g.ReleaseHdc();

    }

    Also, private const uint SRCCOPY = 0x00CC0020;

    Tuesday, May 16, 2006 7:37 PM

All replies

  • Any chance that you are using the 2.0 Framework? if so you could simply use the SetImage() method of the Clipboard class:

                Clipboard.SetImage(bitmap);

    Friday, May 12, 2006 5:36 PM
  • I know; the real issue I'm trying to solve is that the .NET Clipboard does not fully support metafiles (http://support.microsoft.com/kb/323530/en-us) and I need to put my data on the Clipboard as a metatfile, bitmap, and my own format. The only way to do this (that I see) since you have to use the win32 API for the metafile, is to add all three formats using the win32 API.
    Friday, May 12, 2006 5:45 PM
  • public static void SetClipboardData(Bitmap bitmap, IntPtr hDC)

            {

     

                IntPtr memDC = CreateCompatibleDC(hDC);

                IntPtr memBM = CreateCompatibleBitmap(hDC, bitmap.Width, bitmap.Height);

                SelectObject(memDC, memBM);

     

                using (Graphics g = Graphics.FromImage(bitmap))

                {

                    IntPtr hBitmapDC = g.GetHdc();

                    IntPtr hBitmap = bitmap.GetHbitmap();

                    SelectObject(hBitmapDC, hBitmap);

     

                    BitBlt(memDC, 0, 0, bitmap.Width, bitmap.Height, hBitmapDC, 0, 0, SRCCOPY);

     

                    SetClipboardData(CF_BITMAP, memBM);

     

                    g.ReleaseHdc(hBitmapDC);

                }  

            }

     

    The IntPtr hDC passed in should be:

    using (Graphics g = this.CreateGraphics())

    {

          IntPtr hDC = g.GetHdc();

          ClipboardClass.SetClipbaodData(bitmap, hDC);

          g.ReleaseHdc();

    }

    Also, private const uint SRCCOPY = 0x00CC0020;

    Tuesday, May 16, 2006 7:37 PM