views:

244

answers:

2

So my logic is flawed and I need a better and correct way to resize an image in my c# app

I need a function similar to this setup

public void ResizeImageForWeb(string OriginalFile, string NewFile, int MaxWidth, int MaxHeight, int Quality)
{
// Resize Code

}

Basically, I'm a web designer lost trying to programming a desktop app.

+2  A: 

A quick search from Google find this little snippet.

Rohan West
This is the snippet I use in this case. +1.
David Stratton
How can I make the file size smaller using that function?
Landmine
@Tyler, that snippet is using the same functions my code uses (Image.GetThumbnailImage, Image.Save) and is therefore going to have the same limitations.
Anthony Pegram
+5  A: 

This is code I've used to resize images that users upload to either create a thumbnail or simply to enforce a size restriction. It doesn't address picture quality, but it's a start.

// uses System.Drawing namespace
public class ImageResizer
{
    public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth)
    {
        return this.ResizeImage(fullFileName, maxHeight, maxWidth, fullFileName);
    }

    public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth, string newFileName)
    {
        using (Image originalImage = Image.FromFile(fullFileName))
        {
            int height = originalImage.Height;
            int width = originalImage.Width;
            int newHeight = maxHeight;
            int newWidth = maxWidth;

            if (height > maxHeight || width > maxWidth)
            {
                if (height > maxHeight)
                {
                    newHeight = maxHeight;
                    float temp = ((float)width / (float)height) * (float)maxHeight;
                    newWidth = Convert.ToInt32(temp);

                    height = newHeight;
                    width = newWidth;
                }

                if (width > maxWidth)
                {
                    newWidth = maxWidth;
                    float temp = ((float)height / (float)width) * (float)maxWidth;
                    newHeight = Convert.ToInt32(temp);
                }

                Image.GetThumbnailImageAbort abort = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                using (Image resizedImage = originalImage.GetThumbnailImage(newWidth, newHeight, abort, System.IntPtr.Zero))
                {
                    resizedImage.Save(newFileName);
                }

                return true;
            }
            else if (fullFileName != newFileName)
            {
                // no resizing necessary, but need to create new file 
                originalImage.Save(newFileName);
            }
        }

        return false;
    }

    private bool ThumbnailCallback()
    {
        return false;
    }
}
Anthony Pegram
It resizes a 1.8 MB image to 1MB when its only 800 pixels wide, any way to make the file size smaller?
Landmine
Perhaps look at other overloads of the Save method http://msdn.microsoft.com/en-us/library/8ex6sdew(v=VS.100).aspx http://msdn.microsoft.com/en-us/library/ytz20d80(v=VS.100).aspx
Anthony Pegram
The picture quality is terrible! The resize works nicely, but just cant use this.
Landmine