asp 擷取圖片的 大小 和像素 ,解析度,以及壓縮圖片

來源:互聯網
上載者:User
 

擷取圖片的解析度:

  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;        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.