0

I have created a handler for writing text on Image which call a function written below

private bool HavException { get; set; }
private string ExceptionMessage { get; set; }
public Bitmap SourceImage { get; set; }
public Bitmap DestinationImage { get; set; }
public ImageMethods()
{
    HavException = false;
    ExceptionMessage = string.Empty;
}
public Image AddWatermarkText(Image img, string textOnImage)
{
    try
    {
        textOnImage = ConfigurationManager.AppSettings["textOnImage"];
        var opacity = Int32.Parse(ConfigurationManager.AppSettings["opicity"]);
        var red = Int32.Parse(ConfigurationManager.AppSettings["red"]);
        var green = Int32.Parse(ConfigurationManager.AppSettings["green"]);
        var blue = Int32.Parse(ConfigurationManager.AppSettings["blue"]);
        var fontSize = Int32.Parse(ConfigurationManager.AppSettings["fontSize"]);
        var fontName = ConfigurationManager.AppSettings["fontName"];

        var lobFromImage = Graphics.FromImage(img);
        var lobFont = new Font(fontName, fontSize, FontStyle.Bold);
        var lintTextHw = lobFromImage.MeasureString(textOnImage, lobFont);
        var lintTextOnImageWidth = (int)lintTextHw.Width;
        var lintTextOnImageHeight = (int)lintTextHw.Height;
        var lobSolidBrush = new SolidBrush(Color.FromArgb(opacity, Color.FromArgb(red, green, blue)));
        // lobFromImage.Clear(Color.White);
        lobFromImage.DrawImage(img, img.Height, img.Width);

        var posLeft = (img.Width - lintTextOnImageWidth) / 2;
        posLeft = posLeft > 0 ? posLeft : 5;
        var lobPoint = new Point(posLeft, (img.Height / 2) - (lintTextOnImageHeight / 2));
        //  var lobPoint = new Point(RandomNumber(0, img.Width - lintTextOnImageWidth), RandomNumber(0, img.Height - lintTextOnImageHeight));
        lobFromImage.DrawString(textOnImage, lobFont, lobSolidBrush, lobPoint);

        lobFromImage.Save();
        lobFromImage.Dispose();
        lobSolidBrush.Dispose();
        lobFont.Dispose();
    }
    catch (Exception ex)
    {
        HavException = true;
        ExceptionMessage = ex.Message;
    }
    return img;
}

Every thing is working fine but the size of image is getting increased by 2 to three times. Is there any way that the size does not increase too much. I have jpg as original image.

Thanks

1 Answer 1

1

Following call does not make sense and may be the culprit for inflating your image size:

lobFromImage.DrawImage(img, img.Height, img.Width);

This would draw the original image at (height, width) location - for example, if you have 200 x 100 image then above call would draw the image at (100, 200) location and possibly stretching your canvas to 300 x 300 size.

For adding the watermark, all you need to do is to draw the text - so my guess is by removing above line may do the trick.

Also lobFromImage.Save(); looks suspicious - it saves the graphic's object state on stack and doesn't have anything to do with saving the image on the disk.

Sign up to request clarification or add additional context in comments.

7 Comments

I have removed both the line but still the image size is increasing. It is getting all most double of its original size. Do you now a better approach.
@krshekhar, how do you open/save your image file? Post that code as well as!
I have a centralized image server to be used by the different application. I have created a http handler for any request and response from the image server. which is as follow
public void ProcessRequest(HttpContext context) { var imagePath = context.Request.QueryString["ID"]; var absolutePath = context.Server.MapPath(imagePath); var originalImage = Image.FromFile(absolutePath); originalImage = new ImageMethods().AddWatermarkText(originalImage, "One Click"); context.Response.ContentType = "image/jpeg"; originalImage.Save(context.Response.OutputStream, ImageFormat.Bmp); }
originalImage.Save(context.Response.OutputStream, ImageFormat.Bmp);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.