Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image resizing algorithm

Status
Not open for further replies.
Jun 9, 2006
159
US
Hello,

Can someone help me figure out a method of calculating the resize dimension of an image using an Image object in c#? This is what I have so far:

Code:
// Calculate the size and limit it to 
// a max of either 500px height or
// 500px width, whichever is greater
// then assign the latter dimenion its new
// size

Image img = new Image();
  
int h = img.Height;
int w = img.Width;


// set height
if (img.Height >= 500) { // img is bigger that 500px heigh
	imgDetail.Height = 500;
} else if (img.Width >= 500) { // img is bigger that 500px wide
// this doesn't work
	imgDetail.Width = 500;
}	

// calculate the amount to shink the height and width
w = Convert.ToInt32(Math.Round((Convert.ToDouble(h) /
	img.Height) * img.Width));

imgDetail.Width = w;

Thanks.

-- shawn

 
You have the width part in an else statement. Try removing it:

Code:
// Calculate the size and limit it to
// a max of either 500px height or
// 500px width, whichever is greater
// then assign the latter dimenion its new
// size

Image img = new Image();
  
int h = img.Height;
int w = img.Width;


// set height
if (img.Height >= 500) { // img is bigger that 500px heigh
    imgDetail.Height = 500;
} 
[b]if (img.Width >= 500) { // img is bigger that 500px wide
// this should work[/b]
    imgDetail.Width = 500;
}    

// calculate the amount to shink the height and width
w = Convert.ToInt32(Math.Round((Convert.ToDouble(h) /
    img.Height) * img.Width));

imgDetail.Width = w;
 
But my problem is, what if the original size is 600x750. This method below only checks both size regardless of which one is bigger. unfortutanly the Image object needs both height and width dimensions. I guess I need a way to to tell which one is bigger and then perfomr some calculation on the other dimension?

I hope that makes sense


Code:
// Calculate the size and limit it to
// a max of either 500px height or
// 500px width, whichever is greater
// then assign the latter dimenion its new
// size

Image img = new Image();
  
int h = img.Height;
int w = img.Width;


// set height
if (img.Height >= 500) // img is bigger that 500px heigh
    h = 500;

if (img.Width >= 500) // img is bigger that 500px wide
    w = 500;  

// calculate the amount to shink the height and width
w = Convert.ToInt32(Math.Round((Convert.ToDouble(h) /
    img.Height) * img.Width));

imgDetail.Width = w;


 
I'm not sure I am following you. This validate my understanding you want to:

1) Check image height
2) Check image width
3) Determine which value is greater, image height or image width
4) Resize corresponding dimension on greater of two

Is this correct?
 
Yes thats correct.

I want to be able to define a max Width and Height, then resize the image based on its original size.

So;

1) I want a 500px wide image, but only if its original width is above 500. If its below keep the current width.

2) If the image is lower than 500 width but above that in heigth then I want to just adjust the height, but keep the width in perspectiv ratio

3) perform the same check on the opposiste dimension

4) be able to change the max H and W values.

 
Barring doing the conversions you need to do for the int values, what do you think of this?

Code:
        Image img = new Image();
        const int MAX_VALUE = 500;   
        int h = img.Height;
        int w = img.Width;
        int newH = 0;
        int newW = 0;

        if (h > MAX_VALUE)
        {
            if (w > MAX_VALUE && w > h)
            {
                newW = MAX_VALUE;
                newH = (newW * h) / w;

            }
            else
            {
                newH = MAX_VALUE;
                newW = (newH * w) / h;


            }
        }
        else if (h < MAX_VALUE)
        {
            if (w < MAX_VALUE)
            {
                newH = h;
                newW = w;

            }
            else
            {
                newW = MAX_VALUE;
                newH = (newW * h) / w;

            }



        }
 
This is untested, and I'm more of a vb guy, but this should give you the idea:

Code:
// Calculate the size and limit it to
// a max of either 500px height or
// 500px width, whichever is greater
// then assign the latter dimenion its new
// size

Image img = new Image();
  
int h = img.Height;
int w = img.Width;


// set height
if (img.Height >= 500) // img is bigger that 500px heigh
    int hDiff = h - 500; // figure out difference in old and new height
    double percent = hDiff / h; //Figure what percent that # represents
    h = 500;
    hDiff = w * hDiff; //figure out what to take off width from that percent
    w = w - diff; // change width by that amount

//do the same with width
if (img.Width >= 500) // img is bigger that 500px wide
    int wDiff = w - 500;
    double percent = wDiff / w;
    w = 500;
    wDiff = h * hDiff;
    h = h - diff ;

// calculate the amount to shink the height and width
w = Convert.ToInt32(Math.Round((Convert.ToDouble(h) /
    img.Height) * img.Width));

imgDetail.Width = w;

You could also separate that into it's own sub. That way you don't have to repeat you logic, you can just pass params to the sub and call it twice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top