Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Article

Scrollable and RatioStretch PictureBox

Rate me:
Please Sign up or sign in to vote.
4.69/5 (31 votes)
26 Apr 20031 min read 148.1K   3.8K   41   28
A scrollable and ratio-stretch picture box control

Image 1

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 :

C#
// 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:

C#
// 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


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionbest way to strech Pin
GPSol15-Aug-15 11:56
GPSol15-Aug-15 11:56 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey1-Mar-12 22:52
professionalManoj Kumar Choubey1-Mar-12 22:52 
GeneralThank you Pin
huhuhujujuju9-Jan-10 11:10
huhuhujujuju9-Jan-10 11:10 
Generalbest no no Pin
kreont21-Dec-08 12:36
kreont21-Dec-08 12:36 
Generallatest version Pin
kiquenet.com1-Aug-08 1:21
professionalkiquenet.com1-Aug-08 1:21 
Questionhow to make Size long not int Pin
maimosa6-Aug-06 3:04
maimosa6-Aug-06 3:04 
Questionmanipulate scrollbar value by code Pin
Daniel Junglen20-Jun-06 9:13
Daniel Junglen20-Jun-06 9:13 
Generalgood Pin
jinzhecheng7-Apr-06 20:13
jinzhecheng7-Apr-06 20:13 
Generalexcellent.. no flicker! Pin
Tim_Mackey17-Aug-05 1:24
Tim_Mackey17-Aug-05 1:24 
GeneralRe: excellent.. no flicker! Pin
geahaad8-May-06 7:46
geahaad8-May-06 7:46 
Generalscrollable and ratiostrech picture Box in Visual FoxPro Pin
Baba Kh.16-Apr-05 8:32
Baba Kh.16-Apr-05 8:32 
GeneralMouseMove Pin
DerPumuckl5-Apr-05 3:22
DerPumuckl5-Apr-05 3:22 
GeneralRe: MouseMove Pin
achoi68-Apr-05 4:50
achoi68-Apr-05 4:50 
GeneralRe: You can add an event.. :) Pin
Nicejinux31-Mar-09 0:21
Nicejinux31-Mar-09 0:21 
GeneralProblem adding DTC to toolbox... Pin
rcwatson10-Mar-05 6:19
rcwatson10-Mar-05 6:19 
GeneralEvents Pin
achoi624-Feb-05 8:28
achoi624-Feb-05 8:28 
GeneralWon't this be slow Pin
Mike Gore31-Mar-04 2:28
Mike Gore31-Mar-04 2:28 
Questionproblems with using design vew and this control? Pin
dazinith29-Apr-03 5:14
dazinith29-Apr-03 5:14 
AnswerRe: problems with using design vew and this control? Pin
Gil Klod29-Apr-03 6:34
Gil Klod29-Apr-03 6:34 
GeneralRe: problems with using design vew and this control? Pin
dazinith29-Apr-03 8:33
dazinith29-Apr-03 8:33 
GeneralAdding custom controls to the toolbox Pin
Drew Noakes28-Aug-03 2:02
Drew Noakes28-Aug-03 2:02 
QuestionWhat about using a Panel? Pin
Marc Clifton27-Apr-03 10:32
mvaMarc Clifton27-Apr-03 10:32 
AnswerRe: What about using a Panel? Pin
dazinith6-May-03 8:51
dazinith6-May-03 8:51 
do you have an example of this? im having problems with user controls.. i have tried putting a panel, then putting an image on top of it.. setting the panel to autoscroll.. then setting the panel's width and height to the width and height of the image.. unfortunately i still dont have scrollbars Frown | :(

still a newb.. cut me some slack :P
-dz
GeneralRe: What about using a Panel? Pin
Marc Clifton6-May-03 13:23
mvaMarc Clifton6-May-03 13:23 
GeneralRe: What about using a Panel? Pin
gabis6-Aug-03 0:49
gabis6-Aug-03 0:49 

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

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