• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一个具有上进心的码农
因为一篇文章中有很多是从很多篇文章中摘取的,请恕我没有一一说明摘取出处,如果没有说明,则该文章默认是摘取,如有侵犯您的权益,请与我联系,将会马上删除。
博客园    首页    新随笔    联系   管理    订阅  订阅

缩略图代码(按百分比和按大小两种方式)

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;public class ImageThumbnail
{
   
public Image ResourceImage;
   
private int ImageWidth;
   
private int ImageHeight;
   
public string ErrorMessage;

   
public ImageThumbnail(string ImageFileName)
    {
        ResourceImage
= Image.FromFile(ImageFileName);
        ErrorMessage
= "";
    }

   
public bool ThumbnailCallback()
    {
       
return false;
    }


   
// 方法1,按大小
    public bool ReducedImage(int Width, int Height, string targetFilePath)
    {
       
try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb
= new Image.GetThumbnailImageAbort(ThumbnailCallback);
            ReducedImage
= ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
           
return true;
        }
       
catch (Exception e)
        {
            ErrorMessage
= e.Message;
           
return false;
        }
    }


   
// 方法2,按百分比  缩小60% Percent为0.6 targetFilePath为目标路径
    public bool ReducedImage(double Percent, string targetFilePath)
    {
       
try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb
= new Image.GetThumbnailImageAbort(ThumbnailCallback);
            ImageWidth
= Convert.ToInt32(ResourceImage.Width * Percent);
            ImageHeight
= (ResourceImage.Height)*ImageWidth/ ResourceImage.Width;//等比例缩放
            ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
           
return true;
        }
       
catch (Exception e)
        {
            ErrorMessage
= e.Message;
           
return false;
        }
    }


}

后台代码:
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 _Default : System.Web.UI.Page
{

   
protected void Page_Load(object sender, EventArgs e)
    {

    }
   
protected void bt_upload_Click(object sender, EventArgs e)
    {
       
try
        {
           
if (FileUpload1.PostedFile.FileName == "")
            {
               
this.lb_info.Text = "请选择文件!";
            }
           
else
            {
               
string filepath = FileUpload1.PostedFile.FileName;
               
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
               
string serverpath1 = Server.MapPath("images/") + filename;
               
string serverpath2 = Server.MapPath("images/") + System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss") + Session.SessionID + filename;
                FileUpload1.PostedFile.SaveAs(serverpath1);
                ImageThumbnail img
= new ImageThumbnail(filepath);
                img.ReducedImage(
0.4, serverpath2);//0.4表示缩小40%
                this.lb_info.Text = "上传成功!";
            }
        }
       
catch (Exception error)
        {
           
this.lb_info.Text = "上传发生错误!原因:" + error.ToString();
        }
    }


}
posted @ 2008-07-03 11:21  不若相忘于江湖  阅读(245)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3