public void AddWaterText(string oldpath, string text, string newpath, object point, System.Drawing.Font font, System.Drawing.Color color, float rotate = 0.0f, int textWidth = 1,int textHeight=1, int repeatD=0)
{
try
{
FileStream fs = new FileStream(oldpath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.Clear(Color.FromName("white"));
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel);
System.Drawing.SizeF crSize = new SizeF();
crSize = gbmPhoto.MeasureString(text, font);
float y = imgPhotoHeight - crSize.Height;
float x = imgPhotoWidth - crSize.Width;
System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
StrFormat.Alignment = System.Drawing.StringAlignment.Center;
if(point!=null)
{
System.Drawing.Point newpos=((System.Drawing.Point)point);
x=newpos.X;
y=newpos.Y;
}
System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(color);
System.Drawing.Color.FromArgb(1,1,1,1);
gbmPhoto.RotateTransform(rotate);
if (repeatD == 0)
{
gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
}
else
{
int xcount = imgPhotoWidth/textWidth+3;
int ycount = imgPhotoHeight/textHeight+3;
float ox = x;
for (int k = 0; k < ycount; k++)
{
for (int i = 0; i < xcount; i++)
{
for (int j = 0; j < xcount; j++)
{
gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
}
x += textWidth+repeatD;
}
x = ox;
y += textHeight+repeatD;
}
}
bmPhoto.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
gbmPhoto.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
}
catch
{
;
}
}
|