从PictureBox获取绘图区域

时间:2012-09-06 15:32:08

标签: c# picturebox

我遇到了PictureBox在不同分辨率之间有不同大小的问题。

我有一个我需要适应PictureBox的图像,但我需要知道它的绘图大小,因为我需要自己调整大小(否则系统太慢了,我决定手动调整大小,如果我知道所需的分辨率,这可以正常工作。

我尝试了PictureBox.Height / WidthPictureBox.ClientRectangle.Height / Width,但所有分辨率的值都相同。如何设法获得实际的图纸尺寸?

初始化代码:

            // 
            // PicboxRed
            // 
            this.PicboxRed.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));

            this.PicboxRed.BackColor = System.Drawing.Color.DimGray;
            this.PicboxRed.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.PicboxRed.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.PicboxRed.Location = new System.Drawing.Point(19, 92);
            this.PicboxRed.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.PicboxRed.Name = "PicboxRed";
            this.PicboxRed.Size = new System.Drawing.Size(852, 840);
            this.PicboxRed.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
            this.PicboxRed.TabIndex = 9;
            this.PicboxRed.TabStop = false;
            this.PicboxRed.Click += new System.EventHandler(this.PicboxRed_Click);
            this.PicboxRed.Paint += new System.Windows.Forms.PaintEventHandler(this.Picbox_Paint);

我知道这与设置的Anchors有关,但这可以让PictureBox在不同的分辨率下得到很好的体现。我如何抓住真正的绘图区域?

2 个答案:

答案 0 :(得分:1)

ClientSize属性告诉您它有多大。 ClientSizeChanged事件会告诉您它何时因任何原因而发生更改,包括由于表单的AutoScaleMode属性而自动缩放。

答案 1 :(得分:0)

I tried PictureBox.Height / Width, and PictureBox.ClientRectangle.Height / Width, but that values are the same for all resolutions.

我认为您正在寻找dpi设置:

int currentDPI = 0;

using (Graphics g = this.CreateGraphics())
{
    currentDPI = (int)g.DpiX;    
}

此值应在具有不同分辨率和dpi设置的计算机上更改。

或许您对获得当前屏幕分辨率感兴趣。他们可能会有所帮助:

Rectangle resolution = Screen.PrimaryScreen.Bounds;