1. 程式人生 > >Wpf Bitmap(Image)轉BitmapSource(ImageSource),無需外部dll

Wpf Bitmap(Image)轉BitmapSource(ImageSource),無需外部dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Windows.Media;
using System.Windows.Media.Imaging;

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CommonUtils
{
    /// <summary>
    /// Windows圖片處理
    /// </summary>
    public static class WindowsImage
    {
        #region 羅列圖片類
        private static System.Drawing.Bitmap m11;
        private static System.Drawing.Image m12;

        private static System.Drawing.Imaging.BitmapData m21;
        private static System.Drawing.Imaging.ImageFormat m22;
        private static System.Drawing.Imaging.ImageCodecInfo m23;
        private static System.Drawing.Imaging.PixelFormat m24;

        private static System.Windows.Media.BitmapCache m31;
        private static System.Windows.Media.DrawingImage m32;
        private static System.Windows.Media.ImageDrawing m33;
        private static System.Windows.Media.ImageSource m34;
        private static System.Windows.Media.ImageBrush m35;
        private static System.Windows.Media.PixelFormat m36;

        private static System.Windows.Media.Imaging.BitmapImage m41;
        private static System.Windows.Media.Imaging.BitmapSource m42;
        private static System.Windows.Media.Imaging.BitmapDecoder m43;
        private static System.Windows.Media.Imaging.BitmapFrame m44;
        #endregion

        /// <summary>
        /// 獲取截圖
        /// </summary>
        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;
        }

        /// <summary>
        /// 隨機圖片源,官方文件提供
        /// </summary>
        public static BitmapSource RandomSource()
        {
            System.Windows.Media.PixelFormat pixelFormat = PixelFormats.Bgr32;
            int width = 200;
            int height = 200;
            int rawStride = (width * pixelFormat.BitsPerPixel + 7) / 8;
            byte[] rawImage = new byte[rawStride * height];
            // Initialize the image with data.
            Random value = new Random();
            value.NextBytes(rawImage);
            // Create a BitmapSource.
            BitmapSource bitmap = BitmapSource.Create(width, height,
                96, 96, pixelFormat, null,
                rawImage, rawStride);
            return bitmap;
        }

        /// <summary>
        /// 獲取圖片源
        /// </summary>
        public static BitmapSource GetSource()
        {
            Bitmap bitmap = GetScreenShoot();
            MemoryStream memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Bmp);
            bitmap.Dispose();
            BitmapFrame frame = BitmapFrame.Create(memoryStream);

            //為了關記憶體流,嘗試克隆了一下,驗證無效
            //var clone = frame.Clone();
            //驗證記憶體流不能關
            //memoryStream.Close();
            //以下驗證不管用
            //BitmapImage image = new BitmapImage();
            //image.StreamSource = memoryStream;

            return frame;
        }
    }
}