-1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace newconvert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap bitmap;
        public void Form1_Load(object sender, EventArgs e)
        {
            String fileName = 
                @"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg";

            using (Stream bmpStream = 
                System.IO.File.Open(fileName, System.IO.FileMode.Open))
            {
                Image image = Image.FromStream(bmpStream);

                bitmap = new Bitmap(image);
                pictureBox1.Image = bitmap;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }

        public void button1_Click(object sender, EventArgs e)
        {
            try
            {
                String fileName = 
                    @"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg";
                String fName = 
                    @"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg";
                String Naming = 
                    @"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg";
                String Nam = 
                    @"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg";
                String Namd = 
                    @"C:\Users\Public\Pictures\Sample Pictures\Amazingly Funny People Photo #1 (11).jpg";

                using (Stream bmpStream = 
                    System.IO.File.Open(fileName, System.IO.FileMode.Open))
                {
                    bitmap.Dispose();
                    Image image = Image.FromStream(bmpStream);

                    bitmap = new Bitmap(image);
                    pictureBox2.Image = bitmap;
                    pictureBox2.Height = bitmap.Height;
                    pictureBox2.Width = bitmap.Width;    
                }

            }
            catch (Exception a)
            {
                MessageBox.Show("" + a);
            }
        }
     }
}

I use the above code in windows application to convert jpg file into bitmap and stream the image it works. But I need to know how to do this in wpf application. I'm using grid to set the images. In grid only bitmapimage available. How can I do this?

jww
  • 97,681
  • 90
  • 411
  • 885
KVK
  • 1,257
  • 2
  • 17
  • 48
  • possible duplicate of [How can I convert a jpg file into a bitmap, using C#?](http://stackoverflow.com/questions/24383256/how-can-i-convert-a-jpg-file-into-a-bitmap-using-c) – Daniel Kelley Jun 24 '14 at 12:02
  • Daniel kindly read my question i need to know how can i do it in wpf application – KVK Jun 24 '14 at 12:17

2 Answers2

2

Try something like this

public BitmapImage ImageFromStream(Stream stream)
{
    var image = new BitmapImage();
    image.BeginInit();
    image.StreamSource = stream;
    image.EndInit();
    return image;
}

And then

using (Stream bmpStream = System.IO.File.Open(fileName, System.IO.FileMode.Open))
{
    var bitmapImage = ImageFromStream(bmpStream);
    // etc
}
havardhu
  • 3,576
  • 2
  • 30
  • 42
  • 1
    For completeness: You also have to set the `BitmapCacheOption.OnLoad` flag during initialization of a BitmapImage from a stream that is immediately closed after EndInit. See the Remarks section [here](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.cacheoption.aspx) for reference. – Clemens Jun 24 '14 at 13:28
  • When sou are setting `image.StreamSource = stream;`, sometimes you have to use `image.StreamSource = new MemoryStream(stream.ToArray());`, even if you'd set the stream's position to 0. – aniski Jan 13 '16 at 09:55
2

From: MSDN

// Open a Stream and decode a JPEG image
Stream imageStreamSource = new FileStream("tulipfarm.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];

// Draw the Image
Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(20);
sarat
  • 10,512
  • 7
  • 43
  • 74