using System;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
namespace Greatwall.Image
{
/// <summary>
/// ImageModify 的摘要說明。
/// </summary>
public class ImageModify
{
public ImageModify()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
private string fileUrl = "";
private string textFontsizeXYName;
private string textFontsizeXYID;
private string textFontsizeXYDate;
public string FielUrl
{
get
{
return fileUrl;
}
set
{
fileUrl = value;
}
}
public string TextFontSizeXYName
{
get
{
return textFontsizeXYName ;
}
set
{
textFontsizeXYName = value;
}
}
public string TextFontSizeXYID
{
get
{
return textFontsizeXYID;
}
set
{
textFontsizeXYID = value;
}
}
public string TextFontSizeXYDate
{
get
{
return textFontsizeXYDate;
}
set
{
textFontsizeXYDate = value;
}
}
public byte[] AddTextToImg()
{
if(!File.Exists(fileUrl))
{
throw new FileNotFoundException("The file don't exist!");
}
string[] strArrayUserName = this.textFontsizeXYName.Split(',');
string[] strArrayID = this.textFontsizeXYID.Split(',');
string[] strArrayDate = this.textFontsizeXYDate.Split(',');
//還需要判斷檔案類型是否為圖像類型,這裡就不贅述了
System.Drawing.Image image = System.Drawing.Image.FromFile(fileUrl);
Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
Graphics g = Graphics.FromImage(bitmap);
float fontSize = float.Parse(strArrayUserName[1]); //字型大小
float textWidth = strArrayUserName[0].Length*fontSize; //文本的長度
//下面定義一個矩形區域,以後在這個矩形裡畫上白底黑字
float rectX =float.Parse(strArrayUserName[2]);
float rectY = float.Parse(strArrayUserName[3]);
float rectWidth = strArrayUserName[0].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.Black); //白筆刷,畫文字用
Brush blackBrush = new SolidBrush(Color.Transparent); //黑筆刷,畫背景用
g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString( strArrayUserName[0],font,whiteBrush,textArea);
fontSize = float.Parse(strArrayID[1]); //字型大小
textWidth = strArrayID[0].Length*fontSize; //文本的長度
//下面定義一個矩形區域,以後在這個矩形裡畫上白底黑字
rectX =float.Parse(strArrayID[2]);
rectY = float.Parse(strArrayID[3]);
rectWidth = strArrayID[0].Length*(fontSize+8);
rectHeight = fontSize+8;
textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString( strArrayID[0],font,whiteBrush,textArea);
fontSize = float.Parse(strArrayDate[1]); //字型大小
textWidth = strArrayDate[0].Length*fontSize; //文本的長度
//下面定義一個矩形區域,以後在這個矩形裡畫上白底黑字
rectX =float.Parse(strArrayDate[2]);
rectY = float.Parse(strArrayDate[3]);
rectWidth = strArrayDate[0].Length*(fontSize+12);
rectHeight = fontSize+8;
textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString( strArrayDate[0],font,whiteBrush,textArea);
MemoryStream ms = new MemoryStream( );
//儲存為Jpg類型
bitmap.Save(ms,ImageFormat.Jpeg);
g.Dispose();
bitmap.Dispose();
image.Dispose();
return ms.ToArray();
}
}
}