C# 添加文字水印類代碼
更新時間:2009年05月20日 00:32:31 作者:
可以添加文字水印的c# 類函數(shù)。
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace Chen
{
public class warterfont
{
public void addtexttoimg(string filename, string text)
{
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(filename)))
{
throw new FileNotFoundException("the file don't exist!");
}
if (text == string.Empty)
{
return;
}
//還需要判斷文件類型是否為圖像類型,這里就不贅述了
Image image = Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(filename));
Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
Graphics g = Graphics.FromImage(bitmap);
float fontsize = 12.0f; //字體大小
float textwidth = text.Length * fontsize; //文本的長度
//下面定義一個矩形區(qū)域,以后在這個矩形里畫上白底黑字
float rectx = 0;
float recty = 0;
float rectwidth = text.Length * (fontsize + 8);
float rectheight = fontsize + 8; //聲明矩形域
RectangleF textarea = new RectangleF(rectx, recty, rectwidth, rectheight);
Font font = new Font("宋體", fontsize); //定義字體
Brush whitebrush = new SolidBrush(Color.White); //白筆刷,畫文字用
Brush blackbrush = new SolidBrush(Color.Black); //黑筆刷,畫背景用
g.FillRectangle(blackbrush, rectx, recty, rectwidth, rectheight);
g.DrawString(text, font, whitebrush, textarea);
MemoryStream ms = new MemoryStream(); //保存為jpg類型
bitmap.Save(ms, ImageFormat.Jpeg); //輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁面中了
bitmap.Save(System.Web.HttpContext.Current.Server.MapPath("/" + "aa.jpg"), ImageFormat.Jpeg); //保存到磁盤上
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
g.Dispose();
bitmap.Dispose();
image.Dispose();
}
}
}
相關(guān)文章
asp.net使用for循環(huán)實(shí)現(xiàn)Datalist的分列顯示功能
工程業(yè)績--用for循環(huán)代替了DataList多列顯示,得到2行四列的表格,需要內(nèi)存表的8行數(shù)據(jù)2009-12-12asp.net用url重寫URLReWriter實(shí)現(xiàn)任意二級域名 高級篇
Asp.net 用url重寫(URLReWriter)實(shí)現(xiàn)任意二級域名,需要的朋友可以參考下,建議先看一下上一篇文章。2009-11-11.net 應(yīng)對網(wǎng)站訪問壓力的方案總結(jié)
本文將總結(jié)下一些應(yīng)對網(wǎng)站訪問壓力的技術(shù)方案。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02實(shí)例講解.NET中資源文件的創(chuàng)建與使用
資源文件顧名思義就是存放資源的文件。資源文件在程序設(shè)計(jì)中有著自身獨(dú)特的優(yōu)勢,他獨(dú)立于源程序,這樣資源文件就可以被多個程序使用2011-12-12使用?HttpReports?監(jiān)控?.NET?Core?應(yīng)用程序的方法
這篇文章主要介紹了使用?HttpReports?監(jiān)控?.NET?Core?應(yīng)用程序的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03ASP.NET中利用存儲過程實(shí)現(xiàn)模糊查詢
ASP.NET中利用存儲過程實(shí)現(xiàn)模糊查詢...2006-09-09ASP.NET?Core通用主機(jī)的系統(tǒng)配置
這篇文章介紹了ASP.NET?Core通用主機(jī)系統(tǒng)配置的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07