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 7040615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:56:22+00:00 2026-05-28T01:56:22+00:00

public ActionResult CustomChart(int reportID) { Chart chart = new Chart(); // Save the chart

  • 0
public ActionResult CustomChart(int reportID)
{
    Chart chart = new Chart();

    // Save the chart to a MemoryStream
    var imgStream = new MemoryStream();
    chart.SaveImage(imgStream);
    imgStream.Seek(0, SeekOrigin.Begin);

    // Return the contents of the Stream to the client
    return File(imgStream, "image/png");
}

I am accustomed to using the ‘using’ statement in conjuction with MemoryStreams. Is this a scenario where the ‘using’ statement is not necessary? Or is it valid to call return inside of a ‘using’ statement?

EDIT:

For my purposes I have found that the introduction of a ‘using’ statement does NOT work (throws an ObjectDisposedException). Here’s what I’m doing with it client-side:

$('#ReportTest').bind('load', function () {
                        $('#LoadingPanel').hide();
                        $(this).unbind('load');
                    }).bind('error', function () {
                        $('#LoadingPanel').hide();
                        $(this).unbind('error');
                    }).attr('src', '../../Chart/CustomChart?ReportID=' + settings.id);
  • 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-28T01:56:22+00:00Added an answer on May 28, 2026 at 1:56 am

    Does a MemoryStream get disposed of automatically when returning it as an ActionResult?

    Yes, MVC (at least version 3) will clean it up for you. You can take a look at the source of the WriteFile method in FileStreamResult:

    protected override void WriteFile(HttpResponseBase response) {
        // grab chunks of data and write to the output stream
        Stream outputStream = response.OutputStream;
        using (FileStream) {
            byte[] buffer = new byte[_bufferSize];
    
            while (true) {
                int bytesRead = FileStream.Read(buffer, 0, _bufferSize);
                if (bytesRead == 0) {
                    // no more data
                    break;
                }
    
                outputStream.Write(buffer, 0, bytesRead);
            }
        }
    }
    

    The line using (FileStream) { will place the Stream in a using block, thus Disposing of it when it has written the contents to the Http Response.

    You can also verify this behavior by creating a dummy stream that does this:

    public class DummyStream : MemoryStream
    {
        protected override void Dispose(bool disposing)
        {
            Trace.WriteLine("Do I get disposed?");
            base.Dispose(disposing);
        }
    }
    

    So MVC will dispose it.

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

Sidebar

Related Questions

public ActionResult GeneratePdf(int id) { var labelRepository = new LabelRepository(); var label = labelRepository.GetLabel(id);
Here is my code: public ActionResult DeleteItem(int id) { using (var cont = new
[AcceptVerbs(HttpVerbs.Get)] public ActionResult Delete(int id) { CustomerDataContext cs = new CustomerDataContext(); var query =
public ActionResult ReadXMLDevices(int groupID) { var query = from k in XMLEntities.unassigneditems where k.DevOrAcc
Here is my code: public ActionResult MainMenu(int id) { using (WebDataContext context = new
public ActionResult myItems() { var dataContext = new RecordsDataContext(); MembershipUser myObject = Membership.GetUser(); string
public ActionResult Index(){ var dataContext = new DataEvidencijaDataContext(); MembershipUser myObject = Membership.GetUser(); string KorisnickoIme
public ActionResult DeleteCategory(int id) { CategoryManager manager = new CategoryManager(); manager.DeleteCategory(id); TempData[IsDeleted] = true;
Controller public ActionResult GetCategories() { var htmlText = new StringBuilder(); var scriptText = new
public ActionResult DownloadMembers() { MembershipUserCollection users = Membership.GetAllUsers(); var grid = new WebControls.GridView(); grid.DataSource

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.