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

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

<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>c#web控件fileupload图片上传(并生成小图)</title>
</head>

<body>

<script language="网页特效" type="text/javascript">  
  function previewimg(imgfile)  
  {  
  var newpreview = document.getelementbyid("newpreview");  
  newpreview.filters.item("dximagetransform.microsoft.alphaimageloader").src = imgfile.value;  
  newpreview.style.width = "80px";  
  newpreview.style.height = "60px";  
  }  
  </script>  
<asp:fileupload id="fileupload1" runat="server" onchange="previewimg(this)" />  
<div id="newpreview"> </div>  

<script>
function $(o){return document.getelementbyid(o);}  
function checkimg(o,img)  
{  
  if (!/.((jpg)|(bmp)|(gif)|(png))$/ig.test(o.value))  
  {  
  alert('只能上传jpg,bmp,gif,png格式图片!');  
  o.outerhtml = o.outerhtml;  
  }  
  else  
  {  
  $(img).filters.item("dximagetransform.microsoft.alphaimageloader").src=o.value;  
  }  
}
</script>  

<asp:fileupload id="fileupload1" runat="server" onchange="checkimg(this, 'img');" />  
<div id="img" style="filter:progid:dximagetransform.microsoft.alphaimageloader(src= <%= pic%>,sizingmethod=scale);width:88px;height:113px;"> </div>  
public string pic="";
</body>
</html>
<%
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 = "提示:文件类型不符";
}
}
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalimagepath">源图路径(物理路径)</param>
/// <param name="thumbnailpath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>   
    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();
}
}
/// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="path">原服务器图片路径</param>
/// <param name="path_sy">生成的带文字水印的图片路径</param>
    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();
}
/**//// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="path">原服务器图片路径</param>
/// <param name="path_syp">生成的带图片水印的图片路径</param>
/// <param name="path_sypf">水印图片路径</param>
    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.111cn.net/down/code/net/2010/0817/20267.html

时间: 2024-09-17 14:56:09

c#web控件FileUpload图片上传(并生成小图)的相关文章

有谁会改FreeTextBox 3.1.6,给控件加插入上传Flash/视频功能?

问题描述 有谁会改FreeTextBox3.1.6,给控件加插入上传Flash/视频功能?小弟给你们跪下了,有的话给我个.....很急哦 解决方案 解决方案二:lollollol解决方案三:接分先!解决方案四:有问题请先GOOGLE,BAIDU

PHP图片上传并生成缩略图函数

自己写的 PHP图片上传并生成缩略图函数 请参考 函数分为三部分 一.PHP 图片上传函数 二.PHP 生成缩略图函数 三.PHP消息提示函数 //PHP 图片上传函数如下: function img_upload($form_name,$size,$dir,$file_name,$width,$height){  //文件域名称,文件大小限制,文件存放路径,缩略图宽度,缩略图高度  $file_type_arr=array("image/png"=>"png"

使用FileUpload控件上传图片,上传不到文件夹,数据库也没有写入,图片无法显示。

问题描述 使用FileUpload控件上传图片,图片上传不到文件夹,数据库也没有写入图片的表,图片无法显示.但其他都正常,都能显示前台页面代码:<bodystyle="background-image:url(../images/right.png);background-repeat:no-repeat;"><formid="form1"runat="server"><div><br/><b

c#多图片上传并生成缩略图的实例代码_实用技巧

前台代码: 复制代码 代码如下:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>  <!DOCTYPE html>  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat

php图片上传并生成缩略图效果

 代码如下 复制代码 function uploadimage($upname,$smallmark=1,$dstsw,$dstsh=0,$path_dim,$path_xim,$newname,$smallname=0,$filetype="null") {     global $webaddr,$_files,$my;     $phpv=str_replace('.', '', php_version);     $filename=$upname;     $max_file

利用ASPUPLOAD,ASPJPEG实现图片上传自动生成缩略图及加上水印_应用技巧

今天在站长站看到一网友写的相册程序,功能挺简单的,看到他用了ASPJPEG生成缩略图,不由想起再用上ASPUPLOAD上传,于是花了一个小时时间完善了他的代码. 以下代码均加有简单的注释,如果你看不懂,请先看ASPJPEG以及ASPUPLOAD的说明文档(E文,希望有心理准备),看不懂的可以问我. 以下是代码:  复制代码 代码如下: <%  if session("admin")<>"on" then  Response.Redirect&quo

php 图片上传并生成缩略图代码

 if($_FILES['image']['size']){      if($_FILES['image']['type'] == "image/pjpeg"){       $im = @imagecreatefromjpeg($_FILES['image']['tmp_name']);       $n_bmp.='.jpg';      }elseif($_FILES['image']['type'] == "image/x-png"){       $im

php图片上传并生成水印

<?php   header('Content-Type:text/html;charset=gb2312'); $animation = new Imagick();                $animation->setFormat( "gif" );  $image = new Imagick('/skin/'.$_GET["ys"].'.gif'); $unitl = $image->getNumberImages();      f

jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码_php技巧

本例用到其他2个php class.upload.php和 functions.php还有css和js以及img文件 完整实例代码点击此处本站下载. 效果图如下: 实现代码如下: JavaScript代码如下: 复制代码 代码如下: <script type="text/javascript">  $(document).ready(function() {      $("#filelist").niceScroll({          cursor