2

I have to display image in photo gallery @ width=200 height=180, but while uploading images I have to resize it , but the problem is every image have different resolution. How can I resize the images with different resolution so that images remain intact. Here is my code :

private void ResizeImage()
{
    System.Drawing.Image ImageToUpload = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    byte[] image = null;
    int h = ImageToUpload.Height;
    int w = ImageToUpload.Width;
    int r = int.Parse(ImageToUpload.VerticalResolution.ToString());
    int NewWidth = 200;//constant
    int NewHeight = 180;//constant
    byte[] imagesize = FileUpload1.FileBytes;
    System.Drawing.Bitmap BitMapImage = new System.Drawing.Bitmap(ImageToUpload, NewWidth, NewHeight);//this line gives horrible output
    MemoryStream Memory = new MemoryStream();
    BitMapImage.Save(Memory, System.Drawing.Imaging.ImageFormat.Jpeg);
    Memory.Position = 0;
    image = new byte[Memory.Length + 1];
    Memory.Read(image, 0, image.Length);
}

if resolution is 96 and if I set maxwidth=200 then its height would be 150 then only the image looks small and accurate. Can't we resize image in desired way so that it looks exact?

2 Answers 2

2

The function will resize the image maintaining aspect ratio.

public static Image Resize(Image originalImage, int w, int h)
{
    //Original Image attributes
    int originalWidth = originalImage.Width;
    int originalHeight = originalImage.Height;

    // Figure out the ratio
    double ratioX = (double)w / (double)originalWidth;
    double ratioY = (double)h / (double)originalHeight;
    // use whichever multiplier is smaller
    double ratio = ratioX < ratioY ? ratioX : ratioY;

    // now we can get the new height and width
    int newHeight = Convert.ToInt32(originalHeight * ratio);
    int newWidth = Convert.ToInt32(originalWidth * ratio);

    Image thumbnail = new Bitmap(newWidth, newHeight);
    Graphics graphic = Graphics.FromImage(thumbnail);

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

    graphic.Clear(Color.Transparent);
    graphic.DrawImage(originalImage, 0, 0, newWidth, newHeight);

    return thumbnail;
}

Usage

Image BitMapImage = Resize(ImageToUpload, NewWidth, NewHeight);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you once again, would you tell me how to cover the blank space? You might not understand my question. As I said I have fixed the height and width 200x150, your code is perfect, but here I will give an example if I upload a image having resolution=150 then the new width and height would be 105x150 that means there would 95px blank in either side of width. How can I fill that portion?
0

Here i keep height fixed to 180 to maintain aspect ratio. It will resize the image and save to disk. The return value is the percentage value which i use in 'background-size' css.

public float ResizePhoto(string filepath, string filename)
    {
        var path = Path.Combine(filepath, filename);
        var newPath = Path.Combine(filepath, "sml_" + filename);
        Image orgImage = Image.FromFile(path);

        float fixedHt = 180f;
        int destHeight, destWidth;
        float reqScale;


        if(orgImage.Height > fixedHt)
        {
            destHeight = (int)fixedHt;
            destWidth = (int)(fixedHt / orgImage.Height * orgImage.Width);
            reqScale = destWidth  / destHeight * 100;
        }
        else
        {
            destHeight = orgImage.Height;
            destWidth = orgImage.Width;
            reqScale = fixedHt / destHeight * 100;
        }

        Bitmap bmp = new Bitmap(destWidth, destHeight);
        bmp.SetResolution(orgImage.HorizontalResolution,orgImage.VerticalResolution);
        Graphics grPhoto = Graphics.FromImage(bmp);

        grPhoto.DrawImage(orgImage,
            new Rectangle(0, 0, destWidth,  destHeight),
            new Rectangle(0, 0, orgImage.Width, orgImage.Height),
            GraphicsUnit.Pixel);

        bmp.Save(newPath);

        return reqScale;
    }

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.