tags:

views:

429

answers:

2

I have a C# function to REsize images.The parameters of the funciton would be orginal image path,new image path(path after saving the resizing of image). Now i want to build a logic for the below requirement.

1 . If the orginal file's width is greater than 480 or lessthan 450 ,then i want to set the width of output image as 450px.and the height has to be proportional of the width.

  1. If the width of the orginal file is > its height, then add white space to the top and to bottom (to the height) so that the new height will be 650.

I m using the follwing code to resize image

try
    {
        Size oldSize=new Size();
        Bitmap oldbmp1 = (Bitmap) Bitmap.FromFile(originalpath);
        oldSize.Width=oldbmp1.Width;
        oldSize.Height=oldbmp1.Height;

        Size newSize=new Size()

        using (Bitmap newbmp = new Bitmap(newsize.Width, newsize.Height), oldbmp = Bitmap.FromFile(originalpath) as Bitmap)
        {
            Graphics newgraphics = Graphics.FromImage(newbmp);
            newgraphics.InterpolationMode =  System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
           // newsize = GetNewImageSize(oldSize); // here i want the new Size
            newgraphics.Clear(Color.FromArgb(-1));
            newgraphics.FillRectangle(Brushes.White, 0, 0, newsize.Width, newsize.Height);

            newgraphics.DrawImage(oldbmp, 0, 0, newsize.Width, newsize.Height);

                try
                {
                   // newgraphics.Save();

                    ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
                    EncoderParameters Params = new EncoderParameters(1);
                    Params.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                    newbmp.Save(newpath, Info[1], Params);
                }
                catch (Exception ex1)
                {
                    throw ex1;
                }
                finally
                {
                    newbmp.Dispose();
                    oldbmp.Dispose();
                    oldbmp1.Dispose();
                }

        }
    }
    catch (Exception ee)
    {
        throw ee;
    }

Can any one advice what else i have to add on this ?

EDIT (Since comment field will not allow me to have more characters)

I tried this

float ratio = oldImg.Width / oldImg.Height;
SizeF newSize = new SizeF(desiredWidth, desiredWidth * ratio);

But it will not worked as i wish Ex : My orginal image had width 450 and height 1094. So ratio=450/1094 which is 0.4113; Desired height=desired width * ratio ie ; 450*0.4113 which is 185 .1

How its proportional . I think some thing has to be corrected.Any more thoughts ?

Thanks in advance

+4  A: 

What you need is a simple ratio between the width and height of the image. Basic arithmetics.

float ratio = oldImg.Width / oldImg.Height;
SizeF newSize = new SizeF(desiredWidth, desiredWidth / ratio);

With this the new size will be proportional to the old size.

Edited to correct a type and to clarify

I think there's a lacking of mathematics, so here it goes:

Let's say we have an image 100 (w) x 200 (h), with an ratio (w/h) of 0.5, and want to resize it to an width of 400. We simply divide the desired width by the ratio, thus giving us 800, the correct answer.

Now, if we wanted to resize the same image to an width of 50, dividing it by the ratio, would give us an height of 100.

Because of a typo on my previous version of the answer I used an * instead of an /, and that made my answer wrong.

Paulo Santos
A: 

In your example, you need to flip the fraction. Divide by your ratio instead of multiply. That will give you the original 1094.

GalacticCowboy