Results 1 to 8 of 8

Thread: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

  1. #1

    Thread Starter
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Lightbulb Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    I want an image to crop so that it will fill completely inside a rectangle when stretched.
    For example, in Windows 7 & when you set wallpaper position to "Fill" then it does same thing.

    I'm trying to add this feature in BackgroundImageLayout of a form or in SizeMode of a picturebox.
    So, if I just find the centered crop position and crop the image with correct aspect ratio, then the form/picturebox will show that image correctly if viewing in "stretched" mode.

    The solution seems to be easy, but unfortunately I couldn't think of any formula that will give me correct crop position of original image.

    Any idea ?
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    Both of these controls have exactly the same options as the Windows wallpaper except that Fill is called Zoom.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    Sorry, Zoom will not work.

    Zoom: If the image is not of same aspect ratio as the form/picturebox, there will be blank area.
    Stretch: Will ignore aspect ratio completely.

    What I want is a combination of both.
    1. First, find the aspect ratio (A) of the form/picturebox.
    2. Then, find an area in the bitmap (from center) where we can place the largest rectangle of aspect ratio A. (Here need help to get the largest rectangle)
    3. Crop that area of bitmap and place it in form/picturebox.
    4. Set Stretch property of form/picturebox to true. This will now show the image properly - without distortion.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    I'm not sure that that really makes any sense, particularly with respect to a picture box. There's a reason why MS never made the option available! In both cases it has to be simpler and more comprehensible for the user to maintain the aspect ratio in the dimensions of the control rather than mess about with the image in this way (or if it's critically important simply don't allow resizing at all!)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    The idea is to create a form - which, when resized, will not distort the background image or leave empty space.
    The background image will be chosen by user and they can vary in size.
    Hope I'm clear defining my problem.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    Hope I'm clear defining my problem.
    Yes. But if it's 'a problem', it's one entirely of your own making. It seems extremely unlikely that the user of your application will not be familiar with the restrictions of background imaging or that, in this knowledge, they will be choosing an image which is hopelessly unsuitable for the dimensions of the window. The last thing they will expect is to have your arbitrary slicing imposed on them. Apart from anything else, why do you assume that the interest in every image is focused on the centre? In fact it's very poor composition to have the focus slap bang in the middle as any half decent photographer will tell you.

    As a user, I am deeply annoyed by anything that purports to be a choice but turns out not to be. Instead of dictating format, how about you let the user decide for themselves what is the most pleasing way to display their image? The truth is that, unless the form is entirely bereft of controls (in which case what's the point of it?) the vast majority of users elect to have no background image when given the choice. Unless the image is specifically adapted to be 'interesting' in all the places where controls do not obscure it it's little more than distraction!

    There really isn't any difficulty in determining the largest rectangle possible as one of it's dimension will be equal to either the length or width of the original bitmap. Simply test the aspect ratio against the length and width and choose the one that fits. Centring the cropped dimension is equally easy. Divide the difference between the rectangle dimension and the bitmap dimension, divide by 2 and move the rectangle toward the centre by that distance. But just because you can, doesn't mean you should!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    Quote Originally Posted by dunfiddlin View Post
    Yes. But if it's 'a problem', it's one entirely of your own making. It seems extremely unlikely that the user of your application will not be familiar with the restrictions of background imaging or that, in this knowledge, they will be choosing an image which is hopelessly unsuitable for the dimensions of the window. The last thing they will expect is to have your arbitrary slicing imposed on them. Apart from anything else, why do you assume that the interest in every image is focused on the centre? In fact it's very poor composition to have the focus slap bang in the middle as any half decent photographer will tell you.

    As a user, I am deeply annoyed by anything that purports to be a choice but turns out not to be. Instead of dictating format, how about you let the user decide for themselves what is the most pleasing way to display their image? The truth is that, unless the form is entirely bereft of controls (in which case what's the point of it?) the vast majority of users elect to have no background image when given the choice. Unless the image is specifically adapted to be 'interesting' in all the places where controls do not obscure it it's little more than distraction!
    Yes, you are right. But in this case, the client wants this feature. Also, I think this could be a good option for a slideshow application.
    Quote Originally Posted by dunfiddlin View Post
    There really isn't any difficulty in determining the largest rectangle possible as one of it's dimension will be equal to either the length or width of the original bitmap. Simply test the aspect ratio against the length and width and choose the one that fits. Centring the cropped dimension is equally easy. Divide the difference between the rectangle dimension and the bitmap dimension, divide by 2 and move the rectangle toward the centre by that distance. But just because you can, doesn't mean you should!
    Great. That's what I needed.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Fill image inside a rectangle (crop from center - keep aspect ratio of container)

    Quote Originally Posted by dunfiddlin View Post
    But just because you can, doesn't mean you should!
    GoTo Haram shares your disgust at this perverse fill method. But you will have hard time stamping it out. The WPF infidels have cowed to popular demand and provided it as the UniformToFill mode. BB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width