Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1

    Cách Crop hình từ một hình có sẵn theo tỉ lệ cho trước?

    Em muốn cắt hình từ một hình vừa upload lên, lưu xuống 2 file: 1 file ảnh gốc và 1 file ảnh đã crop. Em làm như cách sau mà vẫn chưa cắt hình theo đúng tỉ lệ. Xin mọi người giúp đỡ ạ.

    Mã:
            protected void Page_Load(object sender, EventArgs e)
            {
                Messages.Text = "";
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.FileName != "")
                {
                    FileUpload1.SaveAs(Server.MapPath("~/IMAGES/" + FileUpload1.FileName));
                    Crop(192, 290,"_crop"); //192,290: tỉ lệ hình cho trước
                    Messages.Text = "Upload thành công!!!";
                }
                else
                {
                    Messages.Text = "Hãy chọn ảnh cần upload!!!";
                }
    
           }
    
            public void Crop(int width, int height, string ImgName)
            {
                System.Drawing.Image oImg = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    
                double ratio = (1.0 * width)/(1.0 * height);
                double newW, newH,x,y;
                newW = oImg.Width*ratio;
                newH = oImg.Height;
                y= 0;
                x = (oImg.Width-newW)/2;
    
                Rectangle part = new Rectangle(0, 0, (int)newW, (int)newH);
    
                Bitmap bmp = new Bitmap(part.Width, part.Height);
                Graphics g = Graphics.FromImage(bmp);
                g.DrawImage(oImg, (int)x, (int)y, part, GraphicsUnit.Pixel);
                g.Dispose();
                bmp.Save(Server.MapPath("~/IMAGES/" + FileUpload1.FileName + ImgName + ".jpg"), ImageFormat.Jpeg);
                oImg.Dispose();
                bmp.Dispose();
            }

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    đã làm được. Hi hi!
    Mã:
    protected void Page_Load(object sender, EventArgs e)
            {
                Messages.Text = "";
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.FileName != "")
                {
                    FileUpload1.SaveAs(Server.MapPath("~/IMAGES/" + FileUpload1.FileName));
                    Crop(192, 290,"_crop");
                    Messages.Text = "Upload thành công!!!";
                }
                else
                {
                    Messages.Text = "Hãy chọn ảnh cần upload!!!";
                }
    
           }
    
        
            public void Crop(int width, int height, string ImgName)
            {
                System.Drawing.Image oImg = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    
                double ratio = (1.0 * width)/(1.0 * height);
                double newW, newH,x,y;
                newW = oImg.Width*ratio;
                newH = oImg.Height;
                y= 0;
                x = (oImg.Width-newW)/2;
    
                Rectangle part = new Rectangle((int)x, (int)y, (int)newW, (int)newH);
                Bitmap bmp = new Bitmap(part.Width, part.Height);
                bmp.SetResolution(oImg.VerticalResolution, oImg.HorizontalResolution);
                Graphics g = Graphics.FromImage(bmp);
                g.DrawImage(oImg, 0, 0, part, GraphicsUnit.Pixel);
                g.Dispose();
                bmp.Save(Server.MapPath("~/IMAGES/" + FileUpload1.FileName + ImgName + ".jpg"), ImageFormat.Jpeg);
                oImg.Dispose();
                bmp.Dispose();
            }

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •