0

In a rather simple ASP.NET application where the user can upload a image, mostly from his digital camera. I have to resize it to a workable size for the web and a thumbnail. What is here the best practice for? Is there a library that I in a simple way can implement without installing something on the web server.

2

5 Answers 5

1

This thread on SO will probably help you decide:

What is the best image manipulation library

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

Comments

1

Disclaimer: I am the author of this library.

However, it was designed exactly for this purpose, and is very mature and well-tested.

http://nathanaeljones.com/products/asp-net-image-resizer/

I think you will find the included sample application does exactly what you need.

Comments

0

Look at the BitMap class- you can do this quite easily by specifying the size in the constructor.

So imagine you wanted to half it:

Bitmap firstBitMap; // created somewhere else

// Create a new bitmap from the first, scaling down as we go
Bitmap halfSize = new Bitmap(firstBitMap, new Size(firstBitMap.Width/2, firstBitMap.Height/2));

If you want a higher quality solution, you need to consider the InterpolationMode enumeration.

For the simple scenario you describe you certainly don't need to bother with 3rd party libraries.

Comments

0

I am no image expert, but I implemented image resizing on a website and used something like this:

public static void ResizeImageHighQuality(string imgFilename, string imgResizedFilename, int resizeH, int resizeW)
{
    Image img = Image.FromFile(imgFilename);
    int h = 0, w = 0;

    if (img.Width > img.Height)
    {
        w = Convert.ToInt32(resizeW);
        h = Convert.ToInt32(w * Convert.ToDouble(img.Height) / Convert.ToDouble(img.Width));

    }
    else if (img.Height > img.Width)
    {
        h = Convert.ToInt32(resizeH);
        w = Convert.ToInt32(h * Convert.ToDouble(img.Width) / Convert.ToDouble(img.Height));
    }
    else
    {
        h = resizeH;
        w = resizeW;
    }

    Image thumbnail = new Bitmap(w, h);
    Graphics graphic = Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    graphic.DrawImage(img, 0, 0, w, h);

    ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
    EncoderParameters Params = new EncoderParameters(1);
    Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

    File.Delete(imgResizedFilename);
    FileStream fs = new FileStream(imgResizedFilename, FileMode.CreateNew);
    thumbnail.Save(fs, Info[1], Params);
    fs.Close();
}

Comments

0

Using Bitmap (and any other System.Drawing classes) is specifically prohibited in ASP.NET (see the warning near the top of the documentation page).

Using them may result in exceptions such as these:

TypeInitializationException: The type initializer for 'System.Windows.Media.Brush' threw an exception.
   at System.Windows.Media.SolidColorBrush..ctor(Color color)
   ...
'System.Windows.Media.Transform' threw an exception.
   at System.Windows.Media.Brush..cctor()Win32Exception: The operation completed successfully
   at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at MS.Win32.MessageOnlyHwndWrapper..ctor()
   at System.Windows.Threading.Dispatcher..ctor()
   at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
   at System.Windows.Freezable..ctor()
   at System.Windows.Media.MatrixTransform..ctor(Matrix matrix)
   at System.Windows.Media.Transform..cctor()

and

Win32Exception: The operation completed successfully
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at System.Windows.Media.MediaContextNotificationWindow..ctor(MediaContext ownerMediaContext)
   at System.Windows.Media.MediaContext..ctor(Dispatcher dispatcher)

Depending on what you are trying to do, Windows Imaging Component may fill your need. We have also had luck by creating a single STA thread and invoking all drawing operations over to that.

Comments

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.