zoukankan      html  css  js  c++  java
  • 关于读取图片文件另存问题

    using (Image image = Image.FromFile(arrFiles[0]))
    {
        MemoryStream stream = new MemoryStream();
        image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        Core.HimallIO.CreateFile(dest, stream, FileCreateType.Create);
    }
    

    读取图片,保存路径没有问题,在最后保存的图片,总是打不开。
    其他关键代码

        private byte[] StreamToBytes(Stream stream)
        {
          byte[] buffer = new byte[stream.Length];
          stream.Read(buffer, 0, buffer.Length);
          stream.Seek(0L, SeekOrigin.Begin);
          return buffer;
        }
        public void CreateFile(string fileName, Stream stream, FileCreateType fileCreateType = FileCreateType.CreateNew)
        {
          string physicalPath = this.GetPhysicalPath(fileName);
          string path = physicalPath.Remove(physicalPath.LastIndexOf("\"));
          if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
          if (fileCreateType == FileCreateType.CreateNew)
          {
            if (File.Exists(physicalPath))
              throw new HimallIOException(IOErrorMsg.FileExist.ToDescription());
            FileStream fileStream = new FileStream(physicalPath, FileMode.Create, FileAccess.Write);
            byte[] bytes = this.StreamToBytes(stream);
            fileStream.Write(bytes, 0, bytes.Length);
            fileStream.Close();
          }
          else
          {
            FileStream fileStream = new FileStream(physicalPath, FileMode.Create, FileAccess.Write);
            byte[] bytes = this.StreamToBytes(stream);
            fileStream.Write(bytes, 0, bytes.Length);
            fileStream.Close();
          }
        }
    

    试过使用其他办法保存,比如直接使用Image.Save(string),这个是可以成功的。

    所以出现在steam问题这里。

    仔细研究代码。。。发现了问题在于steam转为byte[]的时候里面代码全是0,为了解决这个问题,加了一行代码,测试通过,一切OK。

  • 相关阅读:
    导出查询结果到excle
    导出所选行为excle
    spring security LDAP获取用户信息
    spring security防御会话伪造session攻击
    Linux安装Loadrunner generator
    Centos7 安装gitlab
    kafka 安装部署
    zookeeper 搭建
    Oracle GoldenGate对接 Oracle 11g和Kafka
    suse 11 sp4 设置yast 安装源
  • 原文地址:https://www.cnblogs.com/xyfy/p/6130513.html
Copyright © 2011-2022 走看看