Posts tagged error

GDI+ Image.FromFile has a problem – here’s how to fix it

In GDI+ you can call Image.FromFile to load an image from a file. BUT there are several issues with this call, the biggest being that GDI+ will keep the file open long after you are done with it. Here is an image loader that gets around this issue.

If you are running a high volume web site, and your images are on a SAN you’ll find this technique necessary to prevent an eventual exhaustion of filehandles.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Data;

namespace Utility
{
public static class ImageLoader
{
// This isn’t going to help much – you’ll run out of memory anyway on very large images – but if you are keeping several in memory it might …
public const int MaximumImageDimension = 10000;

///
/// Method to safely load an image from a file without leaving the file open,
/// also gets the size down to a manageable size in the case of HUGE images
///
/// An Image – don’t forget to dispose of it later
public static Image LoadImage (string filePath)
{
try
{
FileInfo fi = new FileInfo(filePath);

if (!fi.Exists) throw new FileNotFoundException(“Cannot find image”);
if (fi.Length == 0) throw new FileNotFoundException(“Zero length image file “);

// Image.FromFile is known to leave files open, so we use a stream instead to read it
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (!fs.CanRead) throw new FileLoadException (“Cannot read file stream”);

if (fs.Length == 0) throw new FileLoadException(“File stream zero length”);

using (Image original = Image.FromStream(fs))
{
// Make a copy of the file in memory, then release the one GDI+ gave us
// thus ensuring that all file handles are closed properly (which GDI+ doesn’t do for us in a timely fashion)
int width = original.Width;
int height = original.Height;
if (width == 0) throw new DataException(“Bad image dimension width=0″);
if (height == 0) throw new DataException(“Bad image dimension height=0″);

// Now shrink it to Max size to control memory consumption
if (width > MaximumImageDimension)
{
height = height * MaximumImageDimension / width;
width = MaximumImageDimension;
}
if (height > MaximumImageDimension)
{
width = width * MaximumImageDimension / height;
height = MaximumImageDimension;
}

Bitmap copy = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(copy))
{
graphics.DrawImage(original, 0, 0, copy.Width, copy.Height);
}
return copy;
}
}
}
catch (Exception ex)
{
ex.Data.Add(“FileName”, filePath);
throw;
}
}
}

The EntityContainer name could not be determined

If you encounter this error, make sure your constructor is calling the base constructor for an ObjectContext and passing the EntityContainer name.

System.ArgumentException: The EntityContainer name could not be determined. The provided EntitySet name must be qualified by the EntityContainer name, such as'EntityContainerName.EntitySetName', or the DefaultContainerName property mustbe set for the ObjectContext.

WMPnetwk.exe started using 50% of my CPU

I’m not even using Windows Media Player on Windows 7 and I certainly haven’t turned on any network sharing features so why is this service using up so much CPU?

Now disabled thanks to advice on the CNet forums here.

Some witty comments there too like “3. Uninstall Windows Media Player 11…I mean if after 11 versions it still doesn’t work right then its probably not gonna is it!”

Javascript error reporting

It’s all very well to be capturing all the errors that happen server side, logging them to a database and reporting on them like any robust web service ought to, what about the client?

So today I added reporting for client-side errors to the project I’m working on. Turned out to be remarkably easy. A couple of lines of JQuery code are all that’s needed on the client to capture the error and report it back to a web service:-

window.onerror = errorHandler;
// Report javascript errors back to web site
function errorHandler(msg, url, lno) {
try
{
jQuery.ajax({
type: “POST”,
url: ‘/services/error/clientError.ashx’,
processData: true,
contentType: “application/text”,
timeout: 10000,
dataType: “json”,
data: { msg: msg, url: url, lno: lno },
success: function(responseData) {},
error: function() {},
complete: function() {}
});
}
catch (err) {
// do nothing
}
}

And on the server side the clientError.ashx can just grab the body of the post, split it back up and report on the error into the normal server side database of all exceptions.

In under an hour I added reporting for all client side errors and armed with that we’ll be able to find and fix those annoying browser (IE?) issues some of our customers see from time to time.