Reputation:
I'm trying to make my C# code add an image to my (WPF) application's canvas. However, my code does not work.
Image I = new Image();
I.Source = System.IO.File.Open(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", System.IO.FileMode.Open);
I get the error:
Cannot implicitly convert type 'System.IO.FileStream' to 'System.Windows.Media.ImageSource'
I see why this is: The Image object wants the raw bitmap (or jpg or whatever), and my code is giving it an output stream from the file. How do I convert between the two?
Upvotes: 1
Views: 2660
Reputation: 241789
Approximately:
Image I = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", UriKind.Absolute);
bi.EndInit();
I.Source = bi;
Upvotes: 4