Thay đổi kích thước ảnh khi upload trong ASP.Net

Thay đổi kích thước ảnh khi upload trong ASP.Net

Bạn thiết kế một website cho khách hàng, giao quyền admin cho khách hàng có thể cập nhật dữ liệu. Nhưng khi khách hàng upload hình ảnh sản phẩm lên website, làm thế nào có thể xử lý được khi khách hàng upload ảnh quá lớn khiến cho tốc độ load website bị chậm và gây hao tốn dung lượng hosting. WEBXAULA sẽ giới thiệu với bạn cách thay đổi kích thước ảnh ngay khi admin upload ảnh lên host bằng ASP.NET 

Thay đổi kích thước ảnh khi upload trong ASP.Net

Đầu tiên bạn cần add thêm 3 thư viện sau:

using System.Drawing;
using System.IO;

using System.Drawing.Imaging;

Bạn viết thêm 2 hàm sau:

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 //nhấ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;

    }

Đến khi cần sử dụng hàm thì bạn gọi như sau là sẽ đồng thời thay đổi kích thước ảnh theo quy định của mình và upload ảnh đã resize lên host.

ThayDoiKichThuocAnh(Server.MapPath("~/hinh/tin/thumb/"), pChuoiNgayThang, 180, UpHinh.PostedFile.InputStream);

 

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Từng bước sao lưu dữ liệu tài khoản Facebook trước khi quá trễ

Từng bước sao lưu dữ liệu tài khoản Facebook trước khi quá trễ

Facebook - Mạng xã hội phổ biến nhất thế giới với khoảng 1,2 tỉ người dùng vừa bị mất kết nối

Bước 3: Widening Your Net of team Drupal

Bước 3: Widening Your Net of team Drupal

Eric Gaffen, Global Manager, Talent Acquisition at Acquia, is constantly getting requests for new hires from the leadership at Acquia. His previous company grew from 1600 to 3000 in six years, and from that experience, Eric knows larger companies have a very standardized approach to job descriptions which match compensation and evaluation. Things are quite different here. 

Thêm block thời tiết vào trong  Drupal Site

Thêm block thời tiết vào trong Drupal Site

One of our students is building a local tourism website. They wanted to show the local weather on the site but without any of the big, ugly branding that often accompanies weather displays.

Công ty diệt chuột T&C

 

Diet con trung