Click here to Skip to main content
Licence 
First Posted 26 Apr 2003
Views 107,518
Downloads 2,471
Bookmarked 36 times

Scrollable and RatioStretch PictureBox

By | 26 Apr 2003 | Article
A scrollable and ratio-stretch picture box control

Introduction

The Visual Studio.NET PictureBox control supports four behaviors of SizeMode property: Normal, StretchImage, AutoSize and CenterImage.

When I needed to use the PictureBox control it lacked two behaviors :

  1. Scrollable - The ability to use the scroll bars in order to view different areas of large image.
  2. RatioStretch - The ability to view stretched image in the same ratio (width / height) of the original image and avoids distortion of the stretched image.

The Viewer control which is available with this article supports the above two behaviors.

Note : This control supports scrolling using the mouse wheel.

Using the code

In order to use the Scrollable and RatioStretch capabilities of the Viewer control, simply add the Viewer control to your project and use it as follows :

    // open an image
    this.viewer1.Image = Image.FromFile(ImageFilepath);
    // set mode as Scrollable
    this.viewer1.ImageSizeMode = SizeMode.Scrollable;

In the sample above, we first load the image from the file specified by ImageFilepath, to the Viewer control. We then set its ImageSizeMode property to Scrollable, so that when the size of the image is greater than the visible area of the Viewer control, scrollbar(s) are added. Now, if we want the image to be displayed to fit within the Viewer control, without affecting the aspect ratio, we set the ImageSizeMode property of the Viewer control to RatioStretch as shown below:

    // set mode as RatioStretch
    this.viewer1.ImageSizeMode = SizeMode.RatioStretch;

That's all.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gil Klod



Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:52 1 Mar '12  
GeneralThank you Pinmemberhuhuhujujuju11:10 9 Jan '10  
Generalbest no no Pinmemberkreont12:36 21 Dec '08  
Generallatest version Pinmemberalhambra-eidos1:21 1 Aug '08  
Questionhow to make Size long not int Pinmembermaimosa3:04 6 Aug '06  
Questionmanipulate scrollbar value by code PinmemberDaniel Junglen9:13 20 Jun '06  
Generalgood Pinmemberjinzhecheng20:13 7 Apr '06  
Generalexcellent.. no flicker! PinmemberTim_Mackey1:24 17 Aug '05  
GeneralRe: excellent.. no flicker! Pinmembergeahaad7:46 8 May '06  
Generalscrollable and ratiostrech picture Box in Visual FoxPro PinmemberBaba Kh.8:32 16 Apr '05  
GeneralMouseMove PinmemberDerPumuckl3:22 5 Apr '05  
GeneralRe: MouseMove Pinmemberachoi64:50 8 Apr '05  
GeneralRe: You can add an event.. :) PinmemberNicejinux0:21 31 Mar '09  
GeneralProblem adding DTC to toolbox... Pinmemberrcwatson6:19 10 Mar '05  
GeneralEvents Pinmemberachoi68:28 24 Feb '05  
GeneralWon't this be slow PinmemberMike Gore2:28 31 Mar '04  
Questionproblems with using design vew and this control? Pinmemberdazinith5:14 29 Apr '03  
AnswerRe: problems with using design vew and this control? PinmemberGil Klod6:34 29 Apr '03  
GeneralRe: problems with using design vew and this control? Pinmemberdazinith8:33 29 Apr '03  
GeneralAdding custom controls to the toolbox Pinmemberdrewnoakes2:02 28 Aug '03  
QuestionWhat about using a Panel? PinmemberMarc Clifton10:32 27 Apr '03  
AnswerRe: What about using a Panel? Pinmemberdazinith8:51 6 May '03  
GeneralRe: What about using a Panel? PinmemberMarc Clifton13:23 6 May '03  
GeneralRe: What about using a Panel? Pinmembergabis0:49 6 Aug '03  
GeneralRe: What about using a Panel? PinmemberVertexwahn3:53 22 Aug '05  
i set anchor of my picture box to Top, Left and SizeMode to Normal
it works fine
thanks for that great tip!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 27 Apr 2003
Article Copyright 2003 by Gil Klod
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid