Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7075337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:09:55+00:00 2026-05-28T06:09:55+00:00

I am using asp.net mvc3. I am making a bitmap using text by system.drawing.

  • 0

I am using asp.net mvc3. I am making a bitmap using text by system.drawing. I want
to send that image from my controller to my view in VIEWDATA, but in my view I cannot parse VIEWDATA properly.

This is the controller code:

 public ActionResult About( string agha)
        {
            agha = "asgdjhagsdjgajdga";
             Color BackColor = Color.White;
            String FontName = "Times New Roman";
            int FontSize = 25;
            int Height = 50;
            int Width = 700;

            Bitmap bitmap = new Bitmap(Width, Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            Color color = Color.Gray; ;
            Font font = new Font(FontName, FontSize);         

            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);               

            graphics.DrawString(agha,font,Brushes.Red, 0, 0);
            ViewData["picture"] = bitmap;            

            return View( );
        }

The view calling the viewdata looks like this

 <img src="@ViewData["picture"]." />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:09:56+00:00Added an answer on May 28, 2026 at 6:09 am

    I’d recommend you writing a custom action result to avoid polluting your controller action with completely useless and boring GDI+ infrastructure code:

    public class ImageResult : ActionResult
    {
        private readonly string _agha;
        public ImageResult(string agha)
        {
            _agha = agha;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            Color BackColor = Color.White;
            String FontName = "Times New Roman";
            int FontSize = 25;
            int Height = 50;
            int Width = 700;
    
            using (Bitmap bitmap = new Bitmap(Width, Height))
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                Color color = Color.Gray;
                Font font = new Font(FontName, FontSize);
    
                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);
    
                graphics.DrawString(_agha, font, Brushes.Red, 0, 0);
    
                context.HttpContext.Response.ContentType = "image/jpg";
                bitmap.Save(context.HttpContext.Response.OutputStream, ImageFormat.Jpeg);
            }
        }
    }
    

    and then simply define a controller action that will return this custom action result:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult Image(string agha)
        {
            return new ImageResult(agha);
        }
    }
    

    and from within some view (~/Views/Home/Index.cshtml in my example) you could reference the action that will dynamically generate the image for you:

    <img src="@Url.Action("Image", new { agha = "foo bar" })" alt="" />
    

    Another possibility if you don’t want to create an additional action to build the image is to use the Data URI scheme which basically allows you to embed the image as Base64 data directly into the HTML. One caveat with this is that not all browsers support it and ni addition to that it makes your HTML pages much larger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using ASP.net MVC3 with C# I want to create a partial view that uses
i'm using asp.net mvc3 with jquery datepicker widget. i want the user to click
I am writing a simple proof-of-concept web service using ASP.NET MVC3 controller with JSON
I'm using ASP.NET MVC3 with Razor and C#. I am making a form builder
I'm using ASP.NET MVC3 with razor engine.I want to apply css class on body
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using ASP.net, how do you write ANSI characters to a text file? I need
Using ASP.NET MVC when trying to get the information stored on my Session[objectName] from
Using ASP.NET MVC 2.0, I have an actionlink that is used for comments on
using ASP.NET MVC 1.0 and I have a action that returns a JsonResult and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.