Rashim's Blog

Re-sizing Image

Posted on: January 10, 2012

We always need to work with images to display it on the landscape or portrait mode. To do that we need to calculate the image size also need to generate the image dimension. This is very easy stuff though we are fighting a lot!!!

private Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
    {
        double multiplier = 0;
        string layout;
        if (currH > currW)
        {
            layout = "portrait";
        }
        else
        {
            layout = "landscape";
        }

        switch (layout.ToLower())
        {
            case "portrait":
                if (destH > destW)
                {
                    multiplier = (double)destW / (double)currW;
                }
                else
                {
                    multiplier = (double)destH / (double)currH;
                }
                break;
            case "landscape":
                if (destH > destW)
                {
                    multiplier = (double)destW / (double)currW;
                }
                else
                {
                    multiplier = (double)destH / (double)currH;
                }

                break;
        }

        return new Size((int)(currW * multiplier), (int)(currH * multiplier));
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: