public static List<string> ImageUploadFiles(ref List<string> uploadMsgs) { string fileTypes = ".gif,.jpg,.jpeg,.png,.bmp";//定义允许上传的文件扩展名 return UploadFiles(ref uploadMsgs, fileTypes); } public static List<string> UploadFiles(ref List<string> uploadMsgs, string fileTypes = "") { string imageType = "www"; string id = ""; uploadMsgs = new List<string>();//错误消息 List<string> filePathsList = new List<string>(); int picCount = HttpContext.Current.Request.Files.Count; //IFileUpload fileUpload = FileUploadFactory.GetServer(); for (int i = 0; i < picCount; i++) { HttpPostedFile file = HttpContext.Current.Request.Files[i]; if (file.ContentLength <= 0) { uploadMsgs.Add(string.Format("{0}文件大小不对。", file.FileName)); continue; } else { string fileExt = file.FileName.ToLower().Substring(file.FileName.LastIndexOf('.')); if (!string.IsNullOrEmpty(fileTypes) && (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt) == -1)) { uploadMsgs.Add(string.Format("{0}文件类型不正确,允许的文件类型为:{1}", file.FileName, fileTypes)); continue; } else { var inputStream = file.InputStream; string NewFileName = imageType.ToString(); if (!string.IsNullOrEmpty(id)) NewFileName += "-" + id; NewFileName += "-" + DateTime.Now;//SerialGenerator.GenerateNumber(5); ImageHelper imgHelper = new ImageHelper(); var streamBytes = StreamHelper.Stream2Bytes(inputStream); //先stream到bytes,以便后续复用 MemoryStream ms = new MemoryStream(streamBytes); Image image = Image.FromStream(ms); streamBytes = ReadExif(image); //save(); } } } return filePathsList; } static byte[] ReadExif(Image image) { var exif = new Dictionary<string, string>(); var properties = image.PropertyItems; foreach (var property in properties) { switch (property.Id) { case 0x010E: exif["ImageTitle"] = ASCIIToString(property.Value); break; case 0x010F: exif["Make"] = ASCIIToString(property.Value); break; case 0x0110: exif["Model"] = ASCIIToString(property.Value); break; case 0x0112: exif["Orientation"] = ShortToString(property.Value, 0); break; case 0x011A: exif["XResolution"] = RationalToSingle(property.Value, 0); break; case 0x011B: exif["YResolution"] = RationalToSingle(property.Value, 0); break; case 0x0128: exif["ResolutionUnit"] = ShortToString(property.Value, 0); break; case 0x0131: exif["Software"] = ASCIIToString(property.Value); break; case 0x0132: exif["DateTime"] = ASCIIToString(property.Value); break; //GPS case 0x0002: exif["GPSLatitude"] = string.Format("{0}°{1}′{2}″", RationalToSingle(property.Value, 0), RationalToSingle(property.Value, 8), RationalToSingle(property.Value, 16) ); break; case 0x0004: exif["GPSLongitude"] = string.Format("{0}°{1}′{2}″", RationalToSingle(property.Value, 0), RationalToSingle(property.Value, 8), RationalToSingle(property.Value, 16) ); break; case 0x0006: exif["GPSAltitude"] = RationalToSingle(property.Value, 0); break; } } if (exif.ContainsKey("Orientation")) { switch (int.Parse(exif["Orientation"])) { case 2: image.RotateFlip(RotateFlipType.RotateNoneFlipX);//horizontal flip break; case 3: image.RotateFlip(RotateFlipType.Rotate180FlipNone);//right-top break; case 4: image.RotateFlip(RotateFlipType.RotateNoneFlipY);//vertical flip break; case 5: image.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: image.RotateFlip(RotateFlipType.Rotate90FlipNone);//right-top break; case 7: image.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: image.RotateFlip(RotateFlipType.Rotate270FlipNone);//left-bottom break; } } byte[] byt = ImageToBytes(image); return byt; } static string ByteToString(byte[] b, int startindex) { if (startindex + 1 <= b.Length) return ((char)b[startindex]).ToString(); else return string.Empty; } static string ShortToString(byte[] b, int startindex) { if (startindex + 2 <= b.Length) return BitConverter.ToInt16(b, startindex).ToString(); else return string.Empty; } static string RationalToSingle(byte[] b, int startindex) { if (startindex + 8 <= b.Length) return (BitConverter.ToSingle(b, startindex) / BitConverter.ToSingle(b, startindex + 4)).ToString(); else return string.Empty; } static string ASCIIToString(byte[] b) { return Encoding.ASCII.GetString(b); } private static byte[] ImageToBytes(Image image) { ImageFormat format = image.RawFormat; using (MemoryStream ms = new MemoryStream()) { if (format.Guid.Equals(ImageFormat.Jpeg.Guid) || format.Guid.Equals(ImageFormat.MemoryBmp.Guid) ) { image.Save(ms, ImageFormat.Jpeg); } else if (format.Guid.Equals(ImageFormat.Png.Guid)) { image.Save(ms, ImageFormat.Png); } else if (format.Guid.Equals(ImageFormat.Bmp.Guid)) { image.Save(ms, ImageFormat.Bmp); } else if (format.Guid.Equals(ImageFormat.Gif.Guid)) { image.Save(ms, ImageFormat.Gif); } else if (format.Equals(ImageFormat.Icon)) { image.Save(ms, ImageFormat.Icon); } byte[] buffer = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(buffer, 0, buffer.Length); return buffer; } }