locked
Image control can't render BitmapSource/InteropBitmap RRS feed

  • Question

  • Hi,

    I'm building a simple application in which I want to render a "thumbnail" of the clipboard data if that data is an Image. I've managed to do everything right (as far as I know) but the Image control of the Window doesn't render correctly the contents of the BitmapSource.

    Here is the XAML of the Window:
    1<Window x:Class="ClipboardTest.Window1" 
    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    4    Title="Window1" SizeToContent="WidthAndHeight"
    5    <StackPanel Margin="10"
    6        <Image MaxHeight="200" MaxWidth="300" x:Name="ImagePreview" Margin="10"></Image> 
    7        <Button x:Name="ButtonCapture" Click="ButtonCapture_Click" MinWidth="75" Margin="10">Capture Clipboard Data</Button> 
    8    </StackPanel> 
    9</Window> 
    And here is the code of the code behind file:
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Imaging; 
    using System.Windows.Navigation; 
    using System.Windows.Shapes; 
     
    namespace ClipboardTest 
        /// <summary> 
        /// Interaction logic for Window1.xaml 
        /// </summary> 
        public partial class Window1 : Window 
        { 
            public Window1() 
            { 
                InitializeComponent(); 
            } 
     
            private void ButtonCapture_Click(object sender, RoutedEventArgs e) 
            { 
                if (Clipboard.ContainsImage()) 
                { 
                    BitmapSource source = Clipboard.GetImage(); 
                    ImagePreview.Source = source; 
                } 
            } 
        } 
     
    To test it just run the program, then press Print Screen (or copy just any image from paint) and then click on the button. As you'll see, the image gets actually rendered because the Image control resizes accordingly (note the maximum values so if you Print Screen it will just resize to 300x200). BUT the actual contents of the image are not rendered, the image is simply blank. I don't know what I'm doing wrong. I've noticed that altough the documentation states that Clipboard.GetImage(); returns a BitmapSource, the actual object I get when debugging is an InteropBitmap. I don't know if this has anything to do with it.

    Any ideas on how to get the source of the ClipboardData (and actually any bitmapsource/interopbitmap) get rendered well on a default WPF Image control?

    Many thanks!


    Friday, August 29, 2008 11:46 PM

Answers