How to add a a red colored text caption to the image in ASP.NET application without any 3rd party components or libraries

  • Home
  • /
  • Articles
  • /
  • How to add a a red colored text caption to the image in ASP.NET application without any 3rd party components or libraries

Tutorial: how to add watermark to a picture using built-in .NET functionality

Looking for a component to add watermarks based on ready-to-use presets?
Check Bytescout Watermarking SDK for .NET to add more attractive watermarks with ready-to-use customizable presets

This tutorial demonstrates how to add a red text (watermark) to the photo file using .NET built-in functionality without any 3rd party components and libraries (like our Watermarking SDK) in ASP.NET web-application

Download source code for this example: HowToAddWatermarkToImageInDotNetWithout3rdPartyTools_asp.zip (300 KB)

Input image:

sample input image

Output image:

output image (text fits page preset)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Load image
System.Drawing.Image image = System.Drawing.Image.FromFile(“my_sample_image.jpg”);
// Create graphics from image
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image);
// Create font
System.Drawing.Font font = new System.Drawing.Font(“Times New Roman”, 42.0f);
// Create text position
System.Drawing.PointF point = new System.Drawing.PointF(150, 150);
// Draw text
graphics.DrawString(“Watermark”, font, System.Drawing.Brushes.Red, point);

// clear http output
Response.Clear();
// set the content type to JPEG
Response.ContentType = “image/jpeg”;
// add content type header
Response.AddHeader(“Content-Type”, “image/jpeg”);
// set the content disposition
Response.AddHeader(“Content-Disposition”, “inline;filename=my_sample_image.jpg”);
// Save image
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End();
}
}

Download source code for this example: HowToAddWatermarkToImageInDotNetWithout3rdPartyTools_asp.zip (300 KB)


prev
next