CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2012
    Posts
    24

    Zoomed picture box image size.

    Hi folks. I have a problem with getting the size of an image within a picture box, I'll try to explain as clearly as possible.

    On a form I have a picture box, the picture box is anchored to all sides of the form and is set to zoom mode. It has to be zoom mode as I need to display very large images shrunk within the diemsions of the form.

    A dialog is used to select a jpg image, which is loaded into a bitmap, the picturebox image property is then set to the bitmap object. I need to do it this way so I can keep an original copy of the image selected in memory.

    The picture box correctly keeps its borders to the edge of the form and shrinks the largest dimension of the displayed image to keep the correct aspect ratio, the picture is displayed perfectly throughout form resizing.

    For the sake of argument, I want to get the percentage location of a mouse click within the picture displayed, where top left is 0,0 and bottom right is 100,100.

    The problem is I can't find a property within the picturebox that returns the actual size of the image displayed. I can either return the location compared to the actual size of the border, or the original image.

    Code:
    ooooooooooooooooooooooooooooooooooooooooooooooo
    o       xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      o
    o       x                              x      o
    o       x                              x      o
    o       x                              x      o
    o       x                              x      o
    o       x                              x      o
    o       x                              x      o
    o       x                              x      o
    o       xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      o
    ooooooooooooooooooooooooooooooooooooooooooooooo
    picturebox.width returns the value for the o's.
    picturebox.image.width returns the value of the original image.
    I can't find a way to return the x,y,w,h values for the x's

    Is there a way to do this easily, or am I going to have to calculate the picture height, width and placement on the form manually to get the correct values?

    Any help would be appreciated.

    Cheers.
    Last edited by GremlinSA; July 1st, 2013 at 01:54 AM. Reason: Added Code tags...

  2. #2
    Join Date
    Dec 2012
    Posts
    24

    Re: Zoomed picture box image size.

    Ah, my o's and x's didn't display right.

    Imagine the o's being a big rectangle, the x's being a smaller rectangle centered within.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Zoomed picture box image size.

    Use CODE TAGS to preserve formatting. You can EDIT your post to add it.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Zoomed picture box image size.

    You have two options here, and both of them are difficult...

    #1).. Set the PictureBox to Normal or Center Image, and do the Image re-sizing yourself (using the Form Resize event), Taking care of all the math here is a little complicated, but very doable...(See the Crop/Zoom thread NOTE: that is VB6 Code, but the math is the same)

    #2).. Just use the math and calculate the positions... Again the math is a little more complicated but also doable.. (see same thread as above for the math)..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Dec 2012
    Posts
    24

    Re: Zoomed picture box image size.

    Ah yes, I didn't think of that.
    Thanks.

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

Featured