Bitmap 与 BitmapSource之间的互换

(窗体剪贴板)System.Drawing.Bitmap

(剪贴板)System.Windows.Media.Imaging.BitmapSource


1.从bitmap转换成ImageSource

        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern bool DeleteObject(IntPtr hObject);

        /// <summary>
        /// 从bitmap转换成ImageSource
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
        {
            //Bitmap bitmap = icon.ToBitmap();
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
            if (!DeleteObject(hBitmap))
            {
                throw new System.ComponentModel.Win32Exception();
            }
            return wpfBitmap;
        }

2.从Bitmap转换成BitmapSource

        /// <summary>
        /// 从Bitmap转换成BitmapSource
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static BitmapSource ChangeBitmapToBitmapSource(this Bitmap bmp)
        {
            BitmapSource returnSource;
            try
            {
                returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
            }
            catch
            {
                returnSource = null;
            }
            return returnSource;
        }

3.从Icon到ImageSource的转换

        /// <summary>
        /// 从Icon到ImageSource的转换
        /// </summary>
        public ImageSource ChangeIconToImageSource(Icon icon)
        {
            ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(icon.Handle,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
            return imageSource;
        }

4.从Icon到ImageSource的转换

        internal static class IconUtilities
        {
            [DllImport("gdi32.dll", SetLastError = true)]
            private static extern bool DeleteObject(IntPtr hObject);

            public static ImageSource ToImageSource(this Icon icon)
            {
                Bitmap bitmap = icon.ToBitmap();
                IntPtr hBitmap = bitmap.GetHbitmap();

                ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());

                if (!DeleteObject(hBitmap))
                {
                    throw new Win32Exception();
                }

                return wpfBitmap;
            }
            // 这个是没有附加转换的,:)
            public static ImageSource ToImageSource(this Icon icon)
            {
                ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon( icon.Handle,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());

                return imageSource;
            }
        }

 5.从ImageSource转换成Bitmap,是从ImageSource中取出UriSource.LocalPath,然后使用 new Bitmap(FileName)的方法获取。其他的方法我还没有找到,

        public static void ToImageSource(this Icon icon)
        {
            System.Windows.Controls.Image ImgUserHeadFaceCutEdit;
            string str1 = ((BitmapImage)(ImgUserHeadFaceCutEdit.Source)).UriSource.AbsolutePath;// 此路径new Bitmap(str1)无法识别
            string str2 = ((BitmapImage)(ImgUserHeadFaceCutEdit.Source)).UriSource.LocalPath;
            //Bitmap sourceImage = new Bitmap(sourceImageUri.ToString());
            string str3 = strImgSourceFileName;
            Console.WriteLine("AbsolutePath =" + str1);
            Console.WriteLine("LocalPath =" + str2);
            Console.WriteLine("srceFileName =" + str3);
        }

        //这是运行结果:
        //AbsolutePath =C:/Documents%20and%20Settings/zp/%E6%A1%8C%E9%9D%A2/%E6%A1%8C%E9%9D%A2%E7%A7%80/10111411409225.jpg
        //LocalPath =C:\Documents and Settings\zp\桌面\桌面秀\10111411409225.jpg
        //srceFileName =C:\Documents and Settings\zp\桌面\桌面秀\10111411409225.jpg


                BitmapSource image = null;
                if (Clipboard.ContainsImage())
                {
                    image = (BitmapSource)System.Windows.Clipboard.GetImage();
                }

                MemoryStream ms = new MemoryStream();
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(image));
                enc.Save(ms);
                Bitmap img = new Bitmap(ms);
                ms.Flush();
                ms = new MemoryStream();
                img.Save(ms, ImageFormat.Jpeg);

                FileStream fs = new FileStream(lpszPath + lpszName, FileMode.Create);
                fs.Write(ms.ToArray(), 0, ms.ToArray().Length);
                fs.Flush();
                fs.Dispose();
                ms = null;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值