zoukankan      html  css  js  c++  java
  • wpf 图片操作类 ImageBrush BitmapImage

     /// <summary>
            /// 解析字节数组成图片
            /// </summary>
            /// <param name="byteArray"></param>
            /// <returns></returns>
            public static BitmapImage ByteArrayToBitmapImage(byte[] byteArray)
            {
                BitmapImage bmp = null;
                try
                {
                    bmp = new BitmapImage();
                    bmp.BeginInit();
                    bmp.StreamSource = new MemoryStream(byteArray);
                    bmp.EndInit();
                }
                catch
                {
                    bmp = null;
                }
                return bmp;
            }
    
    
            /// <summary>
            /// 图片数据解析成字节流数组(用于存储到数据库)
            /// </summary>
            /// <param name="bmp"></param>
            /// <returns></returns>
            public static byte[] BitmapImageToByteArray(BitmapImage bmp)
            {
                byte[] byteArray = null;
                try
                {
                    Stream sMarket = bmp.StreamSource;
                    if (sMarket != null && sMarket.Length > 0)
                    {
                        sMarket.Position = 0;
                        using (BinaryReader br = new BinaryReader(sMarket))
                        {
                            byteArray = br.ReadBytes((int)sMarket.Length);
                        }
                    }
                }
                catch
                {
                }
                return byteArray;
            }
    
            /// <summary>
            /// 根据图片的路径解析成图片资源
            /// </summary>
            /// <param name="filePath"></param>
            /// <returns></returns>
            public static byte[] BitmapImageToByteArray(String filePath)
            {
    
                byte[] byteArray = null ;
                if(File.Exists(filePath))
                    byteArray = File.ReadAllBytes(filePath);
                return byteArray;
            }
    
            /// <summary>
            /// 根据图片的相对路径 返回 BitmapImage对象的实例化
            /// </summary>
            /// <param name="imgPath">图片的相对路径(如:@"/images/star.png")</param>
            /// <returns></returns>
            public static BitmapImage GetBitmapImage(string imgPath)
            {
                try
                {
                    if (!imgPath.StartsWith("/"))
                    {
                        imgPath = "/" + imgPath;
                    }
                    return new BitmapImage(new Uri("Pack://application:,,," + imgPath));
                }
                catch
                {
                    return EmptyImageSource;
                }
            }
    
            /// <summary>
            /// 根据图片的相对路径 获取Image对象
            /// </summary>
            /// <param name="imgPath">图片的相对路径(如:@"/images/star.png")</param>
            /// <returns></returns>
            public static Image GetImage(string imgPath)
            {
                if (File.Exists(imgPath))
                {
                    Image im = new Image();
                    im.Source = GetBitmapImage(imgPath);
                    return im;
                }
                else
                    return null;
            }
    
            /// <summary>
            /// 根据图片的相对路径 获取ImageBrush对象 (此对象资源可以直接用于绑定控件的Background属性)
            /// </summary>
            /// <param name="imgPath">图片的相对路径(如:@"/images/star.png")</param>
            /// <returns></returns>
            public static ImageBrush GetImageBrush(string imgPath)
            {
                if (File.Exists(imgPath))
                {
                    ImageBrush ib = new ImageBrush();
                    ib.ImageSource = GetBitmapImage(imgPath);
                    return ib;
                }
                else
                    return null;
            }
  • 相关阅读:
    如何利用排班实现告警的灵活分派?
    OneAlert 携手 BearyChat(倍洽)快速构建 IT 运维 on-call 机制
    OneAPM大讲堂 | Metrics, Tracing 和 Logging 的关系
    OneAPM大讲堂 | Java 异常日志记录最佳实践
    从区块链的角度看企业协作
    为什么 APM 能提升 IT 团队工作质量?
    JavaScript中的私有成员[翻译]
    【工作分解法】IT人,你的工作“轻松”么?
    【数据分析】线性回归与逻辑回归(R语言实现)
    【数据分析】贝叶斯原理以及简单案例说明
  • 原文地址:https://www.cnblogs.com/lanymy/p/2579692.html
Copyright © 2011-2022 走看看