Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am new in asp.net mvc i want dynamically generate image and display in view page can any one help me please give me code for that.

this is my code

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;

namespace ImageLabel
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ImageLabel  runat="server">")]
    public class ImageLabel : WebControl
    {
        private string m_URL;

              [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("ImageLabel")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (DesignMode)
                {
                    if (ViewState["Text"] == null)
                        ViewState["Text"] = string.Empty;
                    return (string)ViewState["Text"];
                }
                else
                {
                    string s = PossiblequeryString();

                    if (!string.IsNullOrEmpty(s))
                        return (string)Context.Session[s]; 
                    
                    if (Context.Session[SessionText()] == null)
                        Context.Session[SessionText()] = string.Empty;
                    return (string)Context.Session[SessionText()];
                }
            }

            set
            {
                if (DesignMode)
                {
                    ViewState["Text"] = value;
                }
                else
                {

                    if (PossiblequeryString() == null)
                    {
                        Context.Session[SessionText()] = value;
                    }
               
                }
            }
        }

        private string PossiblequeryString()
        {
            System.Collections.IEnumerator myenum = Context.Request.QueryString.GetEnumerator();
            bool found = false;
            
            while (myenum.MoveNext())
            {
                if (myenum.Current.ToString().StartsWith(secretQueryString))
                {
                    found = true;
                    break;
                }
            }
            if (found)
                return myenum.Current.ToString();
            else
                return null;
        }

        private string SessionText()
        {
            return secretQueryString + this.ClientID;
        }

        private static string secretQueryString = "imgLabel";

        private string QueryStringText()
        {
            return  secretQueryString+ this.ClientID;
        }
       
        protected override void OnInit(EventArgs e)
        {
            if (this.Context.Request.QueryString[QueryStringText()] != null)
            {
                DrawImage(this.Text);
            }  
            this.m_URL = Context.Request.Url.AbsolutePath + "?" + secretQueryString+ this.ClientID + "=1";
           base.OnInit(e);
        }

       
        protected override void Render(HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                base.Render(writer);
            }
            else
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Src, this.m_URL);
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
            }
          
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (DesignMode)
                writer.Write(this.Text);
        }
        private void DrawImage(string Message)
        {

            FontStyle msgFontStyle = SpecifyFontStyle();

            Color foreColor;
            Color backColor;
            {
                if (this.BackColor.IsEmpty)
                    backColor = Color.Transparent;
                else
                    backColor = this.BackColor;

                if (this.ForeColor.IsEmpty)
                    foreColor = Color.Black;
                else
                    foreColor = this.ForeColor;
            }

            float fontsize;
            {
                if (this.Font.Size.Unit.IsEmpty)
                    fontsize = 20;
                else
                    fontsize = (float)this.Font.Size.Unit.Value;
            }

            int height;
            int width;
            {
                if (this.Height.IsEmpty || this.Height == 0)
                    height = (int)fontsize + 8;
                else
                    height = (int)this.Height.Value;
                width = 100;

                //if (this.Text.Length == 0)
                //    width = 20;
                //else if (this.Width.IsEmpty || this.Width == 0)
                //    width = this.Text.Length * 20;
                //else
                //    width = (int)this.Width.Value;
            }


            Bitmap bmp = new Bitmap(width, height);
            Graphics strOutput = Graphics.FromImage(bmp);

            strOutput.FillRectangle(new SolidBrush(backColor), new Rectangle(0, 0, width, height));

            string fontName;
            {
                if (string.IsNullOrEmpty(this.Font.Name))
                    fontName = "FlemishScript BT";
                else
                    fontName = this.Font.Name;
            }

            Font msgFont = new Font(fontName, fontsize, msgFontStyle);

            strOutput.DrawString(Message, msgFont, new SolidBrush(foreColor), 1, 1);
            MemoryStream memStream = new MemoryStream();
            bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);

            Context.Response.Clear();
            Context.Response.ContentType = "image/png";
                Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
               // memStream.WriteTo(Context.Response.OutputStream);\
                memStream.WriteTo(Context.Response.OutputStream);
                Context.Session.Remove(SessionText());
                Context.Response.OutputStream.Flush();
                Context.Response.OutputStream.Close();
                Context.Response.End();

        }

        private FontStyle SpecifyFontStyle()
        {
            FontStyle msgFontStyle = FontStyle.Regular;

            if (this.Font.Bold)
                msgFontStyle |= FontStyle.Bold;

            if (this.Font.Italic)
                msgFontStyle |= FontStyle.Italic;

            if (this.Font.Strikeout)
                msgFontStyle |= FontStyle.Strikeout;

            if (this.Font.Underline)
                msgFontStyle |= FontStyle.Underline;

            return msgFontStyle;
        }
    }
   
}

this gives an error this is not used in asp.net 2010 mvc
please help me
Posted
Updated 23-Sep-11 7:26am
v2
Comments
Marc A. Brown 23-Sep-11 14:41pm    
Can you cut down the code dump to just the relevant code? Also, can you specify where you're getting the error and the specifics of the error message? These things will help other people to help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900