我想在下载图片时或下载后调整其大小。这是我的代码。质量不重要。
那么我该怎么做呢?
public void downloadPicture(string fileName, string url,string path) {
string fullPath = string.Empty;
fullPath = path + @"\" + fileName + ".jpg"; //imagePath
byte[] content;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream)) {
content = br.ReadBytes(500000);
br.Close();
}
response.Close();
FileStream fs = new FileStream(fullPath, FileMode.Create); // Starting create
BinaryWriter bw = new BinaryWriter(fs);
try {
bw.Write(content); // Created
}
finally {
fs.Close();
bw.Close();
}
}
那么我该怎么做呢?
using(){}
,GDI 操作可能会任意失败并导致内存泄漏,这将进一步触发更多异常和额外的泄漏内存,直到服务器崩溃。.NET GC 不知道像 Image、Graphics 或 Bitmap 这样的 GDI 对象。它们在未托管堆中分配数十兆字节的 RAM,对 .NET 不可见。一个 500KB 的 12MP 图像需要 48MB 的连续 RAM 才能简单地被解码。 - Lilith River