Question

I have a problem when i created PREVIEW HANDlER for a image . The problem is when i have photo on preview pane it dont resize itself maintaining the aspect ratio so i want to write a code so that the photo will resize itself maintaining the aspect ratio of the image. I also have to keep in mind that when i resize the image using my algorithim it should maintain the aspect ratio and the image should have possible maximum size in the preview pane (i mean there shouldn't be any other algorithim which could have the image preview of bigger size then the image obtained by our algorithim with maintained aspect ratio as well).

We have to keep in mind that height of the preview pane is constant factor so we have to deal with the changing width only(I mean according to the changing width we have to maintain the aspect ratio for both height and width of the image). Any help is developing algorthim is appreciated.

Was it helpful?

Solution

  • I'll clearly define the two ratios first.

    ImageRatio = WidthOfImage / HeightOfImage and

    WindowRatio = WidthOfPreviewPane / HeightOfPreviewPane.

  • Second, you may try your windows explorer again. When the WindowRatio is smaller than the ImageRatio and you drag the pane vertically, the height of the preview pane will also change.

    enter image description here

  • Finally, you may get ActualWidthOfImage and ActualHeightOfImage as below:

    if (WindowRatio > ImageRatio)
    {
        ActualHeightOfImage = HeightOfPreviewPane;
        ActualWidthOfImage = ActualHeightOfImage * ImageRatio;
    }
    else
    {
        ActualWidthOfImage = WidthOfPreviewPane;
        ActualHeightOfImage = ActualWidthOfImage / ImageRatio;
    }
    

Am I understand your problem correctly? I'd like to help you if you got further questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top