Ejemplo n.º 1
0
 /// <summary>
 /// 把图像文件转化为字节数组
 /// </summary>
 /// <param name="imageFile"></param>
 /// <returns></returns>
 public static byte[] Image2Bytes(string imageFile)
 {
     using (var ms = new MemoryStream())
     {
         using (Image img = Image.FromFile(imageFile))
         {
             img.Save(ms, GetImageFormat(imageFile));
             return(StreamUtility.Stream2Bytes(ms));
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 把字节数组转化为图像对象
 /// </summary>
 /// <param name="bytes"></param>
 /// <returns></returns>
 public static Image Bytes2Image(byte[] bytes)
 {
     try
     {
         return(Image.FromStream(StreamUtility.Bytes2Stream(bytes)));
     }
     catch
     {
         return(null);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 把图像转化为字节数组
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static byte[] Image2Bytes(Image img)
        {
            if (img == null)
            {
                return(null);
            }

            using (Stream s = new MemoryStream())
            {
                img.Save(s, ImageFormat.Jpeg);
                return(StreamUtility.Stream2Bytes(s));
            }
        }