PictureBox 没有出现在 C# WinForm 中

PictureBox doesn't show up in C# WinForm

我正在尝试创建一个自定义错误对话框,它的左侧有一个红色的 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;
        }
    }
}

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

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

试试这个:

image.SizeMode = PictureBoxSizeMode.Zoom;

来自MSDN

By default, in Normal mode, the Image is positioned in the upper-left corner of the PictureBox, and any part of the image that is too big for the PictureBox is clipped. Using the StretchImage value causes the image to stretch or shrink to fit the PictureBox. Using the Zoom value causes the image to be stretched or shrunk to fit the PictureBox; however, the aspect ratio in the original is maintained.

尝试通过以下方式拉伸图片框上的图片:

 image.SizeMode = PictureBoxSizeMode.StretchImage;

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