/// <summary> /// 保存图片 /// </summary> /// <param name="inputStream">输入数据流</param> /// <param name="sRootDirectory">保存的根目录(不要包括根据时间自动生成的目录)</param> /// <param name="sExtension">扩展名</param> /// <returns>返回根据时间自动生成的文件路径</returns> public static string SaveImage(Stream inputStream, string sRootDirectory, string sExtension) { sRootDirectory = sRootDirectory.TrimEnd('/', '\\'); string sReturn = GetImageDirectory() + CreateFileName(sExtension); string path = sRootDirectory + sReturn; CDirectory.Create(Path.GetDirectoryName(path)); using (FileStream fStream = File.Create(path)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = inputStream.Read(data, 0, data.Length); if (size > 0) { fStream.Write(data, 0, size); } else { break; } } } return(sReturn); }
/// <summary> /// 解压zip数据包并保存图片 /// </summary> /// <param name="inputStream">输入数据流</param> /// <param name="sDirectory">保存的根目录(不要包括根据时间自动生成的目录)</param> /// <returns>返回根据时间自动生成的文件路径列表</returns> public static List <string> UnzipImage(Stream inputStream, string sRootDirectory) { sRootDirectory = sRootDirectory.TrimEnd('/', '\\'); List <string> list = new List <string>(); using (ZipInputStream s = new ZipInputStream(inputStream)) { //为了防止图片文件名重复,在文件名后添加一个标识 int i = 0; ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); string extension = Path.GetExtension(theEntry.Name); if (fileName != string.Empty) { if (VaildImageType.IndexOf("/" + extension + "/") == -1) { continue; } string sReturn = GetImageDirectory() + CreateFileName(extension, i++); string path = sRootDirectory + @"\" + sReturn; CDirectory.Create(Path.GetDirectoryName(path)); using (FileStream fStream = File.Create(path)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { fStream.Write(data, 0, size); } else { break; } } } list.Add(sReturn); } } } return(list); }
public ReMsg UploadPic(Stream stream) { string saveBase = SiteConfig.PhotoBasePath; var now = DateTime.Now; string fileUrl = string.Format("{0}/{1}/{2}/{3}/", saveBase, now.Year, now.Month, now.Day); string filePath = HttpContext.Current.Server.MapPath("~" + fileUrl); string fileName = DateTime.Now.ToString("hhmmss") + new Random().Next(1, 1000); string smallFileName = fileName + "_s"; fileName += ".jpg"; smallFileName += ".jpg"; CDirectory.Create(filePath); var fileFullName = Path.Combine(filePath, fileName); var smallFileFullName = Path.Combine(filePath, smallFileName); var fromimg = Image.FromStream(stream); bool b = false; try { b = MakeThumbnail(fromimg, fileFullName, fromimg.Width, fromimg.Height, 80); if (b) { b = MakeThumbnail(fromimg, smallFileFullName, 250, 250, 80); } } catch (Exception ex) { LogHelper.AddLog(ex.ToString()); } finally { if (fromimg != null) { fromimg.Dispose(); } } return(ReMsg(b, b ? fileUrl + fileName : "缩略图存储失败")); }
public static void SaveImage(Stream inputStream, string sFilePath) { CDirectory.Create(Path.GetDirectoryName(sFilePath)); using (FileStream fStream = File.Create(sFilePath)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = inputStream.Read(data, 0, data.Length); if (size > 0) { fStream.Write(data, 0, size); } else { break; } } } }
/// <summary> /// 保存缩略图片 /// </summary> /// <param name="inputStream">输入数据流</param> /// <param name="sRootDirectory">保存的根目录(不要包括根据时间自动生成的目录)</param> /// <param name="sExtension">扩展名</param> /// <param name="iWidth">图片宽度(缩略后)</param> /// <param name="iHeight">图片高度(缩略后)</param> /// <returns></returns> public static string SaveThumbnailImage(Stream inputStream, string sRootDirectory, string sExtension, int iWidth, int iHeight) { Image image = Image.FromStream(inputStream); image = image.GetThumbnailImage(iWidth, iHeight, null, System.IntPtr.Zero); MemoryStream newStream = new MemoryStream(); image.Save(newStream, GetImageType(sExtension)); newStream.Seek(0, SeekOrigin.Begin); //保存 sRootDirectory = sRootDirectory.TrimEnd('/', '\\'); string sReturn = GetImageDirectory() + "T" + CreateFileName(sExtension); string path = sRootDirectory + sReturn; CDirectory.Create(Path.GetDirectoryName(path)); using (FileStream fStream = File.Create(path)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = newStream.Read(data, 0, data.Length); if (size > 0) { fStream.Write(data, 0, size); } else { break; } } } return(sReturn); }
public void SaveModel() { CDirectory.Create(FilePath); SerializeHelper <BaseXmlModel <T> > .SerToXml(_XmlModel, FullPath); }