asp.net 上傳圖片并同時(shí)生成縮略圖的代碼
更新時(shí)間:2010年02月26日 15:36:38 作者:
asp.net 上傳圖片并同時(shí)生成縮略圖的代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
<%@ 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 = "已經(jīng)有同名文件";
}
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)上傳圖片并同時(shí)生成縮略圖:
復(fù)制代碼 代碼如下:
<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
"寫入數(shù)據(jù)庫
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("數(shù)據(jù)庫"))
myconn.open()
sql="insert into picture (姓名,班級(jí),介紹,屬性) 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("你已經(jīng)提交了留言,請(qǐng)等待服務(wù)器應(yīng)答!");
return(false);
}
requestsubmitted=true;
return (true);
}
//-->
</SCRIPT>
<Html>
<Body BgColor=White>
<H3 align="center">請(qǐng)正確填寫下面各項(xiàng)</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="請(qǐng)輸入圖片說明">
</asp:RequiredFieldValidator><br>
<Asp:button id="Upload" OnClick="UploadFile" Text="上傳圖片" runat="server"/>
</form>
<Hr>
<p>注意:</p>
<ol>
<li><b>嚴(yán)禁上傳污染環(huán)境的照片,否則賬號(hào)將被刪除!</b></li>
<li><font color="#808000"><b>請(qǐng)?jiān)敿?xì)填寫照片說明,圖片說明不詳細(xì)將會(huì)被視為亂傳圖片,將會(huì)被刪除!</b></font></li>
</ol>
</Body>
</Html>
您可能感興趣的文章:
- ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
- asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- ASP.Net 上傳圖片并生成高清晰縮略圖
- asp.net圖片上傳生成縮略圖的注意事項(xiàng)
- ASP.NET實(shí)現(xiàn)上傳圖片并生成縮略圖的方法
- Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
- asp.net 添加水印的代碼(已測(cè)試)
- asp.net下GDI+的一些常用應(yīng)用(水印,文字,圓角處理)技巧
- asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)
- asp.net實(shí)現(xiàn)生成縮略圖及給原始圖加水印的方法示例
相關(guān)文章
動(dòng)態(tài)加載用戶控件至DataList并為用戶控件賦值實(shí)例演示
本文借用使用通用的新聞例子演示動(dòng)態(tài)加載用戶控件至DataList并為用戶控件賦值,感興趣的朋友可以了解下2013-01-01為自己的ASP網(wǎng)站系統(tǒng)構(gòu)建一套標(biāo)記語言
為自己的ASP網(wǎng)站系統(tǒng)構(gòu)建一套標(biāo)記語言...2006-09-09詳解Asp.net web.config customErrors 如何設(shè)置
這篇文章主要介紹了詳解Asp.net web.config customErrors 如何設(shè)置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02ASP.NET 運(yùn)行時(shí)錯(cuò)誤: 沒有為擴(kuò)展名“.asax”注冊(cè)的生成提供程序修正版
ASP.NET 運(yùn)行時(shí)錯(cuò)誤: 沒有為擴(kuò)展名“.asax”注冊(cè)的生成提供程序。可以在 machine.config 或 web.config 中的 <compilation><buildProviders> 節(jié)注冊(cè)一個(gè)。2009-01-01asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證
這篇文章主要介紹了asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05