Asp.net 等比例缩放后,裁剪原图中间部分
来源:互联网 发布:centos 7 服务器配置 编辑:程序博客网 时间:2024/04/29 05:09
背景:
等比例裁剪的功能,网上很多,比如限制100*100,根据原图的尺寸进行等比例裁剪,但是出来的图片的比例和原图相同,最终图片的尺寸可能是100*83,可能是85*100,对界面要求比较高的页面,显得不齐。
于是,只能自己动手了。微信朋友圈的缩略图片也是这样的模式,处理方法不同。
先看看效果图:
前端测试的布局:
<div style="background-color:#7eb8ba;"> 原图:<asp:Image ID="imgtest" runat="server" ImageUrl="~/ee.jpg" Height="700" /> 裁剪后:<asp:Image ID="Image1" runat="server" /></div><ul> <li>原图宽度:<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox></li> <li>原图高度:<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox></li> <li>缩放后宽度:<asp:TextBox ID="TextBox10" runat="server"></asp:TextBox></li> <li>缩放后高度:<asp:TextBox ID="TextBox11" runat="server"></asp:TextBox></li> <li>x轴移位:<asp:TextBox ID="TextBox12" runat="server"></asp:TextBox></li> <li>裁剪后图片路径:<asp:TextBox ID="TextBox13" runat="server"></asp:TextBox></li> <li><asp:Button ID="TextBox7" runat="server" Text="裁剪图片" OnClick="TextBox7_Click"></asp:Button></li></ul>后端代码:
protected void TextBox7_Click(object sender, EventArgs e){ System.IO.FileStream file = System.IO.File.Open("E:/Project/Test/Web/ee.jpg", System.IO.FileMode.Open);//原图暂时固定路径的 System.IO.Stream strea = file; MakeSmallImg(strea, "E:/Project/Test/Web/1.jpg", 200, 200);//生成后的缩略图也固定路径的 file.Close();}/// <summary>/// 生成缩略图/// 对于宽高不同的情况,从中间部分裁剪///注:缩略图大小控制在模版区域内/// </summary>/// <param name="fromFileStream">源图文件流</param>/// <param name="fileSaveUrl">缩略图存放地址</param>/// <param name="templateWidth">缩略图的宽度</param>/// <param name="templateHeight">缩略图的高度</param>private void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight){ //从文件取得图片对象,并使用流中嵌入的颜色管理信息 System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true); TextBox8.Text = myImage.Width.ToString(); TextBox9.Text = myImage.Height.ToString(); //缩略图宽、高 System.Double newWidth = myImage.Width, newHeight = myImage.Height; //宽度大于高度的横图 ,以宽度为准,这样才能裁剪中间部分 int x = 0, y = 0; if (myImage.Width >= myImage.Height) { if (myImage.Width > templateWidth)//仅处理缩小的情况 { //高按模版,宽按比例缩放 //原高度除以新高度,得到缩小的比例,新宽度乘以比例,得到新图片应该的宽度,(老宽度-新宽度)/2,得到应该移位的宽度 x = Convert.ToInt32(myImage.Width - templateWidth * (myImage.Height / templateHeight)) / 2; newHeight = templateHeight; newWidth = myImage.Width * (newHeight / myImage.Height); TextBox10.Text = newWidth.ToString();//测试用,可删除 TextBox11.Text = newHeight.ToString();//测试用,可删除 TextBox12.Text = x.ToString();//测试用,可删除 } } //高大于模版的竖图 else { if (myImage.Height > templateHeight)//仅处理缩小的情况 { //宽按模版,高按比例缩放 y = Convert.ToInt32(myImage.Height - templateHeight * (myImage.Width / templateWidth)) / 2; newWidth = templateWidth; newHeight = (newWidth * myImage.Height) / myImage.Width; TextBox10.Text = newWidth.ToString();//测试用,可删除 TextBox11.Text = newHeight.ToString();//测试用,可删除 TextBox12.Text = x.ToString();//测试用,可删除 } } //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)templateWidth, (int)templateHeight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空一下画布 g.Clear(Color.White); //在指定位置画图 g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, (int)newWidth, (int)newHeight), new System.Drawing.Rectangle(x,y, myImage.Width, myImage.Height),//这是移位,保证裁剪的是图片的中间部分 System.Drawing.GraphicsUnit.Pixel); //文字水印 //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap); //System.Drawing.Font f=new Font("宋体",10); //System.Drawing.Brush b=new SolidBrush(Color.Black); //G.DrawString("abc",f,b,10,10); //G.Dispose(); ///图片水印 //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif")); //Graphics a = Graphics.FromImage(bitmap); //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); //copyImage.Dispose(); //a.Dispose(); //copyImage.Dispose(); //保存缩略图 bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); TextBox13.Text = fileSaveUrl;//测试用,可删除 Image1.ImageUrl = "1.jpg";//测试用,可删除 g.Dispose(); myImage.Dispose(); bitmap.Dispose();}
水印应该用不到,我也没测试。
0 0
- Asp.net 等比例缩放后,裁剪原图中间部分
- 图片等比例缩放后放入容器
- java图片处理,裁剪,合图,等比例缩放
- java图片处理,裁剪,合图,等比例缩放
- iOS 图片虚化。裁剪。等比例缩放
- ASP.NET C# 按原图片大小等比例缩放生成缩略图
- ASP.NET中FileUpload控件上传图片时等比例缩放,然后保存到数据库
- 图片等比例缩放
- 等比例缩放图片大小
- ASPJPEGD等比例缩放
- 图片等比例缩放
- 图片等比例缩放
- 分辨率等比例缩放
- 图片等比例缩放
- 图片等比例缩放
- 图片等比例缩放
- 视频等比例缩放
- 等比例缩放图片
- hadoop学习序曲之java基础篇--java常用类
- maridb 10.0 在线ddl修改字段测试
- Java开发之常量池概念
- php5.4之pdo连接sqlserver
- 关于Java中线程池的解读
- Asp.net 等比例缩放后,裁剪原图中间部分
- 照片EXIF信息库EXIV2(c++)
- fragmentManager Androird对Fragemt的管理
- 顺序存储结构与链式存储结构的比较(也可以说的顺序表与链表的比较)
- 深入浅出——搞懂卷积神经网络误差分析(一)
- hadoop学习序曲之java基础篇--java集合类
- Object-C 和 lua的相互调用
- Android基础:Android权限
- Bash的位置参数: