ASP.Net 上傳圖片并生成高清晰縮略圖
更新時間:2009年02月16日 19:14:30 作者:
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/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" runat="server" type="file" /></div><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
string a = this.UpLoadImage(this.File1, "UpLoad/", "thumb_", 118, 118);
}
/// <summary>
/// asp.net上傳圖片并生成縮略圖
/// </summary>
/// <param name="upImage">HtmlInputFile控件</param>
/// <param name="sSavePath">保存的路徑,些為相對服務器路徑的下的文件夾</param>
/// <param name="sThumbExtension">縮略圖的thumb</param>
/// <param name="intThumbWidth">生成縮略圖的寬度</param>
/// <param name="intThumbHeight">生成縮略圖的高度</param>
/// <returns>縮略圖名稱</returns>
public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
{
string sThumbFile = "";
string sFilename = "";
if (upImage.PostedFile != null)
{
HttpPostedFile myFile = upImage.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
return "沒有選擇上傳圖片";
//獲取upImage選擇文件的擴展名
string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
//判斷是否為圖片格式
if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
return "圖片格式不正確";
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
//檢查當前文件夾下是否有同名圖片,有則在文件名+1
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + extendName;
}
System.IO.FileStream newFile
= new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create, System.IO.FileAccess.Write);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
//以上為上傳原圖
try
{
//原圖加載
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
//原圖寬度和高度
int width = sourceImage.Width;
int height = sourceImage.Height;
int smallWidth;
int smallHeight;
//獲取第一張繪制圖的大小,(比較 原圖的寬/縮略圖的寬 和 原圖的高/縮略圖的高)
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
smallWidth = intThumbWidth;
smallHeight = intThumbWidth * height / width;
}
else
{
smallWidth = intThumbHeight * width / height;
smallHeight = intThumbHeight;
}
//判斷縮略圖在當前文件夾下是否同名稱文件存在
file_append = 0;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + extendName;
}
//縮略圖保存的絕對路徑
string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
//新建一個圖板,以最小等比例壓縮大小繪制原圖
using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//繪制中間圖
using (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(Color.Black);
g.DrawImage(
sourceImage,
new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.GraphicsUnit.Pixel
);
}
//新建一個圖板,以縮略圖大小繪制中間圖
using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
{
//繪制縮略圖
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
int lwidth = (smallWidth - intThumbWidth) / 2;
int bheight = (smallHeight - intThumbHeight) / 2;
g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
g.Dispose();
bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
catch
{
//出錯則刪除
System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
return "圖片格式不正確";
}
//返回縮略圖名稱
return sThumbFile;
}
return "沒有選擇圖片";
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" runat="server" type="file" /></div><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
string a = this.UpLoadImage(this.File1, "UpLoad/", "thumb_", 118, 118);
}
/// <summary>
/// asp.net上傳圖片并生成縮略圖
/// </summary>
/// <param name="upImage">HtmlInputFile控件</param>
/// <param name="sSavePath">保存的路徑,些為相對服務器路徑的下的文件夾</param>
/// <param name="sThumbExtension">縮略圖的thumb</param>
/// <param name="intThumbWidth">生成縮略圖的寬度</param>
/// <param name="intThumbHeight">生成縮略圖的高度</param>
/// <returns>縮略圖名稱</returns>
public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
{
string sThumbFile = "";
string sFilename = "";
if (upImage.PostedFile != null)
{
HttpPostedFile myFile = upImage.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
return "沒有選擇上傳圖片";
//獲取upImage選擇文件的擴展名
string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
//判斷是否為圖片格式
if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
return "圖片格式不正確";
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
//檢查當前文件夾下是否有同名圖片,有則在文件名+1
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + extendName;
}
System.IO.FileStream newFile
= new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create, System.IO.FileAccess.Write);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
//以上為上傳原圖
try
{
//原圖加載
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
//原圖寬度和高度
int width = sourceImage.Width;
int height = sourceImage.Height;
int smallWidth;
int smallHeight;
//獲取第一張繪制圖的大小,(比較 原圖的寬/縮略圖的寬 和 原圖的高/縮略圖的高)
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
smallWidth = intThumbWidth;
smallHeight = intThumbWidth * height / width;
}
else
{
smallWidth = intThumbHeight * width / height;
smallHeight = intThumbHeight;
}
//判斷縮略圖在當前文件夾下是否同名稱文件存在
file_append = 0;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + extendName;
}
//縮略圖保存的絕對路徑
string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
//新建一個圖板,以最小等比例壓縮大小繪制原圖
using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//繪制中間圖
using (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(Color.Black);
g.DrawImage(
sourceImage,
new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.GraphicsUnit.Pixel
);
}
//新建一個圖板,以縮略圖大小繪制中間圖
using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
{
//繪制縮略圖
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
int lwidth = (smallWidth - intThumbWidth) / 2;
int bheight = (smallHeight - intThumbHeight) / 2;
g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
g.Dispose();
bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
catch
{
//出錯則刪除
System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
return "圖片格式不正確";
}
//返回縮略圖名稱
return sThumbFile;
}
return "沒有選擇圖片";
}
您可能感興趣的文章:
- ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
- asp.net上傳圖片并作處理水印與縮略圖的實例代碼
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- asp.net圖片上傳生成縮略圖的注意事項
- asp.net 上傳圖片并同時生成縮略圖的代碼
- ASP.NET實現(xiàn)上傳圖片并生成縮略圖的方法
- Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
- asp.net 添加水印的代碼(已測試)
- asp.net下GDI+的一些常用應用(水印,文字,圓角處理)技巧
- asp.net如何在圖片上加水印文字具體實現(xiàn)
- asp.net實現(xiàn)生成縮略圖及給原始圖加水印的方法示例
相關文章
ASP.NET?Core設置Ocelot網(wǎng)關限流
這篇文章介紹了ASP.NET?Core設置Ocelot網(wǎng)關限流的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04asp.net AjaxControlToolKit--TabContainer控件的介紹
ModalPopup控件允許一個asp頁面的部分內(nèi)容以對話框的模式顯示給用戶,同時會限制用戶于頁面的其他部分交互。對話框顯示的內(nèi)容可以是一個層級,這個層級的背景可以使用戶自定義的格式,簡單的理解好比是一個對話框彈出來后,主頁面會顯示灰色,且不可操作。2009-06-06ASP.NET中各種連接數(shù)據(jù)庫的配置的方法及json數(shù)據(jù)轉(zhuǎn)換
本篇文章主要介紹了ASP.NET中各種連接數(shù)據(jù)庫的配置的方法,詳細的介紹了MSSQL、Access、Oracle、SQLite、MySQL數(shù)據(jù)庫配置,具有一定的參考價值,有興趣的可以了解一下。2017-01-01.NET程序集引用COM組件MSScriptControl遇到問題的解決方法
這篇文章主要為大家詳細介紹了.NET程序集引用COM組件MSScriptControl遇到問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01