从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);

我知道这与正在设置的锚点有关,但这允许PictureBox以不同的分辨率被很好地看到。我该如何获取真正的绘图区域?

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

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;

最新更新