C# 添加文字水印類代碼
更新時(shí)間: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; //文本的長(zhǎng)度
//下面定義一個(gè)矩形區(qū)域,以后在這個(gè)矩形里畫上白底黑字
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); //輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁(yè)面中了
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實(shí)現(xiàn)刪除DataGrid的記錄時(shí)彈出提示信息
這篇文章主要介紹了asp.net實(shí)現(xiàn)刪除DataGrid的記錄時(shí)彈出提示信息,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
.Net?Core?使用?TagProvider?與?Enricher?豐富日志的操作代碼
這篇文章主要介紹了.Net?Core?使用?TagProvider?與?Enricher?豐富日志的操作,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
Community Server專題一:概述Community Server
Community Server專題一:概述Community Server...2007-03-03
Global.asax取物理路徑/取絕對(duì)路徑具體方法
本文章來(lái)給大家簡(jiǎn)單介紹利用Global.asax取物理路徑和取絕對(duì)路徑代碼,有需要了解的朋友可參考參考2013-08-08
關(guān)于利用RabbitMQ實(shí)現(xiàn)延遲任務(wù)的方法詳解
最近在使用RabbitMQ來(lái)實(shí)現(xiàn)延遲任務(wù)的時(shí)候發(fā)現(xiàn),這其中的知識(shí)點(diǎn)還是挺多的,所以下面這篇文章主要給大家介紹了關(guān)于利用RabbitMQ實(shí)現(xiàn)延遲任務(wù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12
從請(qǐng)求管道深入剖析HttpModule的實(shí)現(xiàn)機(jī)制圖文介紹
想要了解底層的原理必須對(duì)請(qǐng)求處理過(guò)程和頁(yè)面的生命周期有點(diǎn)了解才方便您入門學(xué)習(xí)一下內(nèi)容,本文將詳細(xì)介紹2012-11-11
ASP.NET MVC使用EasyUI的datagrid多選提交保存教程
ASP.NET MVC使用EasyUI的datagrid多選提交保存教程,需要的朋友可以參考下。2011-12-12

