• Bitmap 与ImageSource之间的转换


        public class ImageConverter
        {
            [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)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();
                ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
                if (!DeleteObject(hBitmap))//记得要进行内存释放。否则会有内存不足的报错。
                {
                    throw new System.ComponentModel.Win32Exception();
                }
                return wpfBitmap;
            }
            /// <summary>
            /// 从Bitmap转换成BitmapSource
            /// </summary>
            /// <param name="bmp"></param>
            /// <returns></returns>
            public static BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
            {
                BitmapSource returnSource;
                try
                {
                    returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                }
                catch
                {
                    returnSource = null;
                }
                return returnSource;
            }
            /// <summary>
            /// 从Icon到ImageSource的转换
            /// </summary>
            public ImageSource ChangeIconToImageSource(Icon icon)
            {
                ImageSource imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
                return imageSource;
            }
        }
  • 相关阅读:
    TCP与UDP
    DNS的概念,用途,DNS查询的实现算法
    子网划分的概念,子网掩码
    ping的原理以及ICMP
    组播和广播的概念,IGMP的用途
    ICMP协议
    RIP协议
    MTU的概念,什么是路径MTU? MTU发现机制,TraceRoute(了解)
    TFTP 与 FTP的区别
    vue slot slot-scope
  • 原文地址:https://www.cnblogs.com/candyzkn/p/3664379.html
Copyright © 2020-2023  润新知