ASP.NET生成缩略图

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInfo
{
    public partial class MaLue : System.Web.UI.Page
    {
        System.Drawing.Image image, newimage; //定义image类的对象
        protected string imagePath;          //图片路径
        protected string imageType;          //图片类型
        protected string imageName;          //图片名称
        System.Drawing.Image.GetThumbnailImageAbort callb = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnUp_Click(object sender, EventArgs e)
        {
            string mPath;

            if ("" != upImage.PostedFile.FileName)
            {
                imagePath = upImage.PostedFile.FileName;
                //取得图片类型
                imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
                //取得图片名称
                imageName = imagePath.Substring(imagePath.LastIndexOf("/") + 1);
                //判断是否是JPG或者GIF图片,这里只是举个例子,并不一定必须是这两种图片
                if ("JPG" != imageType.ToUpper() && "GIF" != imageType.ToUpper())
                {
                    Response.Write("<script language='javascript'> alert('对不起!请您选择jpg或者gif格式的图片!');</script>");
                    return;
                }
                else
                {
                    try
                    {
                        //建立虚拟路径
                        mPath = Server.MapPath("upFile");
                        //保存到虚拟路径
                        upImage.PostedFile.SaveAs(mPath + "/" + imageName);
                        //显示原图
                        imageSource.ImageUrl = "upFile/" + imageName;
                        //为上传的图片建立引用
                        image = System.Drawing.Image.FromFile(mPath + "/" + imageName);

                        int smallW = 100;//小图片宽
                        int smallH = smallW * image.Height / image.Width;//小图片高
                        //生成缩略图
                        newimage = image.GetThumbnailImage(smallW, smallH, callb, new System.IntPtr());
                        //把缩略图保存到指定的虚拟路径
                        newimage.Save(Server.MapPath("upFile") + "/small" + imageName);
                        //释放image对象占用的资源
                        image.Dispose();
                        //释放newimage对象的资源
                        newimage.Dispose();
                        //显示缩略图
                        imageSmall.ImageUrl = "upFile/" + "small" + imageName;
                    }
                    catch(Exception ex)
                    {
                        Response.Write("上传失败!");
                    }
                }
            }
        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MaLue.aspx.cs" Inherits="UserInfo.MaLue" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        传图片:<input id="upImage" type="file" name="File1" runat="server"/>
        <asp:button id="btnUp" runat="server" Text="上传" OnClick="btnUp_Click"></asp:button><br />
        原图片:<asp:image id="imageSource" runat="server"></asp:image><br />
        缩略图:<asp:image id="imageSmall" runat="server"></asp:image>
        </div>
    </form>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值