2 Replies - 7847 Views - Last Post: 06 August 2012 - 06:15 AM Rate Topic: -----

#1 complete  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 12-April 07

How Do I load a BitmapImage in WPF?

Posted 03 August 2012 - 08:41 AM

In a silverlight app, I have a BitmapImage defined as System.Windows.Media.Imaging.BitmapImage and it as a method called "SetSource" where I can set the source like this:

	BitmapImage bitmap = new BitmapImage(); 
	System.IO.Stream stream = _scene.GetStream(); 
	if (stream == null) return; 
	bitmap.SetSource(stream); 

 
In a WPF application I have also have a Bitmap image defined as System.Windows.Media.Imaging.BitmapImage but there is no SetSource method. How do I set the source in a WPF app like I do in a Silverlight app?

Also, it is a stream, not a string. It is not a URI. so "UriSource" method does not work. I tried this:
 
        System.IO.Stream stream = _scene.GetStream(); 
        if (stream == null) return; 
        BitmapImage bitmap = new BitmapImage(); 
 
        bitmap.UriSource = new Uri(stream.ToString()); 

 
And at runtime, it threw an error tha URI cannot be determined. Is the URI an identifier for the intranet? Are you sure that this is not a silverlight thing? I am doing a WPF application

Is This A Good Question/Topic? 0
  • +

Replies To: How Do I load a BitmapImage in WPF?

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: How Do I load a BitmapImage in WPF?

Posted 03 August 2012 - 09:09 AM

Hi,

First, the C# Advanced Discussion forum is for discussion topics only. Help questions go in the C# forum. However, since this question is specifically about WPF, I've moved it to the WPF forum.



This link might help:

http://stackoverflow...ystream-png-gif
Was This Post Helpful? 0
  • +
  • -

#3 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: How Do I load a BitmapImage in WPF?

Posted 06 August 2012 - 06:15 AM

Not sure if this will help or not but here is what i use to get an image from a memorystream. Its in vb but I'm sure its not much different. the variable "data" is a byte array that is grabbed from a database.

 
          data = CType(cmd.ExecuteScalar(), Byte())
        Dim strm As New MemoryStream
        Dim bi As New BitmapImage
        If data IsNot Nothing Then
            strm.Write(data, 0, data.Length)
            strm.Position = 0
            Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(strm)

            bi.BeginInit()
            Dim ms As New MemoryStream
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
            ms.Seek(0, SeekOrigin.Begin)
            bi.StreamSource = ms
            bi.EndInit()
        End If

This post has been edited by bflosabre91: 06 August 2012 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1