zoukankan      html  css  js  c++  java
  • Image和字节数组互转

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    namespace NetUtilityLib
    {
        public static class ImageHelper
        {
            /// <summary>
            /// Convert Image to Byte[]
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static byte[] ImageToBytes(Image image)
            {
                ImageFormat format = image.RawFormat;
                using (MemoryStream ms = new MemoryStream())
                {
                    if (format.Equals(ImageFormat.Jpeg))
                    {
                        image.Save(ms, ImageFormat.Jpeg);
                    }
                    else if (format.Equals(ImageFormat.Png))
                    {
                        image.Save(ms, ImageFormat.Png);
                    }
                    else if (format.Equals(ImageFormat.Bmp))
                    {
                        image.Save(ms, ImageFormat.Bmp);
                    }
                    else if (format.Equals(ImageFormat.Gif))
                    {
                        image.Save(ms, ImageFormat.Gif);
                    }
                    else if (format.Equals(ImageFormat.Icon))
                    {
                        image.Save(ms, ImageFormat.Icon);
                    }
                    byte[] buffer = new byte[ms.Length];
                    //Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.Read(buffer, 0, buffer.Length);
                    return buffer;
                }
            }
    
            /// <summary>
            /// Convert Byte[] to Image
            /// </summary>
            /// <param name="buffer"></param>
            /// <returns></returns>
            public static Image BytesToImage(byte[] buffer)
            {
                MemoryStream ms = new MemoryStream(buffer);
                Image image = System.Drawing.Image.FromStream(ms);
                return image;
            }
    
            /// <summary>
            /// Convert Byte[] to a picture and Store it in file
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="buffer"></param>
            /// <returns></returns>
            public static string CreateImageFromBytes(string fileName, byte[] buffer)
            {
                string file = fileName;
                Image image = BytesToImage(buffer);
                ImageFormat format = image.RawFormat;
                if (format.Equals(ImageFormat.Jpeg))
                {
                    file += ".jpeg";
                }
                else if (format.Equals(ImageFormat.Png))
                {
                    file += ".png";
                }
                else if (format.Equals(ImageFormat.Bmp))
                {
                    file += ".bmp";
                }
                else if (format.Equals(ImageFormat.Gif))
                {
                    file += ".gif";
                }
                else if (format.Equals(ImageFormat.Icon))
                {
                    file += ".icon";
                }
                System.IO.FileInfo info = new System.IO.FileInfo(file);
                System.IO.Directory.CreateDirectory(info.Directory.FullName);
                File.WriteAllBytes(file, buffer);
                return file;
            }
        }
    }
    View Code
  • 相关阅读:
    SVN操作异常
    VS2010安装MVC3
    (转)游戏类型
    (转)32位汇编指令 寄存器
    (转)#pragma 用法
    (转)UI库
    (转)简单实用的网游服务器架构
    (转)一个客户端网游市场分布的数据
    (转)源于魔兽!《植物大战僵尸》成功奥秘
    (转)【分析】中国网游行业上市公司投资分析之网易
  • 原文地址:https://www.cnblogs.com/codeyou/p/4914976.html
Copyright © 2011-2022 走看看