[HELP] Resize ảnh khi upload C#

Code dưới đây em đã thêm dc vào DB và show được lên gridview rồi. nhưng show lên thì ảnh to đúng, giữ nguyên size gốc. GIờ em muốn resize cho vừa đẹp để hiển thị lên gridview trong webform ạ
Thanks~~
Mã:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {


    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        SqlDsSanpham.InsertParameters["id_sanpham"].DefaultValue = txtid_sanpham.Text;
        SqlDsSanpham.InsertParameters["id_danhmuc"].DefaultValue = DV1.SelectedValue;
        SqlDsSanpham.InsertParameters["ten_sanpham"].DefaultValue = txt_tensanpham.Text;
        SqlDsSanpham.InsertParameters["img_sanpham"].DefaultValue = Upload.FileName.ToString();
        SqlDsSanpham.InsertParameters["gia_sanpham"].DefaultValue = txt_giasanpham.Text;
        SqlDsSanpham.InsertParameters["des_sanpham"].DefaultValue = txt_chitiet.Text;


        string filename = Path.GetFileName(Upload.PostedFile.FileName);
        Upload.SaveAs(Server.MapPath("~/images/" + filename));


        try
        {
            SqlDsSanpham.Insert();
            txtid_sanpham.Text = "";
            DV1.Items.Add("--Chọn một danh mục--");
            DV1.DataBind();
            txt_tensanpham.Text = "";
            txt_giasanpham.Text = "";
            txt_chitiet.Text = "";
            lblthongbao.Text = "Thêm thành công.<br/><br/>";
            GridView1.DataBind();
            lblthongbao.Text = "Thêm thành công.<br/><br/>";
        }
        catch (Exception ex)
        {
            lblthongbao.Text = "Một lỗi đã xảy ra, Vui lòng kiểm tra lại.<br/><br/>";
        }
    }
}
 
Đã có code xử lý ảnh đâu bạn mới có code save file lưu lên server

Đây là một phần code mình viết dùng DrawImage để xử lý Resize ảnh mà vãn giữ tỉ lệ kích thước ban đầu dựa trên vùng chọn trong sản phẩm Anime Media Center

Mã:
        private Image ResizeImage(Image image, int width, int height) // giu ti le khi thay doi kich thuoc hinh anh
        {
            // khoi tao hinh anh dich
            Bitmap newImage = new Bitmap(width, height); // thiet lap kich thuoc
            Graphics graphics = Graphics.FromImage((Image)newImage); // bat dau chinh sua


            // thong so nguon
            int posX = 0; // toa do X
            int posY = 0; // toa do Y
            int srcWidth = image.Width; // chieu rong
            int srcHeight = image.Height; // chieu cao


            // ti le hien thi
            double srcRatio = (double)(srcWidth / srcHeight); // ti le nguon
            double descRatio = (double)(width / height); // ti le dich


            // tinh toan lai thong so nguon cho phu hop


            if (srcRatio < descRatio) // ti le nguon < dich, chieu cao anh xa be hon
            {
                int newHeight = (int)(srcWidth * height / width); // tinh chieu cao anh xa
                posY = (int)((srcHeight - newHeight) / 2); // tinh toa do Y moi
                srcHeight = newHeight; // thiet lap chieu cao moi
            }


            if (srcRatio > descRatio) // ti le nguon > dich, chieu rong anh xa be hon
            {
                int newWidth = (int)(srcHeight * width / height); // tinh chieu rong anh xa
                posX = (int)((srcWidth - newWidth) / 2); // tinh toa do X moi
                srcWidth = newWidth; // thiet lap chieu rong moi
            }


            // tien hanh chinh sua
            Rectangle srcRectangle = new Rectangle(posX, posY, srcWidth, srcHeight); // thong so nguon
            Rectangle descRectangle = new Rectangle(0, 0, width, height); // thong so dich
            graphics.DrawImage(image, descRectangle, srcRectangle, GraphicsUnit.Pixel); // ve lai hinh anh
            graphics.Dispose(); // ket thuc chinh sua


            // tra lai hinh anh moi
            return (Image)newImage;
        }
 
Top