asp.net 高质量缩略图

简介:
2012-7-12:
复制代码
  protected  void Page_Load( object sender, EventArgs e)
 {
      string OriginalImgPath =  @" C:\Users\LHTC\Desktop\ttttimg\ttttimg\imgtt\tt.jpg ";
   //    string savepath = Path.ChangeExtension(OriginalImgPath, "jpg");
      string savepath =  @" C:\Users\LHTC\Desktop\ttttimg\ttttimg\imgtt\tt1.jpg ";
     System.Drawing.Image image = System.Drawing.Image.FromFile(OriginalImgPath);

      // System.Web.HttpPostedFile postedFile=Request.Files[0];
     
// System.Drawing.Image originalImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);  

     SaveJpg(image, savepath,  1578475L);
     Response.Write( " ok ");
 }

  ///   <summary>
 
///   自适应按比例 上传保存
 
///   </summary>
 
///   <param name="OriginalImgPath"> 上传图片路径 </param>
 
///   <param name="SavePath"> 保存路径 </param>
 
///   <param name="Width"> 缩略图宽 </param>
 
///   <param name="Height"> 缩略图高 </param>
 
///   <param name="Quality"> 质量 75L </param>
  private  static  void SaveJpg(System.Drawing.Image OriginalImage,  string SavePath,  int Width,  int Height,  long Quality)
 {
      var rotatedImage = OriginalImage.Clone()  as System.Drawing.Image;
     Size thumbnailSize = GetThumbnailSize(rotatedImage, Width, Height);
     Bitmap newImage =  new Bitmap(thumbnailSize.Width, thumbnailSize.Height, PixelFormat.Format24bppRgb);
     newImage.SetResolution( 7272);
      using (Graphics gr = Graphics.FromImage(newImage))
     {
         gr.Clear(Color.Transparent);
         gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
         gr.SmoothingMode = SmoothingMode.AntiAlias;
         gr.CompositingQuality = CompositingQuality.HighQuality;
         gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
         gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
          using ( var attribute =  new ImageAttributes())
         {
             attribute.SetWrapMode(WrapMode.TileFlipXY);
             gr.DrawImage(OriginalImage,  new Rectangle( new Point( 00), thumbnailSize),  00, OriginalImage.Width, OriginalImage.Height, GraphicsUnit.Pixel, attribute);
         }
     }
     ImageCodecInfo myImageCodecInfo = GetEncoderInfo( " image/jpeg ");
     Encoder myEncoder = Encoder.Quality;
     EncoderParameters myEncoderParameters =  new EncoderParameters( 1);
     EncoderParameter myEncoderParameter =  new EncoderParameter(myEncoder, Quality);
     myEncoderParameters.Param[ 0] = myEncoderParameter;
     newImage.Save(SavePath, myImageCodecInfo, myEncoderParameters);
 }


  // 根据长宽自适应 按原图比例缩放 
  private  static Size GetThumbnailSize(System.Drawing.Image original,  int desiredWidth,  int desiredHeight)
 {
      var widthScale = ( double)desiredWidth / original.Width;
      var heightScale = ( double)desiredHeight / original.Height;
      var scale = widthScale < heightScale ? widthScale : heightScale;
      return  new Size
                    {
                        Width = ( int)(scale * original.Width),
                        Height = ( int)(scale * original.Height)
                    };
 }
  private  static ImageCodecInfo GetEncoderInfo(String mimeType)
 {
      int j;
     ImageCodecInfo[] encoders;
     encoders = ImageCodecInfo.GetImageEncoders();
      for (j =  0; j < encoders.Length; ++j)
     {
          if (encoders[j].MimeType == mimeType)
              return encoders[j];
     }
      return  null;
 }
复制代码


 public static void imgSmo(string strPath, string strName, string strType, int i)
    {
        try
        {              
            string path = strPath + strName + strType;
            System.Drawing.Image oImage = System.Drawing.Image.FromFile(path);

            Size imageSize = GetImageSize(oImage,160,70);
int//原图宽度int//原图高度new//质量newnew//JPEGnewlongnewlongnewnullforintifJPEGbreakstringifnullelsecatchthrownewpublicstaticintintnew//初始化//按比例缩小放大doubledoubledoubledoubleintintififreturn

此颜色量化类库的使用,也非常简单:

using (Bitmap bmp = new Bitmap(...))
 {
    OctreeQuantizer quantizer = new OctreeQuantizer ( 255 , 8 ) ;
//两个参数为颜色值,和颜色深度
using ( Bitmap quantized = quantizer.Quantize ( image ) )
        {
Response.ContentType = "image/gif" ;
quantized.Save ( Response.OutputStream , ImageFormat.Gif ) ;
}
}


本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2009/09/17/1568535.html ,如需转载请自行联系原作者
相关文章
|
.NET Shell 开发框架
asp.net显示高清缩略图
如题:   添加命名空间如下: using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.
1253 0
|
.NET 数据库 开发框架
|
.NET 开发框架 C#
Asp.net缩略图代码
前台页面:WebForm1.aspx 无标题页 要上传的图片 ...
774 0
|
.NET 开发框架
如何实现ASP上传图片自动生成缩略图
'lj1 原图路径 'tu 图片名称 'lj2 缩 略图 存放路径 function CreatePic(lj1,tu,lj2) dim Jpeg,Path Set Jpeg = Server.
899 0
|
.NET 开发框架 C#
在ASP.NET里轻松实现缩略图
以前,在页面上实现缩略图必须借助第三方组件。现在,有了.NET,就可以很轻松地实现缩略图。下面就是实现缩略图的例子。 查看例子 http://dotnet.aspx.cc/Exam/Thumbnail.
665 0
|
.NET 数据安全/隐私保护
asp.net 生成缩略图、为图片添加文字水印、图片水印等功能
最近挺嫌的荒,不知道写点什么好,就在网上找了个类似的例子,自己重新写了一遍,稍微改了一下。希望对大家有用 实现了生成缩略图、为图片添加文字水印、图片水印等功能 源代码打包下载:http://files.
1332 0