using (Image image = Image.FromFile(arrFiles[0])) { MemoryStream stream = new MemoryStream(); image.Save(stream, System.Drawing.Imaging.ImageFormat.Pn ...
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。