使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图(转载)...

出自: http://www.cnblogs.com/weekzero/archive/2006/05/05/392003.html
写的很不错,收藏了~
本文借助vs2005中自带的FileUpload控件实现图片文件的上传并生成缩略图。
    实现过程:选择图片上传成功后,取得已经存在服务器的文件生成缩略图,并且判断是否是图片类型的文件,这个的判断可以在程序中修改,本程序只是判断了“image/bmp”、“image/gif”、“image/pjpeg”三种类型。
    代码如下:
ContractedBlock.gif ExpandedBlockStart.gif upfile.aspx
None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upfile.aspx.cs" Inherits="upfile" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
<html xmlns="http://www.w3.org/1999/xhtml">
None.gif
<head runat="server">
None.gif    
<title>无标题页</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif        
<div>
None.gif            
<asp:FileUpload ID="FileUpload1" runat="server" />
None.gif            
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
None.gif            
<asp:Label ID="Label1" runat="server"></asp:Label>
None.gif        
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>

ContractedBlock.gif ExpandedBlockStart.gif upfile.aspx.cs
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.IO;
None.gif
None.gif
public partial class upfile : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (FileUpload1.HasFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string fileContentType = FileUpload1.PostedFile.ContentType;
InBlock.gif            
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string name = FileUpload1.PostedFile.FileName;                  // 客户端文件路径
InBlock.gif

InBlock.gif                FileInfo file 
= new FileInfo(name);
InBlock.gif                
string fileName = file.Name;                                    // 文件名称
InBlock.gif
                string fileName_s = "s_" + file.Name;                           // 缩略图文件名称
InBlock.gif
                string fileName_sy = "sy_" + file.Name;                         // 水印图文件名称(文字)
InBlock.gif
                string fileName_syp = "syp_" + file.Name;                       // 水印图文件名称(图片)
InBlock.gif
                string webFilePath = Server.MapPath("file/" + fileName);        // 服务器端文件路径
InBlock.gif
                string webFilePath_s = Server.MapPath("file/" + fileName_s);  // 服务器端缩略图路径
InBlock.gif
                string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
InBlock.gif
                string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
InBlock.gif
                string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)
InBlock.gif

