简体   繁体   中英

writing text on image asp.net (without affecting size)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM