public string AddWatermark(string pPath, string pWatermark)
{
Image image = Image.FromFile(pPath);
Bitmap bmp = new Bitmap(image);
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
ImageCodecInfo iciJpegCodec = null;
//find the correct Codec and specify its quality
EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);
// Get all image codecs that are available
ImageCodecInfo[] iciCodecs = ImageCodecInfo.GetImageEncoders();
// Store the quality parameter in the list of encoder parameters
EncoderParameters epParameters = new EncoderParameters(1);
epParameters.Param[0] = epQuality;
// Loop through all the image codecs
for (int i = 0; i < iciCodecs.Length; i++)
{
// Until the one that we are interested in is found, which is image/jpeg
if (iciCodecs[i].MimeType == "image/jpeg")
{
iciJpegCodec = iciCodecs[i];
break;
}
}
using (Graphics gr = Graphics.FromImage(bmp))
{
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
gr.DrawImage(image, new Rectangle(0,0, bmp.Width, bmp.Height));
//gr.DrawString(pWatermark, new Font("Arial", 40), Brushes.White, new PointF(5, bmp.Height - 20));
gr.DrawString(pWatermark, new Font("Tahoma", 20), Brushes.White, new RectangleF(0, 0, 500, 500), strFormat);
}
bmp.Save(pPath + "test.jpg", iciJpegCodec, epParameters);
bmp.Dispose();
return "Water Mark done successfully";
}
}
No comments:
Post a Comment