一聚教程网:一个值得你收藏的教程网站

热门教程

c#web控件FileUpload图片上传(并生成小图)

时间:2022-06-25 05:05:03 编辑:袖梨 来源:一聚教程网

本教程是利用asp教程.net c#让web控件fileupload选择完文件之后就自动触发事件,并且image控件显示出图片来

<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>




c#web控件fileupload<a href=/tags.php/php文件上传/ target=_blank >图片上传</a>(并生成小图)

  
  

  

  

  

  
public string pic="";


<%
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;
using system.io;
public partial class upfile_upfile : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{ }
protected void button1_click(object sender, eventargs e)
{
if (fileupload1.hasfile)
{
string filecontenttype = fileupload1.postedfile.contenttype;
if (filecontenttype == "image/bmp" || filecontenttype == "image/gif" || filecontenttype == "image/pjpeg")
{
string name = fileupload1.postedfile.filename;                  // 客户端文件路径

fileinfo file = new fileinfo(name);
string filename = file.name;                                    // 文件名称
                string filename_s = "s_" + file.name;                           // 缩略图文件名称
                string filename_sy = "sy_" + file.name;                         // 水印图文件名称(文字)
                string filename_syp = "syp_" + file.name;                       // 水印图文件名称(图片)
                string webfilepath = server.mappath("file/" + filename);        // 服务器端文件路径
                string webfilepath_s = server.mappath("file/" + filename_s);  // 服务器端缩略图路径
                string webfilepath_sy = server.mappath("file/" + filename_sy); // 服务器端带水印图路径(文字)
                string webfilepath_syp = server.mappath("file/" + filename_syp); // 服务器端带水印图路径(图片)
                string webfilepath_sypf = server.mappath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)

if (!file.exists(webfilepath))
{
try
{
fileupload1.saveas(webfilepath);                                // 使用 saveas 方法保存文件
                        addshuiyinword(webfilepath, webfilepath_sy);
addshuiyinpic(webfilepath, webfilepath_syp, webfilepath_sypf);
makethumbnail(webfilepath, webfilepath_s, 130, 130, "cut");     // 生成缩略图方法
                        label1.text = "提示:文件"" + filename + ""成功上传,并生成"" + filename_s + ""缩略图,文件类型为:" + fileupload1.postedfile.contenttype + ",文件大小为:" + fileupload1.postedfile.contentlength + "b";
}
catch (exception ex)
{
label1.text = "提示:文件上传失败,失败原因:" + ex.message;
}
}
else
{
label1.text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
label1.text = "提示:文件类型不符";
}
}
}
///


/// 生成缩略图
///

/// 源图路径(物理路径)
/// 缩略图路径(物理路径)
/// 缩略图宽度
/// 缩略图高度
/// 生成缩略图的方式   
    public static void makethumbnail(string originalimagepath, string thumbnailpath, int width, int height, string mode)
{
system.drawing.image originalimage = system.drawing.image.fromfile(originalimagepath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalimage.width;
int oh = originalimage.height;
switch (mode)
{
case "hw"://指定高宽缩放(可能变形)               
                break;
case "w"://指定宽,高按比例                   
                toheight = originalimage.height * width / originalimage.width;
break;
case "h"://指定高,宽按比例
                towidth = originalimage.width * height / originalimage.height;
break;
case "cut"://指定高宽裁减(不变形)               
                if ((double)originalimage.width / (double)originalimage.height > (double)towidth / (double)toheight)
{
oh = originalimage.height;
ow = originalimage.height * towidth / toheight;
y = 0;
x = (originalimage.width - ow) / 2;
}
else
{
ow = originalimage.width;
oh = originalimage.width * height / towidth;
x = 0;
y = (originalimage.height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
        system.drawing.image bitmap = new system.drawing.bitmap(towidth, toheight);
//新建一个画板
        system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap);
//设置高质量插值法
        g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
//设置高质量,低速度呈现平滑程度
        g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//清空画布并以透明背景色填充
        g.clear(system.drawing.color.transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
        g.drawimage(originalimage, new system.drawing.rectangle(0, 0, towidth, toheight),
new system.drawing.rectangle(x, y, ow, oh),
system.drawing.graphicsunit.pixel);
try
{
//以jpg格式保存缩略图
            bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.jpeg);
}
catch (system.exception e)
{
throw e;
}
finally
{
originalimage.dispose();
bitmap.dispose();
g.dispose();
}
}
///
/// 在图片上增加文字水印
///

/// 原服务器图片路径
/// 生成的带文字水印的图片路径
    protected void addshuiyinword(string path, string path_sy)
{
string addtext = "测试水印";
system.drawing.image image = system.drawing.image.fromfile(path);
system.drawing.graphics g = system.drawing.graphics.fromimage(image);
g.drawimage(image, 0, 0, image.width, image.height);
system.drawing.font f = new system.drawing.font("verdana", 16);
system.drawing.brush b = new system.drawing.solidbrush(system.drawing.color.blue);
g.drawstring(addtext, f, b, 15, 15);
g.dispose();
image.save(path_sy);
image.dispose();
}
/**////
/// 在图片上生成图片水印
///

/// 原服务器图片路径
/// 生成的带图片水印的图片路径
/// 水印图片路径
    protected void addshuiyinpic(string path, string path_syp, string path_sypf)
{
system.drawing.image image = system.drawing.image.fromfile(path);
system.drawing.image copyimage = system.drawing.image.fromfile(path_sypf);
system.drawing.graphics g = system.drawing.graphics.fromimage(image);
g.drawimage(copyimage, new system.drawing.rectangle(image.width - copyimage.width, image.height - copyimage.height, copyimage.width, copyimage.height), 0, 0, copyimage.width,copyimage.height, system.drawing.graphicsunit.pixel);
g.dispose();
image.save(path_syp);
image.dispose();
}

>

下载地址

http://down.111com.net/down/code/net/2010/0817/20267.html

热门栏目