zoukankan      html  css  js  c++  java
  • Asp.net缩略图代码

    前台页面:WebForm1.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
    </head>
    <body>
         <form id="Form1" method="post" encType="multipart/form-data" runat="server">
                <FONT face="宋体">
                    <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 568px; POSITION: absolute; TOP: 24px; HEIGHT: 336px"
                        cellSpacing="1" cellPadding="1" width="568" border="1">
                        <TR>
                            <TD style="HEIGHT: 16px"><asp:label id="Label1" runat="server">要上传的图片</asp:label></TD>
                            <TD style="HEIGHT: 16px"><INPUT id="upImage" type="file" name="File1" runat="server"></TD>
                            <TD style="HEIGHT: 16px"><asp:button id="btnUp" runat="server" Text="上传并生成缩略图"></asp:button></TD>
                        </TR>
                        <TR>
                            <TD style="HEIGHT: 154px"><asp:label id="Label2" runat="server">原图片</asp:label></TD>
                            <TD style="HEIGHT: 154px" align="center" colSpan="2"><asp:image id="imageSource" runat="server"></asp:image></TD>
                        </TR>
                        <TR>
                            <TD><asp:label id="Label3" runat="server">缩略图</asp:label></TD>
                            <TD align="center" colSpan="2"><asp:image id="imageSmall" runat="server"></asp:image></TD>
                        </TR>
                    </TABLE>
                </FONT>
            </form>
    </body>
    </html>
    
    
    后台代码:WebForm1.aspx.cs
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    namespace WebApplication1
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
    
            //定义image类的对象
            System.Drawing.Image image, newimage;
            //图片路径
            protected string imagePath;
            //图片类型
            protected string imageType;
            //图片名称
            protected string imageName;
            //提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
            //如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
            System.Drawing.Image.GetThumbnailImageAbort callb = null;
    
            private void Page_Load(object sender, System.EventArgs e)
            {
                this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
                this.Load += new System.EventHandler(this.Page_Load);
                // 在此处放置用户代码以初始化页面
            }
    
      
    
            private void btnUp_Click(object sender, System.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 && "gif" != imageType)
                    {
                        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);
                            //生成缩略图
                            newimage = image.GetThumbnailImage(100, 100, callb, new System.IntPtr());
                            //把缩略图保存到指定的虚拟路径
                            newimage.Save(Server.MapPath("upFile") + "\\small" + imageName);
                            //释放image对象占用的资源
                            image.Dispose();
                            //释放newimage对象的资源
                            newimage.Dispose();
                            //显示缩略图
                            imageSmall.ImageUrl = "upFile/" + "small" + imageName;
    
                            Response.Write("上传成功!");
                        }
                        catch
                        {
                            Response.Write("上传成功!");
                        }
    
                    }
                }
    
            }
        }
    }

    作者:沐雪
    文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
    如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
    找一找教程网-随时随地学软件编程 http://www.zyiz.net/

  • 相关阅读:
    417 Pacific Atlantic Water Flow 太平洋大西洋水流
    416 Partition Equal Subset Sum 分割相同子集和
    415 Add Strings 字符串相加
    414 Third Maximum Number 第三大的数
    413 Arithmetic Slices 等差数列划分
    412 Fizz Buzz
    410 Split Array Largest Sum 分割数组的最大值
    409 Longest Palindrome 最长回文串
    day22 collection 模块 (顺便对比queue也学习了一下队列)
    day21 计算器作业
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628996.html
Copyright © 2011-2022 走看看