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。

  • 相关阅读:
    SEO之关键词选择
    我所了解的搜索引擎工作原理
    搜索引擎工作原理
    SEO定义目的,优化的好处
    今天开始我的博客旅程啦!
    [ABC209E] Shiritori
    Codeforces Global Round 12
    CF771E Bear and Rectangle Strips
    CF1392H ZS Shuffles Cards
    CF1439D INOI Final Contests
  • 原文地址:https://www.cnblogs.com/xyfy/p/6130513.html
Copyright © 2011-2022 走看看