Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

c# - ASP.NET Web Pages - Using WebImage to resize and save a photo

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...