• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
悠
閒

博客园    首页       联系   管理    订阅  订阅
ASP.NET生成缩略图源码
先看调用的方法:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class test_Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        generalImage();
    }
    
private void generalImage()
    {
        
string savePath = @"images/123.jpg";//原图路径
        string smallPath = @"images/small/123.jpg";//要生成的小图
        savePath = Server.MapPath(savePath);//这一步别忘了。
        smallPath = Server.MapPath(smallPath);//同样这里也不要忘了。
        YD.Common.ImageClass imageClass = new YD.Common.ImageClass();
        imageClass.ShowThumbnail(savePath, smallPath, 
200, 153);//这样调用
    }
}
生成小图的类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
/// <summary>
/// ImageClass 的摘要说明
/// </summary>
namespace YD.Common
{
    
public class ImageClass
    {
        
public bool ThumbnailCallback()
        {
            
return false;
        }
        
public void ShowThumbnail(string oldfile, string newfile, int white, int height)
        {
            System.Drawing.Image image 
= System.Drawing.Image.FromFile(oldfile);
            
//获取原图高度和宽度
            int oldh = image.Height;
            
int oldw = image.Width;
            
int neww, newh;
            neww 
= white; newh = height;   //直接设定新图的高宽,,

            
try
            {
                System.Drawing.Image.GetThumbnailImageAbort callb 
= new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                System.Drawing.Image bt 
= new System.Drawing.Bitmap(neww, newh);
                System.Drawing.Graphics gr 
= System.Drawing.Graphics.FromImage(bt);
                gr.Clear(Color.White);
                gr.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gr.DrawImage(image, 
new Rectangle(0, 0, neww, newh), 0, 0, oldw, oldh, GraphicsUnit.Pixel);
                
switch (oldfile.Substring(oldfile.Length - 3).ToUpper())
                {
                    
case "JPG":
                        bt.Save(newfile, ImageFormat.Jpeg);
                        
break;
                    
case "GIF":
                        bt.Save(newfile, ImageFormat.Gif);
                        
break;
                    
case "PNG":
                        bt.Save(newfile, ImageFormat.Png);
                        
break;
                    
default:
                        bt.Save(newfile, ImageFormat.Jpeg);
                        
break;
                }
                gr.Dispose();
                bt.Dispose();
                image.Dispose();
            }
            
catch { }

        }
    }
}
posted on 2009-09-10 22:00  悠閒  阅读(302)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3