C# 图片上传,下载,显示汇总
1.图片上传和显示 c# ASP.NET MVC easyui-filebox 图片上传和显示 - Tozhang - 博客园 (cnblogs.com)
2.下载http或fpt图片、文件 C# 下载PDF文件(http与ftp) - Tozhang - 博客园 (cnblogs.com)
3.图片数据流 下载
var qrCode = QRCodeHelper.CreateQRCode(d);//二维码数据流byte var savePath = Guid.NewGuid().ToString("D") + ".png"; string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Image\\" + savePath); //创建文件夹 var folderPath = filePath.Substring(0, filePath.LastIndexOf('\\')); ////FileHelper.CreateDir(folderPath); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } var suffixName = ImageFormat.Png; byte[] bytes = qrCode; using (MemoryStream mem = new MemoryStream(bytes)) { Bitmap bmp = new Bitmap(mem); //保存到磁盘文件 bmp.Save(filePath, suffixName); bmp.Dispose(); }
4.http下载图片直接保存
private void btnPost_Click(object sender, EventArgs e) { string filePath = AppDomain.CurrentDomain.BaseDirectory + "Picture\\" + title + "\\"; string dir = Path.GetDirectoryName(filePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (Path.GetExtension(url) == ".jpg") return; var img = UrlToImage(url); img.Save(filePath + "//" + index + Path.GetExtension(url)); UpdateLB("Success:" + title + ":" + url); //Trace.WriteLine("Success:" + models.Title + ":" + item); }
public static Image UrlToImage(string url) { WebClient mywebclient = new WebClient(); byte[] Bytes = mywebclient.DownloadData(url); using (MemoryStream ms = new MemoryStream(Bytes)) { Image outputImg = Image.FromStream(ms); return outputImg; } }