PictureBox没有出现在C#WinForm中

时间:2015-02-18 06:07:44

标签: c# winforms picturebox

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

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

2 个答案:

答案 0 :(得分:1)

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

试试这个:

image.SizeMode = PictureBoxSizeMode.Zoom;

来自MSDN

  

默认情况下,在“正常”模式下,“图像”位于PictureBox的左上角,图像的任何对于PictureBox来说太大的部分都会被剪裁。使用StretchImage值会导致图像拉伸或缩小以适合PictureBox。使用“缩放”值可以拉伸或缩小图像以适合PictureBox;但是,保持原件的纵横比。

答案 1 :(得分:0)

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

 image.SizeMode = PictureBoxSizeMode.StretchImage;

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

相关问题