Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitmapSource from file

Tags:

c#

wpf

How can I load a BitmapSource from an image file?

like image 959
in4man Avatar asked Aug 05 '10 19:08

in4man


1 Answers

  • The BitmapImage class is a non-asbtract subclass of BitmapSource, so just use the new BitmapImage(Uri) constructor and pass that new instance to anything that expects a BitmapSource object.
  • Don't be confused by the fact the argument type is System.Uri: a Uri isn't just for http:// addresses, but it's also suitable for local filesystem paths and UNC shares, and more.

So this works for me:

BitmapImage thisIsABitmapImage = new BitmapImage(new Uri("c:\\image.bmp"));  BitmapSource thisIsABitmapImageUpcastToBitmapSource = thisIsABitmapImage; 
like image 124
iulian3000 Avatar answered Sep 18 '22 19:09

iulian3000