Click here to Skip to main content
Click here to Skip to main content

Scrollable and RatioStretch PictureBox

By , 26 Apr 2003
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey1 Mar '12 - 22:52 
nice
GeneralThank youmemberhuhuhujujuju9 Jan '10 - 11:10 
Thanks i've been looking all over for this stuff
Generalbest no nomemberkreont21 Dec '08 - 12:36 
doesnt work with LARGE image
(some dimension over 32767) !!!!!
 
qwqwq

Generallatest versionmemberalhambra-eidos1 Aug '08 - 1:21 
Mister, which is the latest version ? Can I download it ?
 
thanks.
 
AE

Questionhow to make Size long not intmembermaimosa6 Aug '06 - 3:04 
hi there,
first of all thanks for your control,but i need to know how can i make the size of pictureBox to be Long type than to be integer type.
thx in advance,
salam.
Questionmanipulate scrollbar value by codememberDaniel Junglen20 Jun '06 - 9:13 
Hi,
 
it is possible to set the scrollbar value at run-time?
 
greetings
Daniel

Generalgoodmemberjinzhecheng7 Apr '06 - 20:13 
Good stuff
Generalexcellent.. no flicker!memberTim_Mackey17 Aug '05 - 1:24 
hi,
this is a great control. there are other samples on the net that have flicker problems and the scroll bars are poorly done.
this is exactly what i wanted.
cheers.
GeneralRe: excellent.. no flicker!membergeahaad8 May '06 - 7:46 
100% acknowledge.
A wonderful control - makes my homework looking a lot nicer Smile | :)
 
thanks from austria
Generalscrollable and ratiostrech picture Box in Visual FoxPromemberBaba Kh.16 Apr '05 - 8:32 
Salut,
 
J'ai besoin de l'aide pour pouvoir scroller une jpg en Visual FoxPro.
Merci à l'avance
Baba Kh.
 
Baba Kh
GeneralMouseMovememberDerPumuckl5 Apr '05 - 3:22 
Hi,
 
can anyone tell me, why the MouseMove event does not work, if a picture is loaded?
GeneralRe: MouseMovememberachoi68 Apr '05 - 4:50 
I believe the events will not work when over the image, only on the space around the image. This is why I actually had to rewrite my own using a panels paint method instead, but for some reason, it seems to be very slow when dragging dialogs over the images.
GeneralRe: You can add an event.. :)memberNicejinux31 Mar '09 - 0:21 
Maybe you have to add an event on the PictureBox..
 
You can find PictureBox in the Viewer.cs [design]..
GeneralProblem adding DTC to toolbox...memberrcwatson10 Mar '05 - 6:19 
I tried building the design time control for PictureViewer to place it in my ToolBox so I could drag it onto a Windows CE project form, but got an error.   It's saying that it expects a different protection level on 'System.Windows.Forms.Control.ControlCollection.AddRange(System.Windows.Forms.Control[])' which doesn't make sense since all properties and methods are public for this up and down the object hierarchy.
 
Below is the command I ran and the error.
 
C:\Documents and Settings\Rob\My Documents\Visual Studio Projects\ratioStretchpi
ctureBox1>csc /noconfig /define:NETCFDESIGNTIME /target:library /out:design.View
er.dll Viewer.cs /r:"C:\Program Files\Microsoft Visual Studio .NET 2003\CompactF
rameworkSDK\v1.0.5000\Windows CE\Designer\System.CF.Design.dll" /r:"C:\Program F
iles\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\
Designer\System.CF.Windows.Forms.dll" /r:"C:\Program Files\Microsoft Visual Stud
io .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\Designer\System.CF.Drawing
.dll" /r:System.Windows.Forms.dll /r:System.Drawing.dll /r:System.dll /r:System.
XML.dll /r:System.Web.Services.dll /r:System.Data.dll /nowarn:1595
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
 
Viewer.cs(176,4): error CS0122:
            'System.Windows.Forms.Control.ControlCollection.AddRange(System.Windows.
            Forms.Control[])' is inaccessible due to its protection level
 
C:\Documents and Settings\Rob\My Documents\Visual Studio Projects\ratioStretchpi
ctureBox1>

