Blog Stats
  • Posts - 94
  • Articles - 0
  • Comments - 10
  • Trackbacks - 0

 

Monday, November 02, 2009

Retrieve Image from Windows Clipboard via .Net C#

If you want to snag clipboard data from a different application into the context of your running .Net application, here's how you can do it:

        private static Image _clipBoardImage = null;

        private static Image GetImageFromCopyPasteBuffer()
        {
            Thread t = new Thread(new ThreadStart(GetClipboardBitmap));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
            return _clipBoardImage;
        }

        private static void GetClipboardBitmap()
        {
            IDataObject data = Clipboard.GetDataObject();
            if (data == null || !data.GetDataPresent(DataFormats.Bitmap, true))
                throw new ApplicationException("No clipboard image data was present.");
            _clipBoardImage = (Image)data.GetData(DataFormats.Bitmap);
        }

 

Hope this helps!

 

 

 

Copyright © Greg Steen