C#實現(xiàn)的上傳圖片、保存圖片、加水印、生成縮略圖功能示例
本文實例講述了C#實現(xiàn)的上傳圖片、保存圖片、加水印、生成縮略圖功能。分享給大家供大家參考,具體如下:
伴隨移動設(shè)備地普及,處理圖片、視頻等需求也變得越來越基礎(chǔ),這里介紹的是圖片的存儲。
上傳圖片必須使用form表單提交的方式,我只知道這一種方法,如果大家知道其他方法的話請留言。
保存圖片、加水印和生成縮略圖這三種功能最好各自放在單獨的方法中,盡量降低耦合度,提高代碼復(fù)用程度,除此之外我們平常寫代碼是也要盡量做到方法功能的唯一性。
前臺代碼:
<form method="POST" enctype="multipart/form-data" action="UploadImg.ashx">
<table>
<tr>
<td>func:</td>
<td><input type="text" name="func"/></td>
</tr>
<tr>
<td>用戶Id:</td>
<td><input type="text" name="userId"/></td>
</tr>
<tr>
<td>頭像:</td>
<td><input type="file" name="icon"/></td>
</tr>
<tr>
<td>水?。?lt;/td>
<td><input type="text" name="waterMark"/></td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>
后臺代碼:
private string UploadImage(HttpContext context)
{
try
{
System.IO.Stream stream = context.Request.Files["icon"].InputStream;
//返回的圖片路徑可以存儲在數(shù)據(jù)庫中
string imageUrl = SaveImage(stream, "Icon", "蟈蟈");
string thumbnailImageUrl = SaveThumbnailImage(stream, "Icon");
string thumbnailImageUrlWithWatermark = SaveThumbnailImage(ConfigurationManager.AppSettings["AttachmentsDirectory"] + imageUrl, "Icon");
return "上傳成功!";
}
catch (Exception ex)
{
return "上傳失??!";
}
}
private string SaveImage(Stream stream, string folderName, string waterMark)
{
try
{
string fileName = Guid.NewGuid() + ".jpg";
string path = ConfigurationManager.AppSettings["AttachmentsDirectory"];
path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\");
string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
if (!string.IsNullOrEmpty(waterMark))
{
Image imgSource = Image.FromStream(stream);
AddWatermarkAndSave(path, fileName, waterMark, imgSource, imgSource.Height - 300, 10, Color.Red,
new Font("宋體", 40));
}
else
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
System.IO.FileStream fs = new System.IO.FileStream(path + fileName, System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
}
return imageUrl + fileName;
}
catch (Exception ex)
{
return "";
}
}
private string SaveThumbnailImage(Stream stream, string folderName)
{
try
{
string fileName = Guid.NewGuid() + ".jpg";
string path = ConfigurationManager.AppSettings["AttachmentsDirectory"];
path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\");
string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse);
//數(shù)據(jù)源來自Stream
Image image = System.Drawing.Bitmap.FromStream(stream);
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
thumbnailImage.Save(path + fileName);
thumbnailImage.Dispose();
return imageUrl + fileName;
}
catch (Exception ex)
{
return "";
}
}
private string SaveThumbnailImage(string originalFileName, string folderName)
{
try
{
string fileName = Guid.NewGuid() + ".jpg";
string path = ConfigurationManager.AppSettings["AttachmentsDirectory"];
path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\");
string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse);
//數(shù)據(jù)源來自File
Image image = System.Drawing.Bitmap.FromFile(originalFileName);
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
thumbnailImage.Save(path + fileName);
thumbnailImage.Dispose();
return imageUrl + fileName;
}
catch (Exception ex)
{
return "";
}
}
private bool GetFalse()
{
return false;
}
/// <summary>
/// 圖片加文字水印
/// </summary>
/// <param name="fileName"> </param>
/// <param name="text">水印文字,如果是多行用分號隔開</param>
/// <param name="img">圖片</param>
/// <param name="paddingTop">上邊距</param>
/// <param name="paddingLeft">左邊距</param>
/// <param name="textColor">文字顏色</param>
/// <param name="textFont">字體</param>
/// <param name="path">保存地址</param>
/// <returns></returns>
private bool AddWatermarkAndSave(string path, string fileName, string text, Image img,
int paddingTop, int paddingLeft, Color textColor, Font textFont)
{
text = text + ";" + "當(dāng)前時間:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
textFont = new Font("宋體", 19);
Bitmap bm = new Bitmap(img);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm);
System.Drawing.Brush b = new SolidBrush(textColor);
string[] str = text.Split(';');
for (int i = 0; i < str.Length; i++)
g.DrawString(str[i], textFont, b, paddingLeft, paddingTop + 33 * i);
g.Dispose();
bm.Save(path + fileName, ImageFormat.Jpeg);
bm.Dispose();
return true;
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
上一篇:Unity shader實現(xiàn)自由放大縮小效果
欄 目:C#教程
下一篇:Unity3D UGUI特效之Image高斯模糊效果
本文標(biāo)題:C#實現(xiàn)的上傳圖片、保存圖片、加水印、生成縮略圖功能示例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4886.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10C#停止線程的方法
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子


