zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    java jdbc笔记整理
    Spring IOC (构造器注入)
    WEB-INF目录与META-INF目录的作用[转]
    [转]LL(1)文法判别之First集合、Follow集合、Select集合求法
    java读取TXT文件的方法 (转)
    Ubuntu 12.04下搭建Web服务器 (MySQL+PHP+Apache)(转)
    题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
    error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCR...(转)
    汇编leal命令的灵活用法及理解
    C++之继承
  • 原文地址:https://www.cnblogs.com/zhiji6/p/1649353.html
Copyright © 2011-2022 走看看