WPF Image控件中的BitmapSource与Bitmap的互相转换

有的时候需要把GDI的System.Drawing中的Bitmap转化成BitmapSource


Source Code

        /// 
        /// 从Bitmap转换成BitmapSource
        /// 
        /// 
        /// 
        public BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
        {
            BitmapSource returnSource; 
            try
            {
                returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());              
            }
            catch
            {
                returnSource = null;
            } 
            return returnSource; 
        }


你可能感兴趣的:(Tips)