WPF Image: .Source = Clipboard.GetImage() is not displayed

前端 未结 3 471
甜味超标
甜味超标 2021-01-21 14:54

This simple program does not work, the image does not appear in the Window.

namespace ClipBoardTest
{
    public partial class MainWindow : Window
    {
                 


        
相关标签:
3条回答
  • 2021-01-21 15:19

    Visual Studio 2019, WPF C# project targeting .NET Core 3.1, the following works (or, at least, it does for me):

    myImage.Source = GetImageFromClipBoard();
    

    where:

    public static ImageSource GetImageFromClipBoard ()
            {
            if (Clipboard.ContainsImage())
                {
                return Clipboard.GetImage();
                }
            else
                return null;
            }
    
    0 讨论(0)
  • 2021-01-21 15:42

    Use Clipboard.GetDataObject to get bitmap and convert it into bitmapSource. Also, be aware that Bitmap.GetHbitmap() leaks memory unless you call DeleteObject on it.

    So, correct solution would be to dispose the IntPtr after use. Declare the method at class level and use it from your code:

    // at class level
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hObject);
    

    if (Clipboard.ContainsImage())
    {
        IDataObject clipboardData = Clipboard.GetDataObject();
        if (clipboardData != null)
        {
            if (clipboardData.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
            {
                System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)clipboardData.GetData(System.Windows.Forms.DataFormats.Bitmap);
                IntPtr hBitmap = bitmap.GetHbitmap();
                try
                {
                    ImageUIElement.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    Console.WriteLine("Clipboard copied to UIElement");
                }
                finally 
                {
                    DeleteObject(hBitmap)
                }
            }
        }
    }
    

    Source - MSDN and Memory leak in Bitmap.

    0 讨论(0)
  • 2021-01-21 15:42

    Now it works fine.

    if (Clipboard.ContainsImage())
    {
        // ImageUIElement.Source = Clipboard.GetImage(); // does not work
        System.Windows.Forms.IDataObject clipboardData = System.Windows.Forms.Clipboard.GetDataObject();
        if (clipboardData != null)
        {
            if (clipboardData.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
            {
                System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)clipboardData.GetData(System.Windows.Forms.DataFormats.Bitmap);
                ImageUIElement.Source =  System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
                Console.WriteLine("Clipboard copied to UIElement");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题