GeneralEventsmemberachoi624 Feb '05 - 8:28 
I took the code and made it into a class library. Everything seemed fine except for the fact that events did not work when I clicked on the image in the component. Events will work find if you click around your image. Any suggestions on how to fix this? Is there a way where we can redirect the events so that maybe we can put a panel on top of the control so the events go to the panel instead of the user control?
GeneralWon't this be slowmemberMike Gore31 Mar '04 - 2:28 
I was wondering if this control is slower than it should be as it is a picturebox hosted inside a usercontrol.
 
The method I've used to create a similar control is to derive a control from ScrollableControl (for scrolling) and then use GDI+ to draw directly onto the surface of the control.
 
I belive that creating the control this way would lead to an increase in performance.
Questionproblems with using design vew and this control?memberdazinith29 Apr '03 - 5:14 
i notice that if i take your example and add anything in the design view (like adding another menu item for example) it will make it to where the control is no longer on the form and no longer works..
 
im having the same problem in my code using your control.. it works fine until i modify the form in the design window, then it changes the generated code and screws it all up.. is there any way to add this control properly such that the form can be edited after its added?
 
thanks!

 
still a newb.. cut me some slack :P
-dz
AnswerRe: problems with using design vew and this control?memberGil Klod29 Apr '03 - 6:34 
I am using visulStudio.net and in the desgin view I dont have any problem regarding this control.
 
if you want , you can send me your code to look at.
 
Cool | :cool:
 

GeneralRe: problems with using design vew and this control?memberdazinith29 Apr '03 - 8:33 
it may be how i added it to my program.. i didnt use the design view, but rather just added the code to the initializecomponents section manually.. it works fine until i go to the design view and modify the modify something (like adding another menu option).. then it removes my hand added section in the initializecomponents section.. is there a way to add the control to the toolbox so that i can add it to my form using the design view?
 
i can send you my code, but its just the source that is on here, all im doing is going to design view and adding another menu option then rerunning it..
 
thanks for the response, id really like to use this control, its just what i need.
 
still a newb.. cut me some slack :P
-dz
GeneralAdding custom controls to the toolboxmemberdrewnoakes28 Aug '03 - 2:02 
In my experience, VS.NET adds all UserControls in the solution to their own tab in the toolbox.
 
Alternatively, you can right-click the toolbox, select 'Add/Remove items...' and browse for your compiled assembly.
 
Dragging the control from the toolbox should help, though I've seen the problem you're explaining before -- even when added from the toolbox.
QuestionWhat about using a Panel?memberMarc Clifton27 Apr '03 - 10:32 
A panel will perform automatic scrolling when child controls exceed the panel's dimensions. Seems like an easier solution.
 
Marc
 
Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

AnswerRe: What about using a Panel?memberdazinith6 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?memberMarc Clifton6 May '03 - 13:23 
One of the things I keep forgetting is to make the controls inside the panel children of the panel, not the form. For example, Panel.Controls.Add(...). That's an off the cuff answer, let me know if that gets you anywhere.
 
Marc
 
Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

GeneralRe: What about using a Panel?membergabis6 Aug '03 - 0:49 
Make sure your pictureBox Dock is set to None
and the Location is set to 0,0.
The designer will make the pictureBox a child
of the panel automaticaly when you drag the
pictureBox into a panel (you'll have a visual feedback).
Set the panel Autoscroll to true and that's it.
GeneralRe: What about using a Panel?memberVertexwahn22 Aug '05 - 3:53 
i set anchor of my picture box to Top, Left and SizeMode to Normal
it works fine
thanks for that great tip!
QuestionRe: What about using a Panel?memberDr Reedo1 Feb '06 - 0:30 
I'm doing all of the above (setting Dock to None, Location to 0,0 , AutoScroll on the Panel to true, resetting Panel's width and height to that of the picture box, Anchor set to Top, Left, SizeMode to Normal, adding the picturebox as a child to the panel) and I still don't get scrollbars Frown | :(
 
What am I doing wrong?
 
Claire
 
-- modified at 6:31 Wednesday 1st February, 2006
AnswerRe: What about using a Panel?memberDr Reedo1 Feb '06 - 0:40 
ok, what I was doing wrong was resetting the panel's width and height, not the picturebox's. Once I did this:
 
this.pictureBox1.Width = this.pictureBox1.Image.Width;
this.pictureBox1.Height = this.pictureBox1.Image.Height;
 
all worked splendidly!!Smile | :)

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

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