zoukankan      html  css  js  c++  java
  • 二进制图片相互转换

    图片转化为二进制

    picpath为图片地址

    using (FileStream fs = new FileStream(picpath, FileMode.Open, FileAccess.Read))
                              {
                                  using (BinaryReader read = new BinaryReader(fs))
                                   {
                                      photobyte = read.ReadBytes((int)fs.Length);
                                       s1 = photobyte.Length;
                                  }
                         }

    二进制转化为图片

     string sa = dsimg.Tables[0].Rows[0]["image"].ToString();  //读出的二进制字符串
                            System.Text.Encoding enc = System.Text.Encoding.UTF8; 
                            photobyte = enc.GetBytes(sa);   //转换为二进制
                            Image image1 = BytesToImage(photobyte);  
                            image1.Save(@"D:\1.JPG", System.Drawing.Imaging.ImageFormat.Jpeg); 

            //保存为d盘1.jpg

    二进制转化为图片保存

    将D:\淘宝图片\1752967.jpg转换为二进制,之后再从二进制转化为图片保存成D:\1.jpg

     class Program
        {
            public static Image BytesToImage(byte[] bytes)
            {
               MemoryStream ms = new MemoryStream(bytes);
                Image image = new Bitmap(ms, true);
               return image;
            }

            public static void Main(string[] args)
            {
                byte[] photobyte;
                int s1;
                using (FileStream fs = new FileStream(@"D:\淘宝图片\1752967.jpg", FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader read = new BinaryReader(fs))
                    {
                        photobyte = read.ReadBytes((int)fs.Length);
                        s1 = photobyte.Length;
                        Image image1 = BytesToImage(photobyte);
                       image1.Save(@"E:\1.JPG", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }

        }

    }

  • 相关阅读:
    ListView -————不能不说的秘密
    良好的开端是成功的第一步———构建程序
    你所不知道的windows窗体
    数据库查询终结版2———分组——连接
    数据库的终结版查询————你想知道吗?
    《官神》浏览闲看笔记
    坚信梦想,奋勇前进!____OS小题狂刷2333
    众里寻他千百度,蓦然回首,却见写者优先算法,她在书本寂静处!
    生产消费问题扩展——三个并发进程R,M,P和一个共享的循环缓冲区B的并发控制
    多生产者-多消费者-环形缓冲区问题
  • 原文地址:https://www.cnblogs.com/happygx/p/1957851.html
Copyright © 2011-2022 走看看