1

我正在使用面板在 Windows 窗体中显示图像我在 Panel_Paint 事件中的面板中绘制图像,如下所示:

Graphics g = panel1.CreateGraphics();
Image im = new Bitmap(@"../../Data/#3_Page2.PNG");
g.DrawImage(im,new Point(10,10));

现在,图像按我的预期绘制,图像底部的某些部分没有显示,因为它的高度大于表格高度。我现在已经添加了 VScrollBar。我如何使该面板在 VScrollBar 的帮助下查看图像的其余部分。

4

2 回答 2

5

您可以使用 PictureBox 并将 SizeMode 设置为 AutoSize 和 Panel 的 AutoScroll 属性。这样,如果需要,面板应该添加滚动条。

PictureBox pictureBox = new System.Windows.Forms.PictureBox();
pictureBox.Image = new Bitmap(@"../../Data/#3_Page2.PNG");
pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

panel.AutoScroll = true;
panel.Controls.Add(this.pictureBox);
于 2013-07-31T12:45:47.143 回答
3

此解决方案有效,但是如果您的图像足够大,滚动时会有一点闪烁(但是可以接受)。首先,您必须VScrollBar在面板右侧和面板HScrollBar底部添加右侧。这个演示需要你有一个VScrollBarnamedvScrollBar1HScrollBarnamed hScrollBar1,一个ButtonnamedbuttonOpenImage允许用户打开一些图像,一个Panelnamedpanel1用作绘制图像的主要区域:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();            
        //To prevent/eliminate flicker, do this
        typeof(Panel).GetProperty("DoubleBuffered", 
                                  System.Reflection.BindingFlags.Instance | 
               System.Reflection.BindingFlags.NonPublic).SetValue(panel1, true, null);
        //------------------------
        UpdateScrollbarsState();
    }        
    int imgWidth, imgHeight;
    Image image;
    Point leftTop = Point.Empty;
    int lastV, lastH;
    private void OpenImage(){
        OpenFileDialog openFile = new OpenFileDialog();            
        openFile.FileOk += (s, e) => {
            try {
                image = Image.FromFile(openFile.FileName);
                //calculate the physical size of the image based on the resolution and its logical size.
                //We have to do this because the DrawImage is based on the physical size (not logical).
                imgWidth = (int)(image.Width * 96f / image.HorizontalResolution + 0.5);
                imgHeight = (int)(image.Height * 96f / image.VerticalResolution + 0.5);
                lastV = lastH = 0;
                UpdateScrollbarsState();
                vScrollBar1.Value = 0;
                hScrollBar1.Value = 0;
                panel1.Invalidate();
            }
            catch {
                image = null;
                MessageBox.Show("Image file is invalid or corrupted!");
            }
        };
        openFile.ShowDialog();
    }
    private void UpdateScrollbarsState() {
        //We have to update all the info about Minimum and Maximum
        vScrollBar1.Minimum = 0;
        hScrollBar1.Minimum = 0;
        vScrollBar1.Maximum = Math.Max(imgHeight-panel1.Height,0);
        hScrollBar1.Maximum = Math.Max(imgWidth-panel1.Width,0);
        vScrollBar1.Visible = vScrollBar1.Maximum > 0;
        hScrollBar1.Visible = hScrollBar1.Maximum > 0;
        if (vScrollBar1.Maximum == 0) {
            leftTop.Y = 0;
            lastV = 0;
        }
        if (hScrollBar1.Maximum == 0) {
            leftTop.X = 0;
            lastH = 0;
        }
    }
    private void panel1_Paint(object sender, PaintEventArgs e) {
        if (image == null) return;
        e.Graphics.DrawImage(image, leftTop);
    }
    //The ValueChanged event handler of your vScrollBar1
    private void vScrollBar1_ValueChanged(object sender, EventArgs e) {
        if (!vScrollBar1.Visible) return;
        leftTop.Offset(0, -vScrollBar1.Value + lastV);
        lastV = vScrollBar1.Value;
        panel1.Invalidate();
    }
    //The ValueChanged event handler of your hScrollBar1
    private void hScrollBar1_ValueChanged(object sender, EventArgs e) {
        if (!hScrollBar1.Visible) return;
        leftTop.Offset(lastH - hScrollBar1.Value, 0);
        lastH = hScrollBar1.Value;
        panel1.Invalidate();
    }
    //handler for SizeChanged event of the panel. However if resizing
    //the form causes the panel's size changing, you should attach this 
    //handler for form.
    private void panel1_SizeChanged(object sender, EventArgs e) {
        UpdateScrollbarsState();
    }
    //handler for the Click event of a button (click to open an image)
    private void buttonOpenImage_Click(object sender, EventArgs e) {
        OpenImage();
    }
}
于 2013-07-31T12:57:34.920 回答