InBlock.gif                
if (!File.Exists(webFilePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        FileUpload1.SaveAs(webFilePath);                                
// 使用 SaveAs 方法保存文件
InBlock.gif
                        AddShuiYinWord(webFilePath, webFilePath_sy);
InBlock.gif                        AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
InBlock.gif                        MakeThumbnail(webFilePath, webFilePath_s, 
130130"Cut");     // 生成缩略图方法
InBlock.gif
                        Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Label1.Text 
= "提示:文件上传失败,失败原因:" + ex.Message;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Label1.Text 
= "提示:文件已经存在,请重命名后上传";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Label1.Text 
= "提示:文件类型不符";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 生成缩略图
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="originalImagePath">源图路径(物理路径)</param>
InBlock.gif    
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
InBlock.gif    
/// <param name="width">缩略图宽度</param>
InBlock.gif    
/// <param name="height">缩略图高度</param>
ExpandedSubBlockEnd.gif    
/// <param name="mode">生成缩略图的方式</param>    

InBlock.gif    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Drawing.Image originalImage 
= System.Drawing.Image.FromFile(originalImagePath);
InBlock.gif
InBlock.gif        
int towidth = width;
InBlock.gif        
int toheight = height;
InBlock.gif
InBlock.gif        
int x = 0;
InBlock.gif        
int y = 0;
InBlock.gif        
int ow = originalImage.Width;
InBlock.gif        
int oh = originalImage.Height;
InBlock.gif
InBlock.gif        
switch (mode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
case "HW"://指定高宽缩放(可能变形)                
InBlock.gif
                break;
InBlock.gif            
case "W"://指定宽,高按比例                    
InBlock.gif
                toheight = originalImage.Height * width / originalImage.Width;
InBlock.gif                
break;
InBlock.gif            
case "H"://指定高,宽按比例
InBlock.gif
                towidth = originalImage.Width * height / originalImage.Height;
InBlock.gif                
break;
InBlock.gif            
case "Cut"://指定高宽裁减(不变形)                
InBlock.gif
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    oh 
= originalImage.Height;
InBlock.gif                    ow 
= originalImage.Height * towidth / toheight;
InBlock.gif                    y 
= 0;
InBlock.gif                    x 
= (originalImage.Width - ow) / 2;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ow 
= originalImage.Width;
InBlock.gif                    oh 
= originalImage.Width * height / towidth;
InBlock.gif                    x 
= 0;
InBlock.gif                    y 
= (originalImage.Height - oh) / 2;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
break;
InBlock.gif            
default:
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//新建一个bmp图片
InBlock.gif
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
InBlock.gif
InBlock.gif        
//新建一个画板
InBlock.gif
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
InBlock.gif
InBlock.gif        
//设置高质量插值法
InBlock.gif
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
InBlock.gif
InBlock.gif        
//设置高质量,低速度呈现平滑程度
InBlock.gif
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
InBlock.gif
InBlock.gif        
//清空画布并以透明背景色填充
InBlock.gif
        g.Clear(System.Drawing.Color.Transparent);
InBlock.gif
InBlock.gif        
//在指定位置并且按指定大小绘制原图片的指定部分
InBlock.gif
        g.DrawImage(originalImage, new System.Drawing.Rectangle(00, towidth, toheight),
InBlock.gif            
new System.Drawing.Rectangle(x, y, ow, oh),
InBlock.gif            System.Drawing.GraphicsUnit.Pixel);
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//以jpg格式保存缩略图
InBlock.gif
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (System.Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw e;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            originalImage.Dispose();
InBlock.gif            bitmap.Dispose();
InBlock.gif            g.Dispose();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 在图片上增加文字水印
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="Path">原服务器图片路径</param>
ExpandedSubBlockEnd.gif    
/// <param name="Path_sy">生成的带文字水印的图片路径</param>

InBlock.gif    protected void AddShuiYinWord(string Path, string Path_sy)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string addText = "测试水印";
InBlock.gif        System.Drawing.Image image 
= System.Drawing.Image.FromFile(Path);
InBlock.gif        System.Drawing.Graphics g 
= System.Drawing.Graphics.FromImage(image);
InBlock.gif        g.DrawImage(image, 
00, image.Width, image.Height);
InBlock.gif        System.Drawing.Font f 
= new System.Drawing.Font("Verdana"16);
InBlock.gif        System.Drawing.Brush b 
= new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
InBlock.gif
InBlock.gif        g.DrawString(addText, f, b, 
1515);
InBlock.gif        g.Dispose();
InBlock.gif
InBlock.gif        image.Save(Path_sy);
InBlock.gif        image.Dispose();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 在图片上生成图片水印
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="Path">原服务器图片路径</param>
InBlock.gif    
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
ExpandedSubBlockEnd.gif    
/// <param name="Path_sypf">水印图片路径</param>

InBlock.gif    protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Drawing.Image image 
= System.Drawing.Image.FromFile(Path);
InBlock.gif        System.Drawing.Image copyImage 
= System.Drawing.Image.FromFile(Path_sypf);
InBlock.gif        System.Drawing.Graphics g 
= System.Drawing.Graphics.FromImage(image);
InBlock.gif        g.DrawImage(copyImage, 
new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 00, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
InBlock.gif        g.Dispose();
InBlock.gif
InBlock.gif        image.Save(Path_syp);
InBlock.gif        image.Dispose();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

Demo下载: http://files.cnblogs.com/hide0511/watermark.rar

转载于:https://www.cnblogs.com/hide0511/archive/2006/09/25/513938.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值