简体   繁体   中英

Proper way for dynamically creating images in ASP.NET C# web application?

I'm fairly new to ASP.NET but I have developed quite a few WinForms apps in C# where I've used the System.Drawing.Bitmap namespace extensively without much issues.

Today, I decided to write some code to dynamically create some PNGs on the fly in my Page_Load event and everything seems to work fine. But I notice this scary looking warning on the microsoft documentaion site. What is up with that ??

I am unaware of any other ways to deal with images in .Net except using System.Drawing.Bitmap ... I am baffled:(

You described dynamically generating images in your Page_Load event which suggests that your images are being served by an ASP.NET page. If you are not serving pages, then there is no need to incur the performance overhead of instantiating the page object and it's associated objects, lifecycle events etc.

A better 'or more proper' way of generating your images would be to serve them from within a lighter HttpHandler (but you can still use Bitmap just fine). To implement the IHttpHandler interface you only have to implement 2 methods and there's not much to it. Something like this:

<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.IO;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {   
        Bitmap output = new Bitmap(path);

        // Do lots of fun stuff here related to image
        // manipulation and/or generation

        ...

        context.Response.ContentType = ResponseType("Image/png");

        // Send image to the browser
        image.Save(context.Response.OutputStream, ImageType(path));

        // Be careful to clean up; you could put this inside a 'using' block    
        image.Dispose();
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

Please note: This is not working code ... just a little example to give you an idea.

Put your code in an HttpHandler and you'll be 50% closer to awesome. At any rate, it will certainly serve your images faster and allow you to handle more requests... plus you get to learn more of the framework ;-)

The reason for this warning is that the windows service control manager (SCM) creates an invisiable windows station (which should not have UI elements as per MS design) for each service (like ASP.NET) and you can get errors like this which results in a crash as the GDI+ does make frequesnt calls to the windows kernel dlls. But as you can see Microsoft, yet again, are being incosiderate a***oles in their role of service providers to bother explaining a little more or at least put link somewhere = people are worring and get white hairs.

I had bookmarked aa very nice reserach article on this but I can't find it right now sorry.

Either way I've never experienced no big problems in ASP.NET using that namespace so don't worry it will be all fine as long as you dispose of the Bitmaps exlicitly or in a using() {} statement. You got my 99.9% guarantee (0.1 percent I reserve for a any possiable court appearances I may need to attend:)

I would assume it's there for legal reasons as a "use at your own risk, and don't blame our library for downtime of your web servers."
But I've used it in such environments before with no problems myself.

GDI+ works for the most part under ASP.NET despite the warning in the documentation but under heavy load (100,000+ images) you may encounter problems including exceptions being thrown for no apparent reason and when you retry the same operation it works just fine.

The only work around in this case was to run separate render processes that recycled frequently, communicating with ASP.NET using MSMQ. For lower traffic sites I doubt you'll see any issues.

You do need to dispose of your bitmaps properly and you do need to avoid using Image.FromFile() which has issues. See http://blog.abodit.com/2010/07/gdi-image-fromfile-problem/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM