asp.net 上传图片并同时生成缩略图的代码

代码如下:

<%@ Page Language="C#" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
 
void Page_Load(Object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ImgPreview.Visible = false;
}
}
void GetThumbnailImage(int width, int height, string strInfo, int left, int right)
{
string file = "Uploads/" + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\') + 1);
string newfile = "Uploads/" + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\') + 1) + ".jpg";
string strAdd = strInfo;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));
System.Drawing.Image thumbnailImage =
oldimage.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Response.Clear();
Bitmap output = new Bitmap(thumbnailImage);
Graphics g = Graphics.FromImage(output);
g.DrawString(strAdd, new Font("Courier New", 14), new SolidBrush(Color.Red), left, right);
output.Save(Server.MapPath(newfile), System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "image/gif";
ImgPreview.Visible = true;
ImgPreview.ImageUrl = newfile;
}
bool ThumbnailCallback()
{
return true;
}
void Button_Click(object sender, EventArgs e)
{
int width, height, left, right;
string strAddInfo = txtAddInfo.Text;
width = Int32.Parse(txtWidth.Text);
height = Int32.Parse(txtHeight.Text);
left = Int32.Parse(txtLeft.Text);
right = Int32.Parse(txtRight.Text);
if (!(uploadFile.PostedFile.ContentLength > 0))
{
lblErrInfo.Text = "没有选择文件";
}
else
{
string path = Server.MapPath("./Uploads/" + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\') + 1));
if (File.Exists(path))
{
lblErrInfo.Text = "已经有同名文件";
}
else
{
uploadFile.PostedFile.SaveAs(path);
GetThumbnailImage(width, height, strAddInfo, left, right);
}
}
}
</script>
<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">
<p>
<input id="uploadFile" type="file" runat="server" />
<asp:Label ID="lblErrInfo" runat="server" ForeColor="Red"></asp:Label>
</p>
<p>
width:<asp:TextBox ID="txtWidth" runat="server" Width="40px">100</asp:TextBox>
height:<asp:TextBox ID="txtHeight" runat="server" Width="40px">150</asp:TextBox>
</p>
<p>
添加信息:<asp:TextBox ID="txtAddInfo" runat="server"> AspxBoy.Com</asp:TextBox>
</p>
<p>
信息位置:left:<asp:TextBox ID="txtLeft" runat="server" Width="40px">10</asp:TextBox>
right:<asp:TextBox ID="txtRight" runat="server" Width="40px">135</asp:TextBox>
</p>
<p>
<input id="button" type="button" value="上传生成所略图" onserverclick="Button_Click" runat="server" />
</p>
<p>
<asp:Image ID="ImgPreview" runat="server"></asp:Image>
</p>
<!-- Insert content here -->
</form>
</body>
</html>

asp.net(vbscript)上传图片并同时生成缩略图:


代码如下:

  <script language="VB" runat="server">
  Sub UploadFile(sender As Object, e As EventArgs)
  If FileUp.PostedFile.ContentLength = 0 Then
  FileInfo.Visible = False
  Exit Sub
  Else
  FileInfo.Visible = True
  FDisplay1.Visible = True
  End If
  FSize.Text ="上传文件大小"+ CStr(FileUp.PostedFile.ContentLength/1024)+"KB"
  FName.Text = "已上传文件名:"+FileUp.PostedFile.FileName+"<br>"+FName.Text
  "写入数据库
  on error resume next
  dim myconn as sqlconnection
  dim mycomm as sqlcommand
  dim sql as string
  dim id as integer
  Dim image,anewimage As System.Drawing.Image
  dim width,height,newwidth,newheight as integer
  Dim callb As System.Drawing.Image.GetThumbnailImageAbort
  myConn=New sqlconnection(ConfigurationSettings.AppSettings("数据库"))
  myconn.open()
  sql="insert into picture (姓名,班级,介绍,属性) values (""&request.cookies("dgxyl").values("dgxylname")&"",""&request.cookies("dgxyl").values("dgxylbj")&"",""&trim(request("TextBox1"))&"",""&request("r1")&"")"
  Mycomm=New sqlcommand(sql,myconn)
  mycomm.executenonquery()
  myconn.close()
  Dim myCommand As New SqlCommand("select top 1 id from picture order by id desc", myConn)
  myCommand.Connection.Open()
  Dim myReader As SqlDataReader=mycommand.executereader()
  if myReader.Read() then
  id=myReader("id")
  end if
  myconn.close()
  "保存图片
  FileUp.PostedFile.SaveAs( Server.MapPath("\classpic\")&cstr(id)&".jpg" )
  "生成缩略图
  image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg"))
  width=image.Width
  height=image.height
  if width>height then
  newwidth=250
  newheight=image.height/image.Width*newwidth
  else
  newheight=250
  newwidth=image.Width/image.height*newheight
  end if
  response.write("id="+cstr(id)+"width="+cstr(Width)+";height="+cstr(height)+" ")
  response.write("newwidth="+cstr(newwidth)+";newheight="+cstr(newheight)+"<br>")
  aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())
  aNewImage.Save(Server.MapPath("/smallpic/"+cstr(id)+".jpg"))
  image.Dispose()
  Dim FileSplit() As String = Split( FileUp.PostedFile.FileName, "\" )
  Dim FileName As String = FileSplit(FileSplit.Length-1)
  Dim Exts() As String = Split( FileName, "." )
  Dim Ext As String = LCase(Exts(Exts.Length-1))
  FDisplay.Text = "<A Target="_blank" HREF="/classpic/"&cstr(id)&".jpg"& "">查看上传文件</A>"
  FDisplay1.text="<a href="/picture/default.asp?bj="&cstr(request.cookies("dgxyl").values("dgxylbj"))&"">返回</a>"
  End Sub
  </script>
  <SCRIPT language=JavaScript>
  <!--
  var requestsubmitted=false;
  function guestbook_Validator(theForm)
  {
  //检查是否从新提交
  if (requestsubmitted==true){
  alert("你已经提交了留言,请等待服务器应答!");
  return(false);
  }
  requestsubmitted=true;
  return (true);
  }
  //-->
  </SCRIPT>
<Html>
  <Body BgColor=White>
  <H3 align="center">请正确填写下面各项</h3>
  <Hr></H3>
  <Div id="FileInfo" Visible="False" runat="server">
  <Asp:Label id="FSize" runat="server"/><br>
  <Asp:Label id="FName" runat="server"/><br>
  <Asp:Label id="FDisplay" runat="server"/>
  <Asp:Label id="FDisplay1" runat="server"/>
  </Div>
  <Form Enctype="multipart/form-data" onsubmit="return guestbook_Validator(this)" runat="server">
  上传文件
  <Input Type="File" id="FileUp" runat="server" size="20"><br>
  图片属性:<input type="radio" value="<%=request.cookies("dgxyl").values("dgxylbj")%>" name="R1" checked>本班<input type="radio" value="全  校" name="R1">全校(本班则只在本班显示,全校则在全校显示)<P>
  图片说明:<br>
  <asp:TextBox id="TextBox1" runat="server" Width="233px" Height="141px">
  </asp:TextBox>
  <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="请输入图片说明">
  </asp:RequiredFieldValidator><br>
  <Asp:button id="Upload" OnClick="UploadFile" Text="上传图片" runat="server"/>
  </form>
  <Hr>
  <p>注意:</p>
  <ol>
  <li><b>严禁上传污染环境的照片,否则账号将被删除!</b></li>
  <li><font color="#808000"><b>请详细填写照片说明,图片说明不详细将会被视为乱传图片,将会被删除!</b></font></li>
  </ol>
  </Body>
  </Html>

(0)

相关推荐

  • ASP.Net 上传图片并生成高清晰缩略图

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT

  • Asp.net 文件上传类(取得文件后缀名,保存文件,加入文字水印)

    复制代码 代码如下: using System; using System.Data; using System.Configuration; 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; usi

  • ASP.NET实现上传图片并生成缩略图的方法

    本文实例讲述了ASP.NET实现上传图片并生成缩略图的方法.分享给大家供大家参考,具体如下: protected void bt_upload_Click(object sender, EventArgs e) { //检查上传文件的格式是否有效 if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { Response.Write("上传图片格式无效!"); re

  • asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧

    public class MyGDI {     public static void CreateWatermark(string sSrcFilePath, string sDstFilePath, string sText1, string sColor1, string sSize1, string sFont1, string sText2, string sColor2, string sSize2, string sFont2, string sBgColor, string sT

  • asp.net 添加水印的代码(已测试)

    加水印的功能代码如下所示 复制代码 代码如下: /// <summary> /// 图片修改类,主要是用来保护图片版权的,版权归原作者所有 /// </summary> public class picmark { #region "member fields" private string modifyImagePath = null; private string drawedImagePath = null; private int rightSpace;

  • asp.net如何在图片上加水印文字具体实现

    第一步,添加一个一般处理程序(Handler),本例是ImageHandler 复制代码 代码如下: using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;usin

  • asp.net实现生成缩略图及给原始图加水印的方法示例

    本文实例讲述了asp.net实现生成缩略图及给原始图加水印的方法.分享给大家供大家参考,具体如下: using System.IO; using System.Drawing.Imaging; private void Button1_ServerClick(object sender, System.EventArgs e) { Graphics g=null; System.Drawing.Image upimage=null; System.Drawing.Image thumimg=nu

  • asp.net文件上传功能(单文件,多文件,自定义生成缩略图,水印)

    前言 上传功能,是大家经常用到了,可能每一个项目都可以会用到.网上到处都有上传功能的代码.比我写的好的有很多.我这里也仅是分享我的代码. 功能实现点 1.单个文件上传: 2.多个文件上传: 3.对于图片等类型的图像,可以自定义生成缩略图大小: 4.文件服务器扩展. 模式 主要使用的是"模板方法"的设计模式. 本文章的功能优缺点 1.可以自定义生成缩略图的大小,任意定义.对于像微生活运动户外商城(http://sports.8t8x.com/) .淘宝网等的网站,他们需要上传大量的商品图

  • ASP.NET简单好用功能齐全图片上传工具类(水印、缩略图、裁剪等)

    使用方法: UploadImage ui = new UploadImage(); /***可选参数***/ ui.SetWordWater = "哈哈";//文字水印 // ui.SetPicWater = Server.MapPath("2.png");//图片水印(图片和文字都赋值图片有效) ui.SetPositionWater = 4;//水印图片的位置 0居中.1左上角.2右上角.3左下角.4右下角 ui.SetSmallImgHeight = &quo

  • asp.net图片上传生成缩略图的注意事项

    bitmap.Save(imgPath,ImageFormat.Jpeg);   //这是保存缩略图的一段代码,其中的ImageFormat.Jpeg一定不能省略,即使你保存的文件本来就是jpg格式的,也不能去掉.因为如果去掉的话,生成的缩略图比原始图片还要大! //另外,imgPath必须首先创建,否则会产生GDI+的一般性错误. path=System.Web.HttpContext.Current.Server.MapPath(path); 使用if(!System.IO.Director

  • asp.net上传图片并作处理水印与缩略图的实例代码

    方法类: 复制代码 代码如下: upFileClass.cs using System;using System.Data;using System.Configuration;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.HtmlC

随机推荐