Tuesday, 16 July 2013

Text As WaterMark

DEFAULT.ASPX


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StringToImage.aspx.cs" Inherits="StringToImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Enter Text:
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
       
       Enter WaterMark Text:  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
       File Name:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
       <asp:Button ID="btnConvert" runat="server" OnClick="Button1_Click" Text="Covert" /><br />
         
        <asp:Image ID="Image1" runat="server" />

       <br />
    
    </div>
    </form>
</body>
</html>


DEFAULT.ASPX.CS


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Configuration;
using System.Text;
using System.Drawing.Drawing2D;

public partial class StringToImage : System.Web.UI.Page
{
    //string FileName = "MyImage";
    //string FontName = "Arial";
    //Color FontColor = Color.BlueViolet;
    //Color BackColor = Color.Red;
    //int Height = 250;
    //int Width = 50;
    //Color objColor;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Text = TextBox1.Text;
        Color FontColor = Color.Black;
        Color BackColor = Color.Yellow;
        string FontName = "Times New Roman";
        int FontSize = 14;
        int Height = 200;
        int Width = 200;
        string FileName = TextBox3.Text;
        Bitmap objBitmap = new Bitmap(Width, Height);
        Graphics objGraphics = Graphics.FromImage(objBitmap);
        Color objColor;
        Font objFont = new Font(FontName, FontSize);
        PointF objPoint = new PointF(5f, 5f);
        SolidBrush objBrushForeColor = new SolidBrush(FontColor);
        SolidBrush objBrushBackColor = new SolidBrush(BackColor);
        objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height);
        objGraphics.DrawString(Text, objFont, objBrushForeColor, objPoint);
        //string From = ConfigurationManager.AppSettings["FromPath"].ToString();
        //string To = ConfigurationManager.AppSettings["ToPath"].ToString();
        string FromPath = @"C:\Documents and Settings\rajasekhary\Desktop\newfolder\TextAsWaterMark\Web\Image\" + FileName + ".GIF";
        ///string ToPath = @To + FileName + ".JPG";
        objBitmap.Save(FromPath, ImageFormat.Gif);
        //Image1.ImageUrl ="~/Questions/Original/" + FileName + ".JPG";
        string mainImage = @"C:\Documents and Settings\rajasekhary\Desktop\newfolder\TextAsWaterMark\Web\Image\" + FileName + ".GIF";
        ImageSetting(TextBox2.Text, "", mainImage);
        Image1.ImageUrl = "~/Image/" + FileName + "_new" + ".GIF";
    }
    void ImageSetting(string wmText, string wmImage, string mainImage)
    {
        byte[] imageBytes = null;
        if (File.Exists(mainImage))
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(mainImage);
            Graphics graphic;
            if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
            {
                // Graphic is not a Indexed (GIF) image
                graphic = Graphics.FromImage(image);
            }
            else
            {
                /* Cannot create a graphics object from an indexed (GIF) image.
                 * So we're going to copy the image into a new bitmap so
                 * we can work with it. */
                Bitmap indexedImage = new Bitmap(image);
                graphic = Graphics.FromImage(indexedImage);

                // Draw the contents of the original bitmap onto the new bitmap.
                graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                image = indexedImage;
            }
            graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;

            //Text Watermark properties
            Font myFont = new Font("Arial", 26, FontStyle.Bold);
            SolidBrush brush = new SolidBrush(Color.FromArgb(70, Color.Red));
            SizeF textSize = new SizeF();
            if (wmText != "")
                textSize = graphic.MeasureString(wmText, myFont);

            //Image Watermark
            System.Drawing.Image ig = null;
            if (wmImage != "")
                ig = System.Drawing.Image.FromFile(wmImage);

            //Code by me
            float y = (image.Height / 2) - (textSize.Height / 2);
            float x = (image.Width / 2) - (textSize.Width / 2);
            PointF pointF = new PointF(x, y);
            graphic.DrawString(wmText, myFont, brush, pointF);

            // Write the text watermark and image watermark across the main image.
            /*for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    PointF pointF = new PointF(x, y);
                    if (wmText != "")
                    {
                        graphic.DrawString(wmText, myFont, brush, pointF);
                        x += Convert.ToInt32(textSize.Width);
                    }
                    if (wmImage != "")
                    {
                        graphic.DrawImage(ig, pointF);
                        x += Convert.ToInt32(ig.Width);
                    }
                }
                if (wmText != "")
                    y += Convert.ToInt32(textSize.Height);
                if (wmImage != "")
                    y += Convert.ToInt32(ig.Height);
            }*/
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // save image in memoryStream with it format which get it from GetImageFormat function
                image.Save(memoryStream, GetImageFormat(mainImage));
                imageBytes = memoryStream.ToArray();
            }
            graphic.Dispose();
        }
        //Image1.ImageUrl="
        MemoryStream ms = new MemoryStream(imageBytes);
        System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
        //save new image and rename it;
        returnImage.Save(mainImage.Insert(mainImage.LastIndexOf("."), "_new"));
        ms.Dispose();
        returnImage.Dispose();
    }

    //Bitmap ConvertStringToImage(string inputString)
    //{
        
    //    Bitmap b = new Bitmap(250, 50);

    //    Font f = new Font("Arial", 15F);
    //    PointF objPoint = new PointF(5f, 5f);

    //    Graphics g = Graphics.FromImage(b);

    //    SolidBrush whiteBrush = new SolidBrush(Color.BlueViolet);
    //    SolidBrush blackBrush = new SolidBrush(Color.Red);

    //    RectangleF canvas = new RectangleF(0, 0, 250, 50);

    //    g.FillRectangle(whiteBrush, canvas);
    //    g.DrawString(inputString, f, blackBrush, canvas);
    //    b.Save(@"E:\" + FileName + ".GIF", ImageFormat.Gif);
    //    return b;
    //}

    ImageFormat GetImageFormat(String path)
    {
        switch (Path.GetExtension(path).ToLower())
        {
            case ".bmp": return ImageFormat.Bmp;
            case ".gif": return ImageFormat.Gif;
            case ".jpg": return ImageFormat.Jpeg;
            case ".png": return ImageFormat.Png;
            default: return null;
        }
    }
}

No comments:

Post a Comment