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;
        }
    }
}

 

你可能感兴趣的:(成长的程序员)