C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Creating Image from String


Posted Date: 29 Oct 2009    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: PraveenMember Level: Diamond    
Rating: 1 out of 5Points: 10



Hi,

A Small Article which Converts given Input string to Image in ASP.Net using C#.net

Design Page Over View


<--div>
<--asp:Label ID="lblError" runat="server" Width="100%">
<--/div>
<--div>
<--asp:TextBox ID="txtImageName" runat="server">
<--asp:Button ID="btnImage" runat="server" Text="Create Image"
onclick="btnImage_Click" />
<--/div>
<--div>
<--asp:Image ID="Image1" runat="server" Visible="false" AlternateText="" />
<--/div>


the Button Click Event for Creating Image according to the Given String by the user


protected void btnImage_Click(object sender, EventArgs e)
{
try
{
if (txtImageName.Text.Trim() == "")
throw new Exception("Please enter text to draw image");
CreateBitmapImage(txtImageName.Text.Trim());
}
catch (Exception ex)
{
Image1.Visible = false;
lblError.Text = ex.Message;
lblError.Visible = true;
}
}


the 'Create Bit map Image method is the methods which creates given string to the bit map image


private void CreateBitmapImage(string sImageText)
{
// Creatin Bit map Object
Bitmap objBmpImage = new Bitmap(1, 1);
// As we don't know how much we have to set height and width to the image so declaring two integer variables
int intWidth = 0;
int intHeight = 0;
// Create the Font object for the image text drawing.
Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
// Create a graphics object to measure the text's width and height.
Graphics objGraphics = Graphics.FromImage(objBmpImage);
// This is where the bitmap size is determined.
intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
// Create the bmpImage again with the correct size for the text and font.
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
// Add the colors to the new bitmap.
objGraphics = Graphics.FromImage(objBmpImage);
// Set Background color
objGraphics.Clear(Color.Black);
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// This methods draws the specified text string
objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(255, 255, 240)), 0, 0);
objGraphics.Flush();
// Saving the Created Image in our server Folder
objBmpImage.Save(Server.MapPath("Temp.jpg"));
// setting the image path to the image Control
Image1.ImageUrl = "Temp.jpg";
// setting the height and width to the image as it varies text lenght
Image1.Width = intWidth;
Image1.Height = intHeight;
// showing image to the user
Image1.Visible = true;
}


hope that will give you an idea

Attachments

  • ImagewithGivenString (34487-29746-Captha.zip)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Creating Image from the given string in asp.net  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Send email from gmail account in asp.net
    Previous Resource: Check if Browser supprts JS
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use