1

I have an image in a the clipboard ( a bitmap ) which I am pulling from the clipboard as follows

     datObj = Clipboard.GetDataObject();  
     InteropBitmap pdfBitmap = datObj.GetData(DataFormats.Bitmap) as InteropBitmap;    

How do I get this pdfBitmap to show up in a WPF listbox. THw WpfListBox looks so..

 <ListBox ItemsSource="{Binding Path=Pages}">
   <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Margin="10" Source="{Binding Path=UriSource}"></Image>               
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>     

and the binding is to an

private ObservableCollection<BitmapImage> pages = new ObservableCollection<BitmapImage>();

I want to first scale the interopbitmap ( something akin to the getthumbnailimage() ) and then I need to somehow turn the interopbitmap into a BitmapImage .
Thanks

Rahul
  • 2,194
  • 4
  • 31
  • 46

1 Answers1

0

There's no need to convert InteropBitmap to a BitmapImage....it's an ImageSource, so can be set as the Source for the Image control.

And in your ListBox itemtemplate, you just use "{Binding}".

You can then "scale" your bitmap by defining a Width and Height on your Image.

(I assume you have a Pages property that you use to access the pages field)

private ObservableCollection<ImageSource> pages = new ObservableCollection<ImageSource>();

 <ListBox ItemsSource="{Binding Path=Pages}">
   <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Margin="10" Source="{Binding}"></Image>               
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox> 

However if the above is still not suitable...e.g. if you are trying to reduce the memory footprint of your "bitmaps" by converting to a lower resolution (i.e. your thumbnail size), then you can convert from InteropBitmap to a BitmapImage by "streaming" the data out, so it can be "read" by the WPF based BitmapImage.

If you can get access to the underlying Hbitmap then you might be able to use CreateBitmapSourceFromHBitmap (e.g. by accessing the Clipboard using WIN32 calls instead of the WPF/.Net ones).

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47