简体   繁体   中英

Trying to get message alert for empty text box

I am currently developing a steganography program.

The Problem is, whenever the user does not choose an image, an error occurs. So I decided to prompt with an error message every time the user fails to choose the image, but it did not work.

The error occurs at,

Bitmap img = new Bitmap(fileText.Text);

It says, 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));
            }
        }
    }

I think your logic for testing the string is wrong:

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

Right? Because you want to show the message box if it is null or a whitespace. Also, I think an empty text box is an empty string, not a whitespace.

You should check whether the file name is correct and whether or not this file is really exist:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM