.net等比缩放生成缩略图的方法

生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形

/// <summary>
   /// 为图片生成缩略图
   /// </summary>
   /// <param name="phyPath">原图片的路径</param>
   /// <param name="width">缩略图宽</param>
   /// <param name="height">缩略图高</param>
   /// <returns></returns>
   public System.Drawing.Image GetHvtThumbnail(System.Drawing.Image image, int width, int height)
   {
     Bitmap m_hovertreeBmp = new Bitmap(width, height);
     //从Bitmap创建一个System.Drawing.Graphics
     Graphics m_HvtGr = Graphics.FromImage(m_hovertreeBmp);
     //设置
     m_HvtGr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     //下面这个也设成高质量
     m_HvtGr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
     //下面这个设成High
     m_HvtGr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
     //把原始图像绘制成上面所设置宽高的缩小图
     Rectangle rectDestination = new Rectangle(0, 0, width, height);

     int m_width, m_height;
     if (image.Width * height > image.Height * width)
     {
       m_height = image.Height;
       m_width = (image.Height * width) / height;
     }
     else
     {
       m_width = image.Width;
       m_height = (image.Width * height) / width;
     }

     m_HvtGr.DrawImage(image, rectDestination, 0, 0, m_width, m_height, GraphicsUnit.Pixel);

     return m_hovertreeBmp;
   }

C#缩略图生成类,采用高质量插值法实现缩略图生成,高质量,低速度呈现平滑程度,可以保持缩略图纵横比,在获取缩略图的时候一开始就根据百分比获取图片的尺寸、根据设定的大小返回图片的大小,并高质量保存缩略图图片,为原图片设置EncoderParameters 对象。

以下为类文件,建议保存文件名为:ImageHelper.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
namespace HtmlSnap
{
  public static class ImageHelper
  {
    /// 获取缩略图
    public static Image GetThumbnailImage(Image image, int width, int height)
    {
      if (image == null || width < 1 || height < 1)
        return null;
      // 新建一个bmp图片
      Image bitmap = new System.Drawing.Bitmap(width, height);

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

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

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

        // 高质量、低速度复合
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

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

 // 在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(image, new Rectangle(0, 0, width, height),
          new Rectangle(0, 0, image.Width, image.Height),
          GraphicsUnit.Pixel);
        return bitmap;
      }
    }
    /// <summary>
    /// 生成缩略图,并保持纵横比
    /// </summary>
    /// <returns>生成缩略图后对象</returns>
    public static Image GetThumbnailImageKeepRatio(Image image, int width, int height)
    {
      Size imageSize = GetImageSize(image, width, height);
      return GetThumbnailImage(image, imageSize.Width, imageSize.Height);
    }

    /// <summary>
    /// 根据百分比获取图片的尺寸
    /// </summary>
    public static Size GetImageSize(Image picture, int percent)
    {
      if (picture == null || percent < 1)
        return Size.Empty;

      int width = picture.Width * percent / 100;
      int height = picture.Height * percent / 100;

      return GetImageSize(picture, width, height);
    }
    /// <summary>
    /// 根据设定的大小返回图片的大小,考虑图片长宽的比例问题
    /// </summary>
    public static Size GetImageSize(Image picture, int width, int height)
    {
      if (picture == null || width < 1 || height < 1)
        return Size.Empty;
      Size imageSize;
      imageSize = new Size(width, height);
      double heightRatio = (double)picture.Height / picture.Width;
      double widthRatio = (double)picture.Width / picture.Height;
      int desiredHeight = imageSize.Height;
      int desiredWidth = imageSize.Width;
      imageSize.Height = desiredHeight;
      if (widthRatio > 0)
        imageSize.Width = Convert.ToInt32(imageSize.Height * widthRatio);
      if (imageSize.Width > desiredWidth)
      {
        imageSize.Width = desiredWidth;
        imageSize.Height = Convert.ToInt32(imageSize.Width * heightRatio);
      }
      return imageSize;
    }
    /// <summary>
    /// 获取图像编码解码器的所有相关信息
    /// </summary>
    /// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
    /// <returns>返回图像编码解码器的所有相关信息</returns>
    public static ImageCodecInfo GetCodecInfo(string mimeType)
    {
      ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
      foreach (ImageCodecInfo ici in CodecInfo)
      {
        if (ici.MimeType == mimeType) return ici;
      }
      return null;
    }
    public static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
    {
      ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
      foreach (ImageCodecInfo icf in encoders)
      {
        if (icf.FormatID == format.Guid)
        {
          return icf;
        }
      }
      return null;
    }
    public static void SaveImage(Image image, string savePath, ImageFormat format)
    {
      SaveImage(image, savePath, GetImageCodecInfo(format));
    }
    /// <summary>
    /// 高质量保存图片
    /// </summary>
    private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
    {
      // 设置 原图片 对象的 EncoderParameters 对象
      EncoderParameters parms = new EncoderParameters(1);
      EncoderParameter parm = new EncoderParameter(Encoder.Quality, ((long)95));
      parms.Param[0] = parm;
      image.Save(savePath, ici, parms);
      parms.Dispose();
    }

  }
}
(0)

