试图获取空文本框的消息警告

本文关键字:消息 警告 文本 获取 | 更新日期: 2023-09-27 18:10:03

我目前正在开发一个隐写程序。

问题是,每当用户没有选择图像时,就会出现错误。因此,我决定在每次用户未能选择图像时提示错误消息,但它不起作用。

错误发生在,

Bitmap img = new Bitmap(fileText.Text);

上面写着argument exception is unhandled, the path is not of a legal form.

private void encodeBtn_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(fileText.Text))
    {
        MessageBox.Show("abc");
        return;
    }
    Bitmap img = new Bitmap(fileText.Text);
    for (int i = 0; i < img.Width; i++)
    {
        for (int j = 0; j < img.Height; j++)
        {
            Color pixel = img.GetPixel(i, j);
            if (i < 1 && j < msgText.TextLength)
            {
                char letter = Convert.ToChar(msgText.Text.Substring(j, 1));
                int value = Convert.ToInt32(letter);
                img.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, value));
            }
            if (i == img.Width - 1 && j == img.Height - 1)
            {
                img.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, msgText.TextLength));
            }
        }
    }

试图获取空文本框的消息警告

您应该检查文件名是否正确以及该文件是否真的存在:

string fileName = fileText.Text;
if(string.IsNullOrWhiteSpace(fileName) || !System.IO.File.Exists(fileName)) {
    MessageBox.Show("Wrong file name");
    return;
}

我认为你测试字符串的逻辑是错误的:

    if (string.IsNullOrWhiteSpace(fileText.Text))
{
    MessageBox.Show("abc");
    return;
}

对吧?因为您希望在消息框为空或空白时显示该消息框。此外,我认为空文本框是一个空字符串,而不是空白。