3

I work in VS 2010, .Net 4.0. In Windows forms app, this is ok:

Image img = Image.FromStream(imagePart.GetStream());

However, in Wpf app, the Image class is from System.Windows.Controls and that isn't valid because it hasn't got a definition for FromStream method. In Windows forms, the Image class is System.Drawing.Image.

Is there a way I can use FromStream in Wpf app?

H H
  • 263,252
  • 30
  • 330
  • 514
petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

1 Answers1

6

In WPF I believe you'd use a BitmapSource such as BitmapImage - and in the latter case, you can set the StreamSource property to the appropriate stream.

EDIT: BitmapSource is just one subclass of ImageSource. Use a source by creating an Image control and setting the Source property.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • An `ImageSource` suffices, which is set as the [`Source`](http://msdn.microsoft.com/en-us/library/system.windows.controls.image.source.aspx) of the `Image` control. – H.B. Nov 06 '11 at 16:43
  • @H.B. But for a bitmap, you'd always use a `BitmapSource` of some description, right? Will edit more detail in. – Jon Skeet Nov 06 '11 at 16:54
  • Yes, i was just reffering to `ImageSource` being the "minimal type". – H.B. Nov 06 '11 at 16:57
  • @H.B. Righto - see whether my edit captures everything you were trying to help with :) – Jon Skeet Nov 06 '11 at 16:57
  • Sory, but could you give an example of the code, because I did not understand how to set the `Stream` to `Source` – EgoPingvina Jun 29 '17 at 12:11
  • @EgoPingvina: I'm not sure what you mean by "set the Stream to Source". You use a StreamSource as the source for your image, passing in the Stream to the StreamSource... – Jon Skeet Jun 29 '17 at 12:36