您是第欢迎光临我的主页位访客
浩凡儿
天行健,君子以自强不息;地势坤,君子以厚德载物!

1.WindowsForm中Image转换为System.Windows.Controls.Image转换

 

 /// <summary>
        /// 把DrawingImage转换为Controls使用的Iamge类
        /// </summary>
        /// <param name="gdiImg"></param>
        /// <returns></returns>
        private System.Windows.Controls.Image ConvertDrawingImageToWPFImage(System.Drawing.Image gdiImg)
        {


            System.Windows.Controls.Image img = new System.Windows.Controls.Image();

            //convert System.Drawing.Image to WPF image
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(gdiImg);
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            img.Source = WpfBitmap;
            img.Width = 500;
            img.Height = 600;
            img.Stretch = System.Windows.Media.Stretch.Fill;
            return img;
        }

 

 

2.Image转换为System.Windows.Media.ImageSource即WPF中使用的图片Source

 

   /// <summary>
        /// 把DrawingImage转换为System.Windows.Media.ImageSource WPF中使用的类中的Iamge类
        /// </summary>
        /// <param name="gdiImg"></param>
        /// <returns></returns>
        private System.Windows.Media.ImageSource ConvertDrawingImageToWPFImage(System.Drawing.Image gdiImg)
        {


            System.Windows.Controls.Image img = new System.Windows.Controls.Image();
            //convert System.Drawing.Image to WPF image
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(gdiImg);
            IntPtr hBitmap = bmp.GetHbitmap();
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }

posted on 2014-10-16 15:02  浩凡儿  阅读(451)  评论(0编辑  收藏  举报