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

前端 未结 3 470
甜味超标
甜味超标 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: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");
            }
        }
    }
    

提交回复
热议问题