I have an ecommerce store built in asp.net c# (Webforms) and a lot of the new product images are very hard to source, so I'd like to watermark them with our logo or domain name.
There are too many products to just download the images and add the watermark, and users with limited image editing experience will be uploading new ones (So they won't have a clue how to add the watermark).
So I guess this just leaves me with using a HttpHandler? Yes / No? If so can you provide some insight (Preferably code samples in C#) into the most efficient way of adding the watermark, considering some pages will have around 20 images (Jpegs) on (All of which need to be watermarked)
I would obtain the Graphics
object to the jpeg, and then draw the watermark on top of that item, and save it again with the watermark:
using (Image image = Image.FromFile("myImage.jpg"))
using(Graphics g = Graphics.FromImage( image)){
g.DrawImage( myWaterMarkImage, myPosition);
image.Save(myFilename);
}
This looks like it could be helpful:
http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image
While it focus's on text, I am sure with a little modification you could add a graphic in also.
Once you have an implementation you could either call it once per view or when adding prior to saving the file.
Here is a sample HttpHandler
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string imageName = string.Empty;
string physicalPath = string.Empty;
Image image = null;
Image thumbnailImage = null;
Bitmap bitmap = null;
using (MemoryStream memoryStream = new MemoryStream())
{
string actionName = context.Request.QueryString["Image"];
string opacity = context.Request.QueryString["Opacity"];
int opacityPercent = int.Parse(opacity);
Color waterMarkColor = Color.Gray;
switch (actionName)
{
case "BlueHills":
string myCompany = "My Company Name";
Font font = new Font("Times New Roman", 8f);
context.Response.ContentType = "image/png";
bitmap = Resources.Resources.BlueHills;
Graphics g = Graphics.FromImage(bitmap);
Brush myBrush = new SolidBrush(Color.FromArgb(opacityPercent, waterMarkColor));
SizeF sz = g.MeasureString(myCompany, font);
int X = (int)(bitmap.Width - sz.Width) / 2;
int Y = (int)(sz.Height) / 2;
g.DrawString(myCompany, font, myBrush, new Point(X, Y));
bitmap.Save(memoryStream, ImageFormat.Png);
break;
default:
string test = actionName;
break;
}
context.Response.BinaryWrite(memoryStream.GetBuffer());
memoryStream.Close();
if (image != null) { image.Dispose(); }
if (thumbnailImage != null) { thumbnailImage.Dispose(); }
if (bitmap != null) { bitmap.Dispose(); }
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
and can be called like such:
<asp:Image ID="Image1" runat="server" ImageUrl="~/ImageHandler.ashx?Image=BlueHills&Opacity=50" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With