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