c#的asp.net上传图片加水印并生成缩略图 文件名 时间

ASP.NET 图片上传与处理
本文介绍了一个基于 ASP.NET 的简单图片上传示例,包括前端界面设计与后端图片处理逻辑,如格式验证、文件保存及添加水印等。
前台UpPicture.aspx代码:
None.gif<%@ Page language="c#" Codebehind="UpPicture.aspx.cs" AutoEventWireup="false" Inherits="UpPic.UpPicture" %>
None.gif
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
<HTML>
None.gif    
<HEAD>
None.gif        
<title>UpPicture</title>
None.gif        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
None.gif        
<meta name="CODE_LANGUAGE" Content="C#">
None.gif        
<meta name="vs_defaultClientScript" content="JavaScript">
None.gif        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
None.gif    
</HEAD>
None.gif    
<body MS_POSITIONING="GridLayout">
None.gif        
<form id="Form1" method="post" runat="server">
None.gif            
<INPUT id="File1" style="Z-INDEX: 101; LEFT: 24px; WIDTH: 440px; POSITION: absolute; TOP: 32px; HEIGHT: 22px"
None.gif                type
="file" size="54" name="File1" runat="server">
None.gif            
<asp:Button id="btnUpPic" style="Z-INDEX: 102; LEFT: 472px; POSITION: absolute; TOP: 32px" runat="server"
None.gif                Text
="上传图片"></asp:Button>
None.gif            
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 64px" runat="server"
None.gif                Width
="520px"></asp:Label>
None.gif            
<asp:Image id="showSmallPic" style="Z-INDEX: 104; LEFT: 32px; POSITION: absolute; TOP: 104px"
None.gif                runat
="server"></asp:Image>
None.gif        
</form>
None.gif    
</body>
None.gif
</HTML>

后台UpPicture.aspx.cs代码:
None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.Web.SessionState;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
using System.IO;
None.gif
namespace UpPic
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// UpPicture 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class UpPicture : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
protected System.Web.UI.WebControls.Image showSmallPic;
InBlock.gif        
protected System.Web.UI.WebControls.Button btnUpPic;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.btnUpPic.Click += new System.EventHandler(this.btnUpPic_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
//提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
InBlock.gif        
//如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
InBlock.gif
        System.Drawing.Image.GetThumbnailImageAbort callb = null;
InBlock.gif        
//定义image类的对象
InBlock.gif
        System.Drawing.Image image,smallImage,shuiyinImg,FlagImage;
InBlock.gif        
private void btnUpPic_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(this.File1.PostedFile.FileName!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//先取得上传的总路径
InBlock.gif
                string imagePath=this.File1.PostedFile.FileName;
InBlock.gif                
//取得图片类型
InBlock.gif
                string imageType=Path.GetExtension(imagePath).ToUpper();
InBlock.gif                
//取得图片名称
InBlock.gif
                string imageName=imagePath.Substring(imagePath.LastIndexOf("\\")+1);
InBlock.gif                imageName
=System.DateTime.Now.ToString("yyyyMMddhhmmss")+"_"+imageName;
InBlock.gif                
//判断上传图片类型
InBlock.gif
                if(imageType==".JPG"||imageType==".BMP"||imageType==".JPEG")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//保存原图
InBlock.gif
                    string ImgPath=Server.MapPath("upFile"+"\\"+imageName);
InBlock.gif                    
this.File1.PostedFile.SaveAs(ImgPath);
InBlock.gif                    
//为上传的图片建立引用
InBlock.gif
                    image=System.Drawing.Image.FromFile(ImgPath);            
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
/**//*************加水印开始*********************************************/
InBlock.gif                    shuiyinImg
=System.Drawing.Image.FromFile(Server.MapPath(".")+"/Flag200_100.png");
InBlock.gif                    Graphics g 
= Graphics.FromImage(image);
InBlock.gif                    g.DrawImage(shuiyinImg, 
new Rectangle(image.Width-shuiyinImg.Width, image.Height-shuiyinImg.Height,
InBlock.gif                        shuiyinImg.Width, shuiyinImg.Height), 
00, shuiyinImg.Width, shuiyinImg.Height, GraphicsUnit.Pixel);
InBlock.gif                    g.Dispose();
InBlock.gif                    
string path1=Server.MapPath("upFile"+"\\Flag_"+imageName);
InBlock.gif                    image.Save(path1);
InBlock.gif                    image.Dispose();
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
/**//************加水印的过程结束*********************************************/
InBlock.gif                    FlagImage
=System.Drawing.Image.FromFile(path1);
InBlock.gif                    
//生成缩略图
InBlock.gif
                    smallImage=FlagImage.GetThumbnailImage(130,100,callb,new System.IntPtr());
InBlock.gif                    
//保存加水印后的缩略图
InBlock.gif
                    smallImage.Save(Server.MapPath("upFile")+"\\small"+imageName);
InBlock.gif                    
//释放对象占用的资源
InBlock.gif
                    FlagImage.Dispose();
InBlock.gif                    smallImage.Dispose();
InBlock.gif                    
//删除原图,删除加水印后的原图
InBlock.gif
                    if(File.Exists(ImgPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        File.Delete(ImgPath);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if(File.Exists(path1))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        File.Delete(path1);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
//显示缩略图
InBlock.gif
                    showSmallPic.ImageUrl = "upFile/"+"small"+imageName;
InBlock.gif                    Response.Write(
"上传成功!");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Label1.Text="对不起!请您选择jpg或者gif格式的图片!";
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Label1.Text="请选择图片!";
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/xufengtian/archive/2007/03/21/682773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值