Monday, November 30, 2009

watermarkup image using C#

watermarkup image ...

first pass bit map imags and ur watermark up images width and original image width and height......

public System.Drawing.Bitmap watermarking(System.Drawing.Bitmap img, int logowidth, int width, int height)
{

Graphics g = Graphics.FromImage(img);
System.Drawing.Image orilogo = System.Drawing.Image.FromFile(Server.MapPath("~/images/logo.png"));

int logoheight = Math.Abs((orilogo.Height * logowidth) / orilogo.Width);

System.Drawing.Bitmap newPic = new System.Drawing.Bitmap(logowidth, logoheight);
System.Drawing.Graphics logogr = System.Drawing.Graphics.FromImage(newPic);
logogr.SmoothingMode = SmoothingMode.AntiAlias;
logogr.InterpolationMode = InterpolationMode.HighQualityBicubic;
logogr.PixelOffsetMode = PixelOffsetMode.HighQuality;


logogr.DrawImage(orilogo, 0, 0, logowidth, logoheight);

newPic.Save(Server.MapPath("~/images/magnustempimg/logo.png"), orilogo.RawFormat);

logogr.Dispose();
newPic.Dispose();
orilogo.Dispose();
System.Drawing.Image logo = System.Drawing.Image.FromFile(Server.MapPath("~/images/magnustempimg/logo.png")); //This is your watermark

Bitmap TransparentLogo = new Bitmap(logo.Width, logo.Height); //Create a blank bitmap object //to which we //draw our transparent logo

Graphics TGraphics = Graphics.FromImage(TransparentLogo);//Create a graphics object so that //we can draw //on the blank bitmap image object

ColorMatrix ColorMatrix = new ColorMatrix(); //An image is represenred as a 5X4 matrix(i.e 4 //columns and 5 //rows)

ColorMatrix.Matrix33 = 1.00F;//the 3rd element of the 4th row represents the transparency

ImageAttributes ImgAttributes = new ImageAttributes();//an ImageAttributes object is used to set all //the alpha //values.This is done by initializing a color matrix and setting the alpha scaling value in the matrix.The address of //the color matrix is passed to the SetColorMatrix method of the //ImageAttributes object, and the //ImageAttributes object is passed to the DrawImage method of the Graphics object.

ImgAttributes.SetColorMatrix(ColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
int x = (width / 2) - (logo.Width / 2);
int y = (height / 2) - (logo.Height / 2);
//TGraphics.DrawImage(logo, new Rectangle(0, 0, TransparentLogo.Width, TransparentLogo.Height), 0, 0, TransparentLogo.Width, TransparentLogo.Height, GraphicsUnit.Pixel, ImgAttributes);
TGraphics.DrawImage(logo, new Rectangle(0, 0, TransparentLogo.Width, TransparentLogo.Height), 0, 0, TransparentLogo.Width, TransparentLogo.Height, GraphicsUnit.Pixel, ImgAttributes);
TGraphics.Dispose();


g.DrawImage(TransparentLogo, x, y);
g.Dispose();
logo.Dispose();
return img;
}

No comments: