Image Viewer application

M

moss

Hi, I'm creating a windows forms Image Viewer application in C# for my
college project and I have problems with display of large images. By
"large" I mean the images that are bigger than the application window
(it's a SDI application). The layout of the form is like this: on the
top there is a MenuStrip, and right below a ToolStrip control, on the
bottom I have a StatusStrip, and in the space between the ToolStrip
and StatusStrip I draw my images. I added a Paint event handler for
the form and I use the e.Graphics property of the PaintEventArgs to
draw the image directly to the form's client area. But I'm starting to
think this wasn't a good idea because if I have to display images that
are bigger than the size of the form (640x480), I have to enable the
AutoScroll, but the scrollable area is the form's whole client area,
not just the space between the ToolStrip and StatusStrip where I
display my images, so the scrollbars don't appear where I want them to
appear. Maybe I should use a panel or pictureBox and set it's Dock =
Fill property to fill the space between the two strips, enable the
AutoScroll for the control, and then add a Paint event handler for the
panel or pictureBox and draw the image there? What do you think would
be the best solution?
 
N

Niko Rosvall

Why don't you just use PictureBox control and set image with
MyPicBox.Image = MyImage; ?

Niko
 
P

Peter Duniho

[...] Maybe I should use a panel or pictureBox and set it's Dock =
Fill property to fill the space between the two strips, enable the
AutoScroll for the control, and then add a Paint event handler for the
panel or pictureBox and draw the image there? What do you think would
be the best solution?

Put a PictureBox inside a Panel (no dock/fill, let it auto-resize for the
picture it's showing), set the Panel to auto-scroll. That should do it.
 
M

moss

Why don't you just use PictureBox control and set image with
MyPicBox.Image = MyImage; ?

Yeah, I'll do that. But I also want to do some editing on the picture, so
I'll use Graphics g = Graphics.FromImage(MyPicBox.Image); and then
Invalidate() the PictureBox after I've made the changes.
 
M

moss

Put a PictureBox inside a Panel (no dock/fill, let it auto-resize for the
picture it's showing), set the Panel to auto-scroll. That should do it.

Yeah, but I want to center the image inside the window and also be able to
zoom in and out, so I would have to dock/fill the panel, and center the
pictureBox that's holding the image. Please excuse me if I'm wrong, I'm a
beginner at this :)
 
P

Peter Duniho

Yeah, but I want to center the image inside the window and also be able
to
zoom in and out, so I would have to dock/fill the panel, and center the
pictureBox that's holding the image.

Centering is easy. Just set the scroll position appropriately.

Zooming is harder. But one possible approach is to nest your PictureBox
inside _two_ Panels. That is, put it inside a Panel with "Fill" dock
setting, set the PictureBox size mode to "Stretch" so that it simply
scales the image to the control's size, and then put the Panel containing
the PictureBox inside another Panel with scrolling enabled.

You'll control scaling with the inner Panel, by adjusting its size, and
you'll control panning (scrolling) with the outer Panel, by adjusting the
scroll bar positions.

None of this takes into account your apparent desire to do some editing of
the picture. You're not specific about what kind of editing would be
done; as long as it's really simple stuff, the above should still be
compatible. But, the more complicated your user interaction starts to
become, the more compelling you may find to simply create a custom control
class that handles everything (zoom, panning/scrolling, editing, whatever
else).

Pete
 
J

Jeff Johnson

Hi, I'm creating a windows forms Image Viewer application in C# for my
college project

Well, I was about to give you a URL to a ready-made bit of code that does
this, but then I re-read this part and since the whole point is for you to
do something yourself, I'm not going to.

As far as zooming goes, I recommend drawing the image yourself. Use
transformations to increase the zoom factor and math to determine how much
of the image (and which part) to draw based on the current scroll location
and zoom.
 
M

moss

Well, I was about to give you a URL to a ready-made bit of code that does
this, but then I re-read this part and since the whole point is for you to
do something yourself, I'm not going to.

At this point, I don't think that I'm experienced enough in programming,
and my project has to be finished in a week, so I would vey much appreciate
any ready-made bits of code. It takes too much time for me to figure
everything out by myself, and this is kind of urgent.
I have to study the code, understand it, and explain it in the project
documentation anyway.
I've read the articles on bobpowell.net regarding GDI+ transformations,
image scaling and zooming techniques, but the more I read the more I'm
confused. I have to see this in a real-world application in order to
understand it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top