Load BitmapImage and immediately initialize a from a stream. - CSharp System.Windows.Media.Imaging

CSharp examples for System.Windows.Media.Imaging:BitmapImage

Description

Load BitmapImage and immediately initialize a from a stream.

Demo Code

// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Windows.Media.Imaging;
using System.IO;/*from ww  w .  ja va  2s . co m*/
using System;

public class Main{
        /// <summary>
        /// Load and immediately initialize a <see cref="BitmapImage"/> from
        /// a stream.
        /// </summary>
        public static BitmapImage Load(Stream stream) {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = stream;
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.EndInit();
            return image;
        }
        /// <summary>
        /// Load and immediately initialie a <see cref="BitmapImage"/> from
        /// a file on disk. This does not keep a lock on the file.
        /// </summary>
        public static BitmapImage Load(string filePath) {
            // Use Begin/EndInit to avoid locking the file on disk
            var image = new BitmapImage();
            image.BeginInit();
            image.UriSource = new Uri(filePath);
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.EndInit();
            return image;
        }
}

Related Tutorials