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
AttachmentsImagewithGivenString (34487-29746-Captha.zip)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|