问题
I am learning GDI+ and I am trying to make a display window with scroll bars (so I can only see part of the image at a time and I can scroll around it). I have read through the basics of GDI+ from several books but I have not found any good tutorials online or in books available to me about doing more advanced things like this.
Any recommendations on guides or example code on how do do this?
Here is what I have so far
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (Label != null)
{
using (Bitmap drawnLabel = new Bitmap(Label.LabelHeight, Label.LableLength, System.Drawing.Imaging.PixelFormat.Format1bppIndexed))
using (Graphics drawBuffer = Graphics.FromImage(drawnLabel))
{
drawBuffer.ScaleTransform(_ImageScaleFactor, _ImageScaleFactor);
foreach (Epl2.IDrawableCommand cmd in Label.Collection)
{
cmd.Paint(drawBuffer);
}
drawBuffer.ResetTransform();
}
}
}
I would like to paint this in to a PictureBox I have on the control and control what is shown by a VScrollBar and HScrollBar but I don't know how to do that step.
P.S. Label is a custom class that I have in my namespace, it is a object that represents a label you would print from a label printer.
回答1:
What you need to do is:
- Host a Panel control on a Form (or in a UserControl for reuse)
- Set the Panel AutoScroll property to True
- Make a PictureBox control a child of the Panel
- Resize the PictureBox control to the size of the image it contains at runtime
The Panel control will show the vertical and horizontal scroll bars as needed giving you exactly the functionality you're looking for.
To do your own zooming you might actually forgo the PictureBox control. Follow the above steps but instead of a PictureBox, host another Panel inside the parent panel, size it as you need and handle its Paint event for the zooming.
来源:https://stackoverflow.com/questions/2655353/creating-a-image-viewer-window-controll