-7

Is there a quick method to determine if an image is a square or not in C#?

Question has been phrased incorrectly, my apologies.

Is there a way to determine if an image can be scaled "down" to fit into a square block, without cropping either height or width, for example, if I have 960x640, we have a square, on it's width, but if we have 640x960, we don't.

I need to be able to determine if an image can be scaled down 100% into a square block, for example, 150x150, without losing portions of the image.

Update

Let me try again.

I have to iterate a collection of images:

960x658 960x566 960x381 960x378 714x960 658x960

I know, that the first two images will be square (150x150), I know the middle two will be rectangular (horizontal) (300x150) and I know the remaining two will be rectangular (vertical) (150x300). Is there an algorithm, 3rd party component or built in method to determine this for me?

I don't want to go and code nested spaghetti code using if statements to do this? I'm so lost :$

JadedEric
  • 1,943
  • 2
  • 26
  • 49
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 18 '13 at 17:05
  • 7
    How could 960x640 be a square, but 940x600 not? – mbeckish Apr 18 '13 at 17:05
  • Is this what you are looking for? http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file – Odys Apr 18 '13 at 17:05
  • 11
    `if(width==height)` :) – I4V Apr 18 '13 at 17:06
  • 1
    How would you resize 960x640 to square? Wouldn't the only way be simply to check if the numbers are equal? Do you mean cropping? – David S. Apr 18 '13 at 17:10
  • Perhaps he means if the aspect ratio can be kept if resized to a square image? – paulm Apr 18 '13 at 17:20
  • 3
    @paulm - If the aspect ratio is not a square to begin with, how can you resize it to a square while also maintaining the original aspect ratio? – mbeckish Apr 18 '13 at 17:25
  • 2
    @JadedEric - When you say "an image is a square", do you mean 1) "the width and height of the image are equal", or 2) "determine if the image is a drawing of a square on a rectangular background"? – mbeckish Apr 18 '13 at 17:27
  • Your edit doesn't address the issue. How does a 960x640 image scale down to a square? – Servy Apr 18 '13 at 17:47
  • mbeckish, 2) is kind of what i mean. I have a collection of images that I need to place in a 150x150 block, only if they fit. these images are uploaded by clients and there is currently no way to ensure they upload the correct images. is this possible in c#. i'm no expert with post-media processing. thus my question, as stupid as it might be. – JadedEric Apr 18 '13 at 17:48
  • @servy, it does not, i used it as an example. it would be a lot easier if i can ask the question in my native language, unfortunately that's the best i can do. sorry. – JadedEric Apr 18 '13 at 17:49
  • @JadedEric You can resize any image to fit into a 150x150 square, while maintaining aspect ratio, so long as the width isn't more than 150 times larger than the height and the height isn't more than 150 larger than the width. Just divide 150 by the largest dimension to get the scaling ratio, and then multiply that value by both the height and width to get the new dimensions. – Servy Apr 18 '13 at 17:49
  • @JadedEric Well, you said it did in your question. – Servy Apr 18 '13 at 17:50
  • @Servy, see what you mean, apologies :$ – JadedEric Apr 18 '13 at 17:51
  • 1
    Question is closed, but you're looking for something like this: `Double scale = Math.Min(destinationSize.Width / sourceSize.Width, destinationSize.Height / sourceSize.Height); Size scaledSourceSize = new Size(sourceSize.Width * scale, sourceSize.Height * scale); // 150,100` – Fls'Zen Apr 18 '13 at 17:55

1 Answers1

4

Is there a quick method to determine if an image is a square or not in C#?

Well you're pretty much giving the answer yourself. You have the image. You have the image's properties.

if(img.Width == img.Height)
    //I'm a square

Now since you're iterating through a collection of images.

foreach(Image img in myImageCollection)
    if(img.Width == img.Height) 
      squareImages.Add(img);

As simple as that.

phadaphunk
  • 12,785
  • 15
  • 73
  • 107