public void GenerateWatermarkedImage()
{
// Load
Source Image
String
strSourceImagePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["SourceImagePath"]);
Image
imgSource = Image.FromFile(strSourceImagePath);
//Create
New Bitmap Image
int
socWidth = imgSource.Width;
int
socHeight = imgSource.Height;
Bitmap
bmPhoto = new Bitmap(socWidth,
socHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics
grPhoto = Graphics.FromImage(bmPhoto);
//Load
Logo Image
String
strLogoImagePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["LogoImagePath"]);
Image
imgLogo = new Bitmap(strLogoImagePath);
int
logoWidth = imgLogo.Width;
int
logoHeight = imgLogo.Height;
//#####################################
//Step
#1 - Watermark Text
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(
imgSource,
new
Rectangle(0, 0, socWidth, socHeight),
0,
0,
socWidth,
socHeight,
GraphicsUnit.Pixel);
//Determine
the largest possible size for WaterMark Text Message
int[]
sizes = new int[]
{ 16, 14, 12, 10, 8, 6, 4 };
Font
crFont = null;
SizeF
crSize = new SizeF();
string
strWaterMarkText = TxtWatermark.Text;
for
(int i = 0; i < 7; i++)
{
crFont = new Font("arial", sizes[i], FontStyle.Bold);
crSize =
grPhoto.MeasureString(strWaterMarkText, crFont);
if
((ushort)crSize.Width < (ushort)socWidth)
break;
}
//String
Alignment to Center
int
yPixlesFromBottom = (int)(socHeight * .05);
float
yPosFromBottom = ((socHeight -
yPixlesFromBottom) -
(crSize.Height / 2));
float
xCenterOfImg = (socWidth / 2);
StringFormat
StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//Draw
the WaterMark string at the appropriate position
SolidBrush
semiTransBrush2 = new SolidBrush(Color.FromArgb(153,
0, 0, 0));
grPhoto.DrawString(
strWaterMarkText,
crFont,
semiTransBrush2,
new
PointF(xCenterOfImg + 1, yPosFromBottom +
1),
StrFormat);
SolidBrush
semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
grPhoto.DrawString(
strWaterMarkText,
crFont,
semiTransBrush,
new
PointF(xCenterOfImg, yPosFromBottom),
StrFormat);
//#####################################
//Step
#2 - Watermark Logo
Bitmap
bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgSource.HorizontalResolution,
imgSource.VerticalResolution);
Graphics
grWatermark = Graphics.FromImage(bmWatermark);
//Translucent
watermark
ImageAttributes
imageAttributes = new ImageAttributes();
ColorMap
colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[]
remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
//Change
the opacity of the watermark
float[][]
colorMatrixElements = {
new float[]
{1.0f, 0.0f, 0.0f,
0.0f, 0.0f},
new float[]
{0.0f, 1.0f, 0.0f,
0.0f, 0.0f},
new float[]
{0.0f, 0.0f, 1.0f,
0.0f, 0.0f},
new float[]
{0.0f, 0.0f, 0.0f,
0.3f, 0.0f},
new float[]
{0.0f, 0.0f, 0.0f,
0.0f, 1.0f}
};
ColorMatrix
wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//Draw
the watermark
int
xPosOfWm = ((socWidth - logoWidth) - 10);
int
yPosOfWm = 10;
grWatermark.DrawImage(imgLogo,
new
Rectangle(xPosOfWm, yPosOfWm, logoWidth,
logoHeight),
0,
0,
logoWidth,
logoHeight,
GraphicsUnit.Pixel,
imageAttributes);
//Replace
the original Image with the new Bitmap
imgSource = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
//Save
Watermarked Image
String
TragetImagePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["TragetImagePath"]);
imgSource.Save(TragetImagePath);
imgSource.Dispose();
imgLogo.Dispose();
ImgWatermarked.ImageUrl = ConfigurationManager.AppSettings["TragetImagePath"];
}
|