You have to create an .aspx file like create-water-mark-image.aspx, which .cs file will be like this,

Request[“ImageFileName”] – > image file

ConfigurationManager.AppSettings[“LOGO_IMAGE”] -> logo image

protected void Page_Load(object sender, EventArgs e)
{
if (Request[“ImageFileName”] != null)
{
string path = Request[“ImageFileName”].ToString();

CreateWaterMarkImage(path, ConfigurationManager.AppSettings[“LOGO_IMAGE”]);
}
}

private void CreateWaterMarkImage(string sourceFileName, string logoFileName)
{
string pathOfSource = Server.MapPath(sourceFileName);
string pathOfLogo = Server.MapPath(logoFileName);

System.Drawing.Image objImage = System.Drawing.Image.FromFile(pathOfSource);
System.Drawing.Image logoImage = System.Drawing.Image.FromFile(pathOfLogo);
int height = objImage.Height;
int width = objImage.Width;
int logoHeight = logoImage.Height;
int logoWidth = logoImage.Width;
int logoStartX = 0;
int logoStartY = 0;

System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);

if (logoWidth < width)
logoStartX = (width – logoWidth) / 2;

if (logoHeight < height)
logoStartY = (height – logoHeight) / 2;

g.DrawImage(logoImage, new System.Drawing.Rectangle(logoStartX, logoStartY, logoWidth, logoHeight), 0, 0, logoWidth, logoHeight, System.Drawing.GraphicsUnit.Pixel, SetImageAttributes());

System.Drawing.Image newImage = bitmapimage;

this.Response.Clear();
this.Response.ContentType = “image/jpeg”;
newImage.Save(this.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

private System.Drawing.Imaging.ImageAttributes SetImageAttributes()
{
System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();

colorMap.OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0);

System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);

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,  1.0f, 0.0f},
new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
};

System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(wmColorMatrix,
System.Drawing.Imaging.ColorMatrixFlag.Default,
System.Drawing.Imaging.ColorAdjustType.Bitmap);

return imageAttributes;
}

This page can be called like

aImageLink.HRef = “create-water-mark-image.aspx?ImageFileName=” + productImage;

Advertisements