• asp.net生成缩略图


    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    //using System.IO;
    namespace ImageBll
    {
        public class ImageUtility
        {
            /// <summary>
            /// 生成缩略图的方法
            /// </summary>
            /// <param name="Path">原始图片路径</param>
            /// <param name="StPath">生成图片存放的路径</param>
            /// <param name="width">生成图片的宽度</param>
            /// <param name="height">生成图片的高度</param>
            /// <param name="mode">生成的模式( "HW":指定高宽缩放;"W"://指定宽度,计算缩略图;"H":指定高度,计算缩略图;"CUT":)</param>
            /// <returns></returns>
            public static  string  MakeThumbImage(string Path, string StPath, int width, int height, string mode)
            {
                string messages = string.Empty;
                System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
                int tw = width;
                int th = height;
                //原始图片的宽度和高度
                int sw = image.Width;
                int sh = image.Height;
                int x = 0, y = 0;
                switch (mode)
                {
                    case "HW"://指定高宽缩放
                        break;
                    case"W"://按比例缩放,指定宽度,计算缩略图的高度
                        th = image.Height * width / image.Width;
                        break;
                    case "H"://按比例缩放,指定高度,计算缩略图的高度
                        tw = image.Width * height / image.Height;
                        break;
                    case "CUT":
                        if ((double)tw / (double)th < (double)width / (double)height)
                        {
                            sw = image.Width;
                            sh = image.Width * height / tw;
                            x = 0;
                            y = (image.Height - sh) / 2;
                        }
                        else
                        {
                            sh = image.Height;
                            sw = image.Height * width / th;
                            y = 0;
                            x = (image.Width - sw) / 2;
                        }
                        break;
                    default: break;
                }
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th);
                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(System.Drawing.Color.Transparent);
                g.DrawImage(image,
                    new System.Drawing.Rectangle(x, y, tw, th),
                    new System.Drawing.Rectangle(x, y, sw, sh), 
                    System.Drawing.GraphicsUnit.Pixel);
                try
                {
                    bitmap.Save(StPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                catch (Exception ex)
                {
                    messages= ex.Message;
                }
                finally
                {
                    image.Dispose(); 
                    bitmap.Dispose();
                    g.Dispose();
                    if (messages == string.Empty)
                    {
                        messages = "成功生成!";
                    }
                    
                }

                return messages;
            }
        }
    }
  • 相关阅读:
    tail,more查看日志(定点和翻页)
    Python:浅拷贝和深拷贝
    mybatis sql查子list
    mybatis在insert中获取到id
    mvn安装jar到本地仓库
    微信支付wxpay -- 移动端app第二章节 -- java 后端代码
    微信支付wxpay -- 移动端app第一章节 -- 注意点
    java字符串大小写转化
    Zxing图片右下角生成二维码
    switchTap、navigateTo、switchTap
  • 原文地址:https://www.cnblogs.com/zhiji6/p/1649353.html
Copyright © 2020-2023  润新知