国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 添加文字水印類代碼

2024-09-07 17:05:15
字體:
來源:轉載
供稿:網友
代碼如下:
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; //文本的長度
//下面定義一個矩形區域,以后在這個矩形里畫上白底黑字
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();
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米脂县| 富源县| 聂荣县| 泰安市| 英德市| 肥乡县| 河南省| 治多县| 白山市| 兴文县| 涿鹿县| 临洮县| 长阳| 芷江| 习水县| 东阿县| 武夷山市| 祁阳县| 开阳县| 诸暨市| 南华县| 衡南县| 万源市| 韩城市| 高尔夫| 太原市| 皮山县| 锡林浩特市| 伊宁市| 隆德县| 蒲城县| 荥阳市| 二手房| 六枝特区| 日土县| 舞阳县| 洪江市| 城口县| 湟源县| 策勒县| 泸西县|