Task: Show Image With Watermark dynamically using Asp.net and C# code.
Description: Sometime we need to add watermark with image on website so that no one can copy and reuse it. So that we can do it by a vary simple way using our back-end code. Here I write a code for achieving this task.
Here we can use image URL to that page URL where we write a code for merging these two images. And in code file I make a function for it we can call it in page_load event. In this function we have to pass both image URL, our image and watermark (watermark should be less then of image size - I will work later for resolve this issue also) and two points x and y for watermark position on that image (these points should be appropriate I also resolve this issue in my next post).
Watermark image should be "PNG" for better result.
Here I show the repetitive watermark on image, You can use it single or anyway as you w...
Call the page where you code for image merging :
Dynamic Code for image generation and merging (dynamicimage.aspx.cs)
public void drawimage(string
imageurl, string watermark, int startx, int
starty)
Description: Sometime we need to add watermark with image on website so that no one can copy and reuse it. So that we can do it by a vary simple way using our back-end code. Here I write a code for achieving this task.
Here we can use image URL to that page URL where we write a code for merging these two images. And in code file I make a function for it we can call it in page_load event. In this function we have to pass both image URL, our image and watermark (watermark should be less then of image size - I will work later for resolve this issue also) and two points x and y for watermark position on that image (these points should be appropriate I also resolve this issue in my next post).
Watermark image should be "PNG" for better result.
Here I show the repetitive watermark on image, You can use it single or anyway as you w...
Call the page where you code for image merging :
Image1.ImageUrl = "dynamicimage.aspx";
Dynamic Code for image generation and merging (dynamicimage.aspx.cs)
using
System.Drawing;
using
System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
protected void Page_Load(object
sender, EventArgs e)
{
string
ThumbnailPath = Server.MapPath(@"images/testimage.jpg");
string watermark
=Server.MapPath(@"images/watermark.png");
drawimagewm(ThumbnailPath, watermark, 50, 50);
}
{
string
ThumbnailPath = imageurl;
string
Thumb_WMPath = watermark;
System.Drawing.Image
image = System.Drawing.Image.FromFile(ThumbnailPath);
System.Drawing.Image
img_WM = System.Drawing.Image.FromFile(Thumb_WMPath);
int width =
image.Width;
int height
= image.Height;
int width2
= img_WM.Width;
int height2
= img_WM.Height;
System.Drawing.Graphics
graphics = System.Drawing.Graphics.FromImage(image);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
if (startx
> (width - width2) || starty > (height - (height2)))
{
graphics.DrawImage(image2, new Point(startx,
starty));
}
// I use 50 for gapping between two watermark...
for (int i = startx; i < (width - (width2)); i = i +
width2 + 50)
{
for (int j = starty; j < (height - (height2)); j = j +
height2 + 50)
{
graphics.DrawImage(img_WM, new Point(i, j));
}
}
ImageCodecInfo[]
info = ImageCodecInfo.GetImageEncoders();
EncoderParameters
encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
string sExt
= System.IO.Path.GetExtension(ThumbnailPath);
Response.ContentType = "image/jpeg";
if
(sExt.ToString() == ".jpg" ||
sExt.ToString() == ".jpeg")
{
image.Save(Response.OutputStream,
info[1], encoderParameters);
}
else
{
image.Save(Response.OutputStream,
image.RawFormat);
}
image.Dispose();
img_WM.Dispose();
}
No comments:
Post a Comment