【问题标题】:PictureBox doesn't show up in C# WinFormPictureBox 不显示在 C# WinForm 中
【发布时间】:2015-02-18 06:07:43
【问题描述】:

我正在尝试创建一个自定义错误对话框,左侧有一个红色 x。以下是我的代码;

using JohnsonControls.FieldBusFDD.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JohnsonControls.FieldBusFDD
{
    public class ErrorDialog : Dialog
    {
        public static bool ShowDialog(string text, string title)
        {
            Form prompt = new Form();
            prompt.Width = 435;
            prompt.Height = 122;
            prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
            prompt.Text = title;
            prompt.StartPosition = FormStartPosition.CenterScreen;

            PictureBox image = new PictureBox();
            image.Image = Resources.red_x;
            image.Location = new Point(10, 10);
            image.Size = new Size(50, 50);

            Label textLabel = new Label() { Left = 60, Top = 10, Width = 350, Text = text };

            Button confirmation = new Button() { Text = "Ok", Left = 300, Width = 100, Top = 52 };
            confirmation.Click += (sender, e) => { prompt.Close(); };
            confirmation.DialogResult = DialogResult.OK;

            prompt.Controls.Add(image);
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.AcceptButton = confirmation;

            return prompt.ShowDialog() == DialogResult.OK ? true : false;
        }
    }
}

该文件存储在项目资源中,编译正常但不显示图像。

【问题讨论】:

  • 您确定代码不适合您吗?我只是查看了代码并设法运行代码,并且能够获得带有指定图像(jpg文件)的对话框。我根本没有改变你的代码。您确定red_x 是正确的图像吗?您通过尝试使用图像查看器查看来验证这一点。
  • @fujiFX 是的,.png 不支持吗?
  • 我猜图片的像素太大了?试试image.SizeMode = PictureBoxSizeMode.Zoom;
  • @ripple,这行得通。创建一个答案,我会选择它。谢谢!

标签: c# winforms picturebox


【解决方案1】:

当图片像素过大时,PictureBox默认只显示图片的左上角。

试试这个:

image.SizeMode = PictureBoxSizeMode.Zoom;

来自MSDN

默认情况下,在 Normal 模式下,Image 位于 PictureBox 的左上角,任何对于 PictureBox 来说太大的图像部分都会被剪裁掉。使用 StretchImage 值会使图像拉伸或收缩以适合 PictureBox。使用 Zoom 值会导致图像被拉伸或缩小以适合 PictureBox;但是,原始的纵横比保持不变。

【讨论】:

    【解决方案2】:

    尝试通过以下方式在图片框中拉伸图像:

     image.SizeMode = PictureBoxSizeMode.StretchImage;
    

    Using the StretchImage value causes the image to stretch or shrink to fit the PictureBox.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2016-07-08
      • 2018-06-19
      相关资源
      最近更新 更多