Sunday, 20 July 2014

How to Convert Text Into Image(.jpg) Format Using C#.Net In Asp.Net

7/20/2014 - By Pranav Singh 0

This article will show you how you can convert text into image(.jpg) format using c#.net In asp.net. In this we will click on button control to display the entered text into image format.

So for this article first we will create a new asp.net application and add button control, a textcontrol and an image control to display the converted text into image.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HtmlTextToOImage.aspx.cs"
    Inherits="demoasp_net.HtmlTextToOImage" %>

<!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>How to Convert Text Into Image(.jpg) Format Using C#.Net In Asp.Ne</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="198px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <br />
    </div>
    <div>
        <asp:Image ID="Image1" runat="server" Width="200" Height="60" />
    </div>
    </form>
</body>
</html>

Now click on button to generate the button click event and add the below code.

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;

namespace demoasp_net
{
    public partial class HtmlTextToOImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = Convert.ToString(GenerateImage("Vinay"));
        }
        public Bitmap GenerateImage(string imgText)
        {
            Color FontColor = Color.Blue;
            Color BackColor = Color.White;
            String FontName = "Arial";
            int FontSize = 60;
            int Height = 100;
            int Width = 300;
            Bitmap bitmap = new Bitmap(Width, Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            Color color = Color.Gray; ;
            Font font = new Font(FontName, FontSize);
            SolidBrush BrushForeColor = new SolidBrush(FontColor);
            SolidBrush BrushBackColor = new SolidBrush(BackColor);
            Pen BorderPen = new Pen(color);
            Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
            graphics.FillRectangle(BrushBackColor, displayRectangle);
            graphics.DrawRectangle(BorderPen, displayRectangle);

            StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
            StringFormat format2 = new StringFormat(format1);
            graphics.DrawString(imgText, font, Brushes.Blue, (RectangleF)displayRectangle, format2);
            Response.ContentType = "image/jpeg";
            bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
            return bitmap;
        }
    }
}

Now run the application see the output.


 DOWNLOAD

Tags: ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top