【问题标题】:Get drawing area from PictureBox从 PictureBox 中获取绘图区域
【发布时间】:2012-09-07 02:10:39
【问题描述】:

我遇到了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 在不同的分辨率下被很好地看到。我如何抓住那个真正的绘图区域?

【问题讨论】:

    标签: c# picturebox


    【解决方案1】:

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

    【讨论】:

    • 谢谢,原来它只在实际显示时调整大小,而不是在初始化后。之后它显示了预期值。
    【解决方案2】:

    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;

    【讨论】:

      猜你喜欢
      • 2010-11-03
      • 1970-01-01
      • 2015-12-14
      • 2021-04-14
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      相关资源
      最近更新 更多