-
14-09-2011, 05:02 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
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(); }
View more random threads:
- Không insert được dữ liệu SQL Server trong asp.net, cũng không báo lỗi?
- lỗi dropdownlist không lấy được selectvalue
- Cách quay lại trang vừa duyệt qua như nút back trên fireFox
- Lỗi facebook bắt xác nhận đường link
- [HandleError] trước mỗi hàm ASP.NET có ý nghĩa gì?
- Kéo dài session trong MVC
- Không tìm thấy Stored procedure khi làm việc trên ASP.NET.
- Cách truyền giá trị của textbox vào tham số của controller ASPNET MVC
- Rút gọn Text trong hyperlink ASP.NET?
- Lỗi không chạy dc web (viết theo mô hình 3 lớp)trên Host somee.com?
-
15-09-2011, 04:20 PM #2Junior Member
- 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(); }
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...