ASP.NET生成高质量缩略图通用函数

在网站开发时,生成缩略图是一个非常常见和实用的功能.以前在asp里只能借助com组件实现,现在在.net里可以利用框架的强大的类库轻松实现.下面帖出完整的代码(带详细注释),参考了网上的一些文章及.net sdk相关内容.QQROOM网络家园的图片上传用到了所有的4种生成方式.

/// <summary>
     /// 生成缩略图
     /// </summary>
     /// <param name="originalImagePath">源图路径(物理路径)</param>
     /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
     /// <param name="width">缩略图宽度</param>
     /// <param name="height">缩略图高度</param>
     /// <param name="mode">生成缩略图的方式</param>
     public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
     {
       Image originalImage = Image.FromFile(originalImagePath);
       int towidth = width;
       int toheight = height;
       int x = 0;
       int y = 0;
       int ow = originalImage.Width;
       int oh = originalImage.Height;

       switch (mode)
       {
         case "HW"://指定高宽缩放(可能变形)
           break;
         case "W"://指定宽,高按比例
           toheight = originalImage.Height * width/originalImage.Width;
           break;
         case "H"://指定高,宽按比例
           towidth = originalImage.Width * height/originalImage.Height;
           break;
         case "Cut"://指定高宽裁减(不变形)
           if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
           {
             oh = originalImage.Height;
             ow = originalImage.Height*towidth/toheight;
             y = 0;
             x = (originalImage.Width - ow)/2;
           }
           else
           {
             ow = originalImage.Width;
             oh = originalImage.Width*height/towidth;
             x = 0;
             y = (originalImage.Height - oh)/2;
           }
           break;
         default :
           break;
       }
       //新建一个bmp图片
       Image bitmap = new System.Drawing.Bitmap(towidth,toheight);

       //新建一个画板
       Graphics g = System.Drawing.Graphics.FromImage(bitmap);

       //设置高质量插值法
       g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

       //设置高质量,低速度呈现平滑程度
       g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

       //清空画布并以透明背景色填充
       g.Clear(Color.Transparent);

       //在指定位置并且按指定大小绘制原图片的指定部分
       g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
         new Rectangle(x, y, ow,oh),
         GraphicsUnit.Pixel);

       try
       {
         //以jpg格式保存缩略图
         bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
       }
       catch(System.Exception e)
       {
         throw e;
       }
       finally
       {
         originalImage.Dispose();
         bitmap.Dispose();
         g.Dispose();
       }
     }

键方法Graphics.DrawImage见ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11

时间: 2024-08-22 14:32:06

ASP.NET生成高质量缩略图通用函数的相关文章

ASP.NET生成高质量缩略图通用函数(c#代码)

asp.net|函数|缩略图 在网站开发时,生成缩略图是一个非常常见和实用的功能.以前在asp里只能借助com组件实现,现在在.net里可以利用框架的强大的类库轻松实现.下面帖出完整的代码(带详细注释),参考了网上的一些文章及.net sdk相关内容.QQROOM网络家园的图片上传用到了所有的4种生成方式.         /// <summary>        /// 生成缩略图        /// </summary>        /// <param name=&

asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式

asp.net|函数|缩略图 在网站开发时,生成缩略图是一个非常常见和实用的功能.以前在asp里只能借助com组件实现,现在在.net里可以利用框架的强大的类库轻松实现.下面帖出完整的代码(带详细注释),参考了网上的一些文章及.net sdk相关内容.QQROOM网络家园的图片上传用到了所有的4种生成方式.         /**//// <summary>        /// 生成缩略图        /// </summary>        /// <param na

ASP.NET 生成高质量缩略图代码

asp.net|缩略图  private static Size NewSize(int maxWidth, int maxHeight, int width, int height)  {   double w = 0.0;   double h = 0.0;   double sw = Convert.ToDouble(width);   double sh = Convert.ToDouble(height);   double mw = Convert.ToDouble(maxWidth

C# 生成高质量缩略图程序—终极算法_实用技巧

先看代码: using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; /**//// <summary> ///  /// **生成高质量缩略图程序** ///  ///  File: GenerateThumbnail.cs ///  ///  Author: 周振兴 (Zxjay 飘遥) ///  ///  E-Mail: tda7264@163.com

C#根据大图片生成高清缩略图

  C#生成高清缩略图代码,一个C#函数模块,内含注释,后附函数参数,下面来看这个C#生成缩略图代码: 01public static void SetGoodImage(string fileName, string newFile, int maxHeight, int maxWidth,long qualitys) 02{ 03 if (qualitys == 0) 04 { 05 qualitys = 80; 06 } 07 using (System.Drawing.Image img

C#放缩、截取、合并图片并生成高质量新图的类

原文:C#放缩.截取.合并图片并生成高质量新图的类 using System;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;using System.IO; namespace Framework{public class ImageClass{        public Image ResourceImage;        public int Width=0;       

php生成高清缩略图实例详解_php技巧

本文实例讲述了php生成高清缩略图的方法.分享给大家供大家参考,具体如下: 在使用php的函数生成缩略图的使用,缩略图很多情况下都会失真,这个时候需要有一些对应的解决方法. 1.用imagecreatetruecolor和imageCopyreSampled函数分别取代imagecreate和imagecopyresized 2.给imagejpeg的第三个参数带上100(例:imagejpeg($ni,$toFile,100)) 下面是具体的函数 function CreateSmallIma

Asp.Net 上传图片并生成高清晰缩略图

  不是很复杂,大概写一下.目的只在于实现,未仔细按照标准什么的来写.其中参考了网上已经存在的代码. using System.Drawing; 页面,如图: 点击提交按钮:  代码如下   httpPostedFile hpf = UploadImage.PostedFile; //取得文件名(不含路径) string Filename = Path.GetFileName(hpf.FileName);//原文修改 if (hpf.FileName.Length < 1) { Response

Asp.Net上传图片同时生成高清晰缩略图_实用技巧

在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来分享,(效果能达到一些绘图软件的效果) 代码如下: /// <summary> /// asp.net上传图片并生成缩略图 /// </summary> /// <param name="upImage">HtmlInputFile控