-
17-11-2012, 06:12 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Thay đổi kích thước ảnh khi upload lên server bằng cách nào?
Dạo vài dòng kiếm code thay đổi kich thước ảnh, tìm được đoạn code này, nhưng làm hoài không dc, trình con gà, mong moi người giúp.
Đầu tiên bạn cần add thêm 3 thư viện sau:
Mã:using System.Drawing; using System.IO; using System.Drawing.Imaging;
Mã:public void ThayDoiKichThuocAnh(string ImageSavePath, string fileName, int MaxWidthSideSize, Stream Buffer) { int intNewWidth; int intNewHeight; System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Buffer); ImageCodecInfo myImageCodecInfo; myImageCodecInfo = GetEncoderInfo("image/jpeg"); // System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter; //Giá trị width và height nguyên thủy của ảnh; int intOldWidth = imgInput.Width; int intOldHeight = imgInput.Height; //Kiểm tra xem ảnh ngang hay dọc; int intMaxSide; /*if (intOldWidth >= intOldHeight) { intMaxSide = intOldWidth; } else { intMaxSide = intOldHeight; }*/ //Để xác định xử lý ảnh theo width hay height thì bạn bỏ note phần trên; //Ở đây mình chỉ sử dụng theo width nên gán luôn intMaxSide= intOldWidth; ^^; intMaxSide = intOldWidth; if (intMaxSide > MaxWidthSideSize) { //Gán width và height mới. double dblCoef = MaxWidthSideSize / (double)intMaxSide; intNewWidth = Convert.ToInt32(dblCoef * intOldWidth); intNewHeight = Convert.ToInt32(dblCoef * intOldHeight); } else { //Nếu kích thước width/height (intMaxSide) cũ ảnh nhỏ hơn MaxWidthSideSize thì giữ nguyên //kích thước cũ; intNewWidth = intOldWidth; intNewHeight = intOldHeight; } //Tạo một ảnh bitmap mới; Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight); //Phần EncoderParameter cho phép bạn chỉnh chất lượng hình ảnh ở đây mình để chất lượng tốt /hất là 100L; myEncoderParameter = new EncoderParameter(myEncoder, 100L); myEncoderParameters.Param[0] = myEncoderParameter; //Lưu ảnh; bmpResized.Save(ImageSavePath + fileName, myImageCodecInfo, myEncoderParameters); //Giải phóng tài nguyên; //Buffer.Close(); imgInput.Dispose(); bmpResized.Dispose(); } private ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }
Mã:ThayDoiKichThuocAnh(Server.MapPath("~/images/"), pChuoiNgayThang, 180, UpHinh.PostedFile.InputStream);
không biết nó là cái gì. FileUpload mình đặt tên la Uphinh.View more random threads:
- Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path
- Cách chuyển từ Gridview sang DataList trên web ASP.NET?
- 1 tin nhiều nhóm sản phẩm cùng 1 lúc
- Thay thế trang cha bằng trang con trong lập trình ASP.NET
- Cách sử dụng Session ở đâu thì hợp lý, ý nghĩa?
- Thư mục gốc trong asp.net là thế nào?
- Asp.net bị lỗi object reference not set to an instance of an object
- hiển thị sản phẩm và tin tức trên cùng 1 trang cshtml mvc3
- đếm số người online chạy không đúng ????
- Thêm và Sửa Table có quan hệ nhiều nhiều trong mô hinh MVC như thế nào?
-
18-11-2012, 02:18 PM #2Junior Member
- Ngày tham gia
- Sep 2015
- Đang ở
- Số 60 Thái Hà, Đống Đa, Hà Nội
- Bài viết
- 0
Oa, người ta đã hướng dẫn chi tiết vậy rồi mà
VD nhé, trong Web form, khai báo như sau
Mã:<asp:FileUpload ID="UpHinh" runat="server" /> <asp:Button ID="btnOK" runat="server" Text="ok" onclick="btnOK_Click" />
Mã:protected void btnOK_Click(object sender, EventArgs e) { if (UpHinh.FileName != "") { ThayDoiKichThuocAnh(Server.MapPath("~/images/"), UpHinh.FileName, 180, UpHinh.PostedFile.InputStream); } }
Sở dĩ người viết ra code này định dùng "chuỗi ngày tháng" để đặt tên file mà ko dùng UpHinh.FileName là do người ta sợ trường hợp file được up lên trùng tên với file đã có trên server ( khi đó sẽ xảy ra việc chép đè mất file đã có ) => người viết quyết định giải quyết vấn đề này bằng cách đặt tên file theo kiểu ngay_thang_nam_gio_phut_giay của thời điểm file đc up lên .....
Pallet nhựa Lâm Đồng giải pháp hoàn hảo cho nhà kho Pallet nhựa Lâm Đồng là một giải pháp đa năng và hiệu quả trong việc vận chuyển và lưu trữ hàng hóa. Trong đó, pallet nhựa Lâm Đồng không chỉ đáp...
Pallet nhựa Lâm Đồng giải pháp...