简体   繁体   中英

how to stream the image using c# wpf application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web; 
using System.IO;

namespace WpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public MainWindow(Stream stream)
        {
            InitializeComponent();
            String path = @"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg";
            var image = new BitmapImage();
            try
            {
                image.BeginInit();
                image.StreamSource = stream;
                image.EndInit();
                using (Stream bmpStream = System.IO.File.Open(path,System.IO.FileMode.Open))
                {
                    Image im = Image.FromStream(bmpStream);
                    //Bitmap img = (Bitmap)Image.FromFile("aa.gif", true);
                    //var im = ImageFromStream(bmpStream);
                    grid1.Background = new ImageBrush(new BitmapImage(new Uri(@"im")));
                }
                // return image;
            }

            catch (Exception a)
            {
                // return image;
            }
        }
        public void Window_Loaded(object sender, RoutedEventArgs e)
        {


        }


     }

}

I am using the above code to stream the jpg image as bitmap, and set it into a grid using c#, but Image im = Image.FromStream(bmpStream); shows an error. Can someone please guide me in how to stream the jpg image as bitmap and set into grid?

The way WPF handles and displays images is a bit different from the way WinForms did this. Most image-displaying "things" need a ImageSource and you have to search for the right one to use. In this case you should be able to use the BitmapImage - but the last time I checked this one needs some more care to display the content of a byte-stream correctly.

This snippet shows how you should be able to get this working right BitmapImage :

private static BitmapImage LoadImage(Stream stream)
{
    // assumes that the streams position is at the beginning
    // for example if you use a memory stream you might need to point it to 0 first
    var image = new BitmapImage();

    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.StreamSource = stream;
    image.EndInit();

    image.Freeze();
    return image;
}

As you can see you need to tell the image to use the stream as soon as it loads (or you can get nasty things like ObjectDisposedException s.

You can use the returned object as your Image's source property.

So I think what you are looking for is something like this:

public MainWindow(Stream stream)
{
    InitializeComponent();
    grid1.Background = new ImageBrush(LoadImage(stream));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM