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。