Wpf Bitmap(Image) Base64, Url, 파일 Path, Stream에서 BitmapSource(ImageSource)로 전환하여 외부 dll 필요 없음

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Windows.Media.Imaging;

namespace CommonUtils
{
    /// 
    /// Windows    
    /// 
    public static class WindowsImage
    {
        #region BitmapSource
        /// 
        ///      
        /// 
        public static BitmapSource GetSource(Stream stream)
        {
            //   stream    
            return BitmapFrame.Create(stream);
        }

        /// 
        ///      
        /// 
        public static BitmapSource GetSource(Image image)
        {
            return GetSource(image.Stream());
        }

        /// 
        ///      
        /// 
        public static BitmapSource GetSource(byte[] bytes)
        {
            return GetSource(bytes.ToStream());
        }

        /// 
        ///      
        /// 
        public static BitmapSource GetSourceFromBase64(string base64)
        {
            return GetSource(base64.Base64Decode());
        }

        /// 
        ///      
        /// 
        public static BitmapSource GetSourceFromUrlOrPath(string urlOrPath)
        {
            return new BitmapImage(new Uri(urlOrPath));
        }
        #endregion

        /// 
        ///     
        /// 
        public static Bitmap GetScreenShoot()
        {
            Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), bitmap.Size);
            graphics.Dispose();
            return bitmap;
        }
    }
}

좋은 웹페이지 즐겨찾기