相关推荐

  • 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#多图片上传并生成缩略图的实例代码

    前台代码: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&q

  • c#图片缩放图片剪切功能实现(等比缩放)

    所谓c#图片处理高级应,多数是基于.net framework类库完成 复制代码 代码如下: using system;using system.collections.generic;using system.text;using system.io;using system.drawing;using system.drawing.drawing2d;using system.drawing.imaging; namespace wujian.common{    /// <summary>

  • c#生成缩略图不失真的方法实例分享

    复制代码 代码如下: /// <summary>/// 获得缩微图/// </summary>/// <returns></returns>  public bool GetThumbImg(){try{string imgpath; //原始路径     if(imgsourceurl.IndexOf("\",0)<0) //使用的是相对路径     {imgpath = HttpContext.Current.Server.Ma

  • php实现按指定大小等比缩放生成上传图片缩略图的方法

    本文实例讲述了php实现按指定大小等比缩放生成上传图片缩略图的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: /**  * *  *等比缩放  * @param unknown_type $srcImage   源图片路径  * @param unknown_type $toFile     目标图片路径  * @param unknown_type $maxWidth   最大宽  * @param unknown_type $maxHeight  最大高  * @par

  • C#获取视频某一帧的缩略图的方法

    本文实例讲述了C#获取视频某一帧的缩略图的方法.分享给大家供大家参考.具体实现方法如下: 读取方式:使用ffmpeg读取,所以需要先下载ffmpeg.网上资源有很多. 原理是通过ffmpeg执行一条命令获取视频某一帧的缩略图. 首先,需要获取视频的帧高度和帧宽度,这样获取的缩略图才不会变形. 获取视频的帧高度和帧宽度可以参考:http://www.jb51.net/article/57475.htm. 获取到视频的帧高度和帧宽度后,还需要获取缩略图的高度和宽度,这是按比例缩放的. 比如你存放缩略

  • c#生成图片缩略图的类(2种实现思路)

    第一种 复制代码 代码如下: /**//// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width&quo

  • .net等比缩放生成缩略图的方法

    生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 /// </summary> /// <param name="phyPath">原图片的路径</param> /// <param name="width">缩略图宽</param> /// <param name=&quo

  • C#简单生成缩略图的方法

    本文实例讲述了C#简单生成缩略图的方法.分享给大家供大家参考.具体实现方法如下: /// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <para

  • python使用pil生成缩略图的方法

    本文实例讲述了python使用pil生成缩略图的方法.分享给大家供大家参考.具体分析如下: 这段代码实现python通过pil生成缩略图的功能,会强行将图片大小修改成250x156 from PIL import Image img = Image.open('jb51.jpg') img = img.resize((250, 156), Image.ANTIALIAS) img.save('jb51_small.jpg') 希望本文所述对大家的Python程序设计有所帮助.

  • Go语言图片处理和生成缩略图的方法

    本文实例讲述了Go语言图片处理和生成缩略图的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: package main import (     "fmt"     "os"     "image"     "image/color"     "image/draw"     "image/jpeg" ) func main() {     f1, err := os

  • ASP.NET实现上传图片并生成缩略图的方法

    本文实例讲述了ASP.NET实现上传图片并生成缩略图的方法.分享给大家供大家参考,具体如下: protected void bt_upload_Click(object sender, EventArgs e) { //检查上传文件的格式是否有效 if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { Response.Write("上传图片格式无效!"); re

  • Thinkphp调用Image类生成缩略图的方法

    本文实例讲述了Thinkphp调用Image类生成缩略图的方法.分享给大家供大家参考.具体分析如下: Thinkphp的Image类 在ThinkPHP/Extend/Library/ORG/Util/Image.class.php中. 调用方法如下: import("ORG.Util.Image"); $Img = new Image();//实例化图片类对象 $image_path = './图片路径'; //若当前php文件在Thinkphp的中APP_PATH路径中 //'./

  • php实现根据url自动生成缩略图的方法

    本文实例讲述了php实现根据url自动生成缩略图的方法,是非常实用的功能.分享给大家供大家参考.具体方法如下: 原理:设置apache rewrite ,当图片不存在时,调用php创建图片. 例如: 原图路径为:http://localhost/upload/news/2013/07/21/1.jpg 缩略图路径为:http://localhost/supload/news/2013/07/21/1.jpg 当访问 http://localhost/supload/news/2013/07/21

  • PHP基于ffmpeg实现转换视频,截图及生成缩略图的方法

    本文实例讲述了PHP基于ffmpeg实现转换视频,截图及生成缩略图的方法.分享给大家供大家参考,具体如下: 这里把ffmpeg 和  生成缩略图整合了一下: include("ImageResize.class.php") //转视频 $cmd="ffmpeg.exe -i starwar.avi -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 1.flv"; exec($cmd); //视频截图 $cmd="ffmpeg

  • PHP实现原比例生成缩略图的方法

    本文实例讲述了PHP实现原比例生成缩略图的方法.分享给大家供大家参考,具体如下: <?php $image = "jiequ.jpg"; // 原图 $imgstream = file_get_contents($image); $im = imagecreatefromstring($imgstream); $x = imagesx($im);//获取图片的宽 $y = imagesy($im);//获取图片的高 // 缩略后的大小 $xx = 140; $yy = 200;

  • AJAX实现图片预览与上传及生成缩略图的方法

    要实现功能,上传图片时可以预览,因还有别的文字,所以并不只上传图片,实现与别的文字一起保存,当然上来先上传图片,然后把路径和别的文字一起写入数据库:同时为 图片生成缩略图,现只写上传图片方法,文字在ajax里直接传参数就可以了,若要上传多图,修改一下就可以了. 借鉴了网上资料,自己写了一下,并不需要再新加页面,只在一个页面里就OK啦. JS代码: //ajax保存数据,后台方法里实现此方法 function SaveData() { filename = document.getElementB

随机推荐