PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ASP.NET教程 > 正文

    Asp.net 等比例缩放后,裁剪原图中间部分

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:等比例裁剪的功能,网上很多,比如限制100*100,根据原图的尺寸进行等比例裁剪,但是出来的图片的比例和原图相同,最终图片的尺寸可能是100...
    等比例裁剪的功能,网上很多,比如限制100*100,根据原图的尺寸进行等比例裁剪,但是出来的图片的比例和原图相同,最终图片的尺寸可能是100*83,可能是85*100,对界面要求比较高的页面,显得不齐。

    于是,只能自己动手了。微信朋友圈的缩略图片也是这样的模式,处理方法不同。

    先看看效果图:

    Asp.net 等比例缩放后,裁剪原图中间部分

     

    前端测试的布局:
     

    1. <div style="background-color:#7eb8ba;"> 
    2.     原图:<asp:Image ID="imgtest" runat="server" ImageUrl="~/ee.jpg" Height="700" /> 
    3.     裁剪后:<asp:Image ID="Image1" runat="server" /> 
    4. </div> 
    5. <ul> 
    6.     <li>原图宽度:<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox></li> 
    7.     <li>原图高度:<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox></li> 
    8.     <li>缩放后宽度:<asp:TextBox ID="TextBox10" runat="server"></asp:TextBox></li> 
    9.     <li>缩放后高度:<asp:TextBox ID="TextBox11" runat="server"></asp:TextBox></li> 
    10.     <li>x轴移位:<asp:TextBox ID="TextBox12" runat="server"></asp:TextBox></li> 
    11.     <li>裁剪后图片路径:<asp:TextBox ID="TextBox13" runat="server"></asp:TextBox></li> 
    12.     <li><asp:Button ID="TextBox7"  runat="server" Text="裁剪图片" OnClick="TextBox7_Click"></asp:Button></li> 
    13. </ul> 

    后端代码:
     

    1. protected void TextBox7_Click(object sender, EventArgs e) 
    2.     System.IO.FileStream file = System.IO.File.Open("E:/Project/Test/Web/ee.jpg", System.IO.FileMode.Open);//原图暂时固定路径的 
    3.     System.IO.Stream strea = file; 
    4.  
    5.     MakeSmallImg(strea, "E:/Project/Test/Web/1.jpg", 200, 200);//生成后的缩略图也固定路径的 
    6.     file.Close(); 
    7. /// <summary> 
    8. /// 生成缩略图 
    9. /// 对于宽高不同的情况,从中间部分裁剪 
    10. ///注:缩略图大小控制在模版区域内 
    11. /// </summary> 
    12. /// <param name="fromFileStream">源图文件流</param> 
    13. /// <param name="fileSaveUrl">缩略图存放地址</param> 
    14. /// <param name="templateWidth">缩略图的宽度</param> 
    15. /// <param name="templateHeight">缩略图的高度</param> 
    16. private  void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight) 
    17.     //从文件取得图片对象,并使用流中嵌入的颜色管理信息  
    18.     System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true); 
    19.     TextBox8.Text = myImage.Width.ToString(); 
    20.     TextBox9.Text = myImage.Height.ToString(); 
    21.     //缩略图宽、高  
    22.     System.Double newWidth = myImage.Width, newHeight = myImage.Height; 
    23.     //宽度大于高度的横图 ,以宽度为准,这样才能裁剪中间部分 
    24.     int x = 0, y = 0; 
    25.     if (myImage.Width >= myImage.Height) 
    26.     { 
    27.         if (myImage.Width > templateWidth)//仅处理缩小的情况 
    28.         { 
    29.             //高按模版,宽按比例缩放  
    30.             //原高度除以新高度,得到缩小的比例,新宽度乘以比例,得到新图片应该的宽度,(老宽度-新宽度)/2,得到应该移位的宽度 
    31.             x = Convert.ToInt32(myImage.Width - templateWidth * (myImage.Height / templateHeight)) / 2; 
    32.             newHeight = templateHeight; 
    33.             newWidth = myImage.Width * (newHeight / myImage.Height); 
    34.  
    35.             TextBox10.Text = newWidth.ToString();//测试用,可删除 
    36.             TextBox11.Text = newHeight.ToString();//测试用,可删除 
    37.             TextBox12.Text = x.ToString();//测试用,可删除 
    38.         } 
    39.     } 
    40.     //高大于模版的竖图  
    41.     else 
    42.     { 
    43.         if (myImage.Height > templateHeight)//仅处理缩小的情况 
    44.         { 
    45.             //宽按模版,高按比例缩放  
    46.             y = Convert.ToInt32(myImage.Height - templateHeight * (myImage.Width / templateWidth)) / 2; 
    47.             newWidth = templateWidth; 
    48.             newHeight = (newWidth * myImage.Height) / myImage.Width; 
    49.  
    50.             TextBox10.Text = newWidth.ToString();//测试用,可删除 
    51.             TextBox11.Text = newHeight.ToString();//测试用,可删除 
    52.             TextBox12.Text = x.ToString();//测试用,可删除 
    53.         } 
    54.     } 
    55.     //新建一个bmp图片  
    56.     System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)templateWidth, (int)templateHeight); 
    57.     //新建一个画板  
    58.     System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 
    59.     //设置高质量插值法  
    60.     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
    61.     //设置高质量,低速度呈现平滑程度  
    62.     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
    63.     //清空一下画布  
    64.     g.Clear(Color.White); 
    65.     //在指定位置画图  
    66.  
    67.     g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, (int)newWidth, (int)newHeight), 
    68.             new System.Drawing.Rectangle(x,y, myImage.Width, myImage.Height),//这是移位,保证裁剪的是图片的中间部分 
    69.             System.Drawing.GraphicsUnit.Pixel); 
    70.     //文字水印  
    71.     //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);  
    72.     //System.Drawing.Font f=new Font("宋体",10);  
    73.     //System.Drawing.Brush b=new SolidBrush(Color.Black);  
    74.     //G.DrawString("abc",f,b,10,10);  
    75.     //G.Dispose();  
    76.     ///图片水印  
    77.     //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));  
    78.     //Graphics a = Graphics.FromImage(bitmap);  
    79.     //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);  
    80.     //copyImage.Dispose();  
    81.     //a.Dispose();  
    82.     //copyImage.Dispose();  
    83.     //保存缩略图  
    84.     bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); 
    85.  
    86.     TextBox13.Text = fileSaveUrl;//测试用,可删除 
    87.     Image1.ImageUrl = "1.jpg";//测试用,可删除 
    88.  
    89.     g.Dispose(); 
    90.     myImage.Dispose(); 
    91.     bitmap.Dispose(); 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-11-4941-1.html
    相关热词搜索: