獲取圖片的解析度:
public static System.IO.Stream fileDataStream = null; fileDataStream = FileUpload1.PostedFile.InputStream; System.Drawing.Image image = System.Drawing.Image.FromStream(fileDataStream);//從上傳檔案流中例項化Image類 float w = image.Width;//獲取上傳檔案的水平解析度(以“畫素/英寸”為單位)。 float h = image.Height;//獲取上傳檔案的垂直解析度(以“畫素/英寸”為單位)。
獲取圖片的大小:
public static int fileLength = 0;
fileLength = FileUpload1.PostedFile.ContentLength;//獲取上傳控制元件 fileupload 的內容的大小 這個是以位元組的形式存在的
圖片的壓縮:
#region 壓縮圖片 /// <summary> /// 壓縮圖片 /// </summary> /// <param name="filePath">要壓縮的圖片的路徑</param> /// <param name="filePath_ystp">壓縮後的圖片的路徑</param> public void ystp(Stream fileDataStream, string filePath_ystp) { //Bitmap Bitmap bmp = null; //ImageCoderInfo ImageCodecInfo ici = null; //Encoder Encoder ecd = null; //EncoderParameter EncoderParameter ept = null; //EncoderParameters EncoderParameters eptS = null; try { bmp = new Bitmap(fileDataStream); ici = this.getImageCoderInfo("image/jpeg"); ecd = Encoder.Quality; eptS = new EncoderParameters(1); ept = new EncoderParameter(ecd, 10L); eptS.Param[0] = ept; bmp.Save(filePath_ystp, ici, eptS); } catch (Exception ex) { throw new Exception(ex.Message); } finally { bmp.Dispose(); ept.Dispose(); eptS.Dispose(); } } /// <summary> /// 獲取圖片編碼型別資訊 /// </summary> /// <param name="coderType">編碼型別</param> /// <returns>ImageCodecInfo</returns> private ImageCodecInfo getImageCoderInfo(string coderType) { ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo retIci = null; foreach (ImageCodecInfo ici in iciS) { if (ici.MimeType.Equals(coderType)) retIci = ici; } return retIci; } #endregion 壓縮圖片
獲取檔案的副檔名
private static bool IsAllowedExtension(FileUpload upfile) { string strOldFilePath = ""; string strExtension = ""; string[] arrExtension = { ".jpg" }; if (upfile.PostedFile.FileName != string.Empty) { strOldFilePath = upfile.PostedFile.FileName;//獲得檔案的完整路徑名 strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf(".")).ToLower();//獲得檔案的副檔名,如:.jpg for (int i = 0; i < arrExtension.Length; i++) { if (strExtension.Equals(arrExtension[i])) { return true; } } } return false; }