?? watermark.cs
字號:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace PhotoSprite.ImageProcessing
{
/// <summary>
/// 圖像水印處理類
/// </summary>
public class Watermark : ImageInfo
{
/**************************************************
*
* 水印文字定位
*
* X 坐標、Y 坐標、對齊方式
*
**************************************************/
/// <summary>
/// 獲取或設置水印文字 X 坐標
/// </summary>
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
/// <summary>
/// X 坐標
/// </summary>
int x = 0;
/// <summary>
/// 獲取或設置水印文字 Y 坐標
/// </summary>
public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
/// <summary>
/// Y 坐標
/// </summary>
int y = 0;
/// <summary>
/// 水印文字對齊方式
/// </summary>
public Function.AlignMode Align
{
get
{
return align;
}
set
{
align = value;
}
}
/// <summary>
/// 對齊方式
/// </summary>
Function.AlignMode align = Function.AlignMode.TopLeft;
/**************************************************
*
* 文字屬性
*
* 字體簇、大小、字形、字體顏色
*
**************************************************/
/// <summary>
/// 獲取或設置字體簇
/// </summary>
public string FontFamily
{
get
{
return fontFamily;
}
set
{
fontFamily = value;
}
}
/// <summary>
/// 字體簇
/// </summary>
string fontFamily = "Arial";
/// <summary>
/// 獲取或設置字體大小
/// </summary>
public int FontSize
{
get
{
return fontSize;
}
set
{
fontSize = value;
}
}
/// <summary>
/// 字體大小
/// </summary>
int fontSize = 30;
/// <summary>
/// 獲取或設置字體風格
/// </summary>
public System.Drawing.FontStyle FontWeight
{
get
{
return fontWeight;
}
set
{
fontWeight = value;
}
}
/// <summary>
/// 字體風格
/// </summary>
System.Drawing.FontStyle fontWeight = FontStyle.Regular;
/// <summary>
/// 獲取或設置字體顏色
/// </summary>
public Color FontColor
{
get
{
return fontColor;
}
set
{
fontColor = value;
}
}
/// <summary>
/// 字體顏色
/// </summary>
Color fontColor = Color.Red;
/************************************************************
*
* 統計像素點、水印文字
*
************************************************************/
/// <summary>
/// 統計出一個字符含多少個像素點
/// </summary>
/// <param name="character">字符</param>
/// <returns></returns>
public int CountTextDots(char character)
{
Bitmap b = new Bitmap(20, 20);
Graphics g = Graphics.FromImage(b);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Font font = new Font(new FontFamily("宋體"), 12, FontStyle.Regular);
SolidBrush brush = new SolidBrush( Color.White);
g.DrawString(character.ToString(), font, brush, 0, 0);
g.Save();
GrayProcessing gp = new GrayProcessing();
byte[,] Gray = gp.BinaryArray(b, 128);
int count = 0;
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 20; x++)
{
// 統計白點
if (Gray[x, y] > 128)
count++;
} // x
} // y
b.Dispose();
return count;
} // end of CountTextDots
/// <summary>
/// 水印文字
/// </summary>
/// <param name="b">位圖流</param>
/// <param name="text">水印文字</param>
/// <param name="isHorz">是否按水平方式在圖像上寫文字</param>
/// <returns></returns>
public Bitmap WaterText(Bitmap b, string text, bool isHorz)
{
int W = b.Width;
int H = b.Height;
Graphics g = Graphics.FromImage(b);
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; // 高質量插值
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 高質量平滑
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // 消除鋸齒
// 確定是水平還是垂直顯示文字
System.Drawing.StringFormat stringFormat = new StringFormat();
if (!isHorz)
{
// 垂直顯示文字
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
}
// 產生水印文字
Font waterFont = new Font(fontFamily, fontSize, fontWeight, GraphicsUnit.Pixel);
// 顏色
SolidBrush brushColor = new SolidBrush(fontColor);
// 根據指定的對齊方式對文字進行定位
Point point = new Point(x, y);
// 繪制水印文字
g.DrawString(text, waterFont, brushColor, point.X, point.Y, stringFormat);
g.Save();
g.Dispose();
return b;
} // end of WaterText
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -