Click here to Skip to main content
15,867,568 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

 
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 

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.