1

I'm using ASP.NET Web Pages to create a form in which I can select an image. I want to then resize the image into various different sizes so that I can display them on my website.

This is working for smaller images (in filesize), but the images I want to resize are from my digital SLR and they can be as large as 14MB per jpeg. I got the following error...

Maximum request length exceeded.

I added a web.config with the following code:

<?xml version="1.0"?>

<configuration>

    <system.web>
        <compilation debug="false" targetFramework="4.0" />
        <httpRuntime maxRequestLength="20480" />  
    </system.web>

</configuration>

I no longer get the error, but it doesn't actually do anything. It still works with smaller images.

I've used the tutorial here: http://www.asp.net/web-pages/tutorials/files,-images,-and-media/9-working-with-images

My code is as follows:

@{  WebImage photo = null;
    var newFileName = "";
    var imagePath = "";
    var imageThumbPath  = "";

    if(IsPost){
        photo = WebImage.GetImageFromRequest();
        if(photo != null){
            newFileName = "Original_" + Path.GetFileName(photo.FileName);
            imagePath = @"images\" + newFileName;
            photo.Save(@"~\" + imagePath);

            newFileName = "Thumbnail_" + Path.GetFileName(photo.FileName);
            imagePath = @"images\" + newFileName;
            photo.Resize(width: 60, height: 60, preserveAspectRatio: true, preventEnlarge: true);
            photo.Save(@"~\" + imagePath);        
        }
    }
}

<!DOCTYPE html>

<html>
<head>
   <title>Resizing Image</title>
</head>

<body>

    <h1>Thumbnail Image</h1>
    <form action="" method="post" enctype="multipart/form-data">
        <fieldset>
            <legend> Creating Thumbnail Image </legend>

            <label for="Image">Image</label>
            <input type="file" name="Image" />
            <br/>
            <input type="submit" value="Submit" />
        </fieldset>
    </form>

</body>
</html>

Any ideas why it's not working for larger images. Any help appreciated!

1 Answer 1

10

Microsoft's WebImage class is really, really poor. After reading the source and spotting ~10 critical bugs in the first two or three pages, I gave up on it. It's not server-safe.

My imageresizing.net library is designed to run on the server, and manages memory much better. For SLR photos you will still need about 100-200MB of RAM to decompress a single image, but if you have that, it should get the job done quite reliably. It's been used successfully with gigapixel sized images, so your SLR will be easy as long as you have a teaspoon of RAM.

Here's an example of how to upload, resize, and save with the library. Using the uploaded file name is a really big vulnerability - a GUID is a much safer choice.

However, since the library is extremely fast, and is designed to support single-source imaging, you might consider just saving the original, and generating the thumbnails dynamically. The DiskCache plugin will cache them to disk as static files, served by IIS - it provides great performance.

Sign up to request clarification or add additional context in comments.

7 Comments

Very comprehensive answer. Thanks.
Thanks for sharing this. I was having a strange problem with the GetBytes() method inside WebImage. Switched over to this library and it worked straight out of the box.
Hi @Computer Linguist, I was wondering if you could shed some light on the critical bugs that you found, and why it's 'really really poor'? I'm just curious as to the details, so my reasoning for avoiding this isn't just "because some guy on stack said so"
Latest WebImage.cs source code. It causes a variety of image artifacts (black/gray border on many result images, poor image compositing quality, poor resizing quality, poor resulting file size. I documented most of these pitfalls in 2009, but it doesn't look like they took notice.
It looks like some of the memory leaks have been fixed since 2012 (with the notable exception of the result image (tempImage) leaking if there's any kind of exception). It's still quite inefficient in how it applies image transformations, but the codebase is no longer as scary (from a stability/security point of view) as it used to be. There's still no excuse for the resampling and border artifacts though, that's just basic.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.