Reputation: 33
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:(
Upvotes: 3
Views: 1949
Reputation: 17247
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 ;-)
Upvotes: 3
Reputation: 39277
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/
Upvotes: 0
Reputation: 900
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 a a 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:)
Upvotes: 2
Reputation: 8269
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.
Upvotes: 0