asp.net圖片上傳實例
第一、圖片上傳,代碼如下:
xxx.aspx
<td class="style1">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上傳一般圖片" onclick="Button1_Click" />
</td>
<td class="style3">
<asp:Image ID="Image1" runat="server" Height="200px" Width="200px" />
</td>
xxx.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
string FileName = System.IO.Path.GetFileName(file.FileName);
string[] SplitFileName = FileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss")+"." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image1.ImageUrl = "upload/" + AtterFileName;
}
}
else
{
Response.Write("<script>alert('該文件不是圖片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('請選擇要上傳的圖片');</script>");
}
}
}
第二、添加文字水印的圖片上傳,代碼如下:
xxx.aspx
<td class="style1">
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:Button ID="Button2" runat="server" Text="上傳文字圖片" onclick="Button2_Click" />
</td>
<td>
<asp:Image ID="Image2" runat="server" Height="200px" Width="200px" />
</td>
xxx.aspx.cs
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawString("我的圖片", new Font("宋體", 14), Brushes.Red, 0, 0);
}
string FileName = System.IO.Path.GetFileName(file.FileName);
string[] SplitFileName = FileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image2.ImageUrl = "upload/" + AtterFileName;
}
}
else
{
Response.Write("<script>alert('該文件不是圖片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('請選擇要上傳的圖片');</script>");
}
}
}
第三、添加圖片水印的圖片上傳,代碼如下:
xxx.aspx
<td class="style1">
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:Button ID="Button3" runat="server" Text="上傳水印圖片" onclick="Button3_Click" />
</td>
<td>
<asp:Image ID="Image3" runat="server" Height="200px" Width="200px" />
</td>
xxx.aspx.cs
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
string fileName = file.FileName;
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (System.Drawing.Image imgWater = System.Drawing.Image.FromFile(Server.MapPath("/img/czlogo.jpg")))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawImage(imgWater, 0, 0);
}
string[] SplitFileName = fileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image3.ImageUrl = "upload/" + AtterFileName;
}
}
}
else
{
Response.Write("<script>alert('該文件不是圖片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('請選擇要上傳的圖片');</script>");
}
}
}
第四、上傳圖片濃縮圖,代碼如下:
xxx.aspx
<td class="style1">
<asp:FileUpload ID="FileUpload4" runat="server" />
<asp:Button ID="Button4" runat="server" Text="上傳濃縮圖片" onclick="Button4_Click" />
</td>
<td>
<asp:Image ID="Image4" runat="server" Height="200px" Width="200px" />
</td>
xxx.aspx.cs
protected void Button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (System.Drawing.Image imgThumb = new Bitmap(200, 100))
{
using (Graphics g = Graphics.FromImage(imgThumb))
{
g.DrawImage(img, new Rectangle(0, 0, imgThumb.Width, imgThumb.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
}
string fileName = file.FileName;
string[] SplitFileName = fileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image4.ImageUrl = "upload/" + AtterFileName;
}
}
}
else
{
Response.Write("<script>alert('該文件不是圖片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('請選擇要上傳的圖片');</script>");
}
}
}
- JQuery.uploadify 上傳文件插件的使用詳解 for ASP.NET
- asp.net+FCKeditor上傳圖片顯示叉叉圖片無法顯示的問題的解決方法
- asp.net fileupload控件上傳文件與多文件上傳
- asp.net(c#)開發(fā)中的文件上傳組件uploadify的使用方法(帶進度條)
- asp.net MVC實現(xiàn)無組件上傳圖片實例介紹
- asp.net 多文件上傳,兼容IE6/7/8,提供完整代碼下載
- Asp.net實現(xiàn)MVC處理文件的上傳下載功能實例教程
- Asp.Net 無刷新文件上傳并顯示進度條的實現(xiàn)方法及思路
- asp.net下文件上傳和文件刪除的代碼
- ASP.NET MVC Webuploader實現(xiàn)上傳功能
相關(guān)文章
C# javaScript函數(shù)的相互調(diào)用
如何在JavaScript訪問C#函數(shù),如何在C#中訪問JavaScript的已有變量等實現(xiàn)方法2008-12-12ASP與ASP.NET互通COOKIES的一點經(jīng)驗
ASP與ASP.NET互通COOKIES的一點經(jīng)驗...2006-09-09asp.net JavaScript插件 JavaScript Function Outliner
一個JavaScript Function Outliner插件 第四版本 支持內(nèi)嵌javascript,且可以對javascript進行壓縮2008-07-07Javascript 直接調(diào)用服務(wù)器C#代碼 ASP.NET Ajax實例
近來總有一些朋友會問到一些入門的問題,把這些問題整理一下,寫出來。在以前的文章里,曾經(jīng)利用純JS編寫過Ajax引擎,在真正開發(fā)的時候,大家都不喜歡以這種低效率的方式開發(fā),利用MS Ajax的集成的引擎,可以簡單不少工作。2010-03-03ASP.NET Core Zero使用Power Tool工具
這篇文章介紹了ASP.NET Core Zero使用Power Tool工具的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-02-02ASP.NET配合jQuery解決跨域調(diào)用的問題
這篇文章主要介紹了ASP.NET配合jQuery解決跨域調(diào)用的問題,簡單實用,需要的朋友可以參考下。2016-06-06