【问题标题】:How to detect whether pictureBox successfully displayed the image or not?如何检测pictureBox是否成功显示图片?
【发布时间】:2013-09-05 18:30:41
【问题描述】:

我有一个pictureBox,它直接从互联网加载图像。图像可以动态更改,并由用户在textBox 中指定,该TextChanged 事件将pictureBox 中的图像更改为textBox 中的URL。当用户单击提交按钮时,图像 URL 将保存在数据库中。但在保存之前,我想验证图像,是图像显示成功还是错误图像代替它显示。那么我该如何验证呢?

【问题讨论】:

标签: c# .net winforms validation picturebox


【解决方案1】:

您可以使用 LoadComplete 事件来查看它何时发生变化,以及 eventArg 的错误是 null(成功)还是非 null(失败)。

void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
        if (e.Error != null)
            MessageBox.Show(e.Error.ToString());
}

this.pictureBox1.Validated += new EventHandler(pictureBox1_Validated);
this.pictureBox1.ImageLocation = this.textBox1.Text;

- 编辑:刚刚看到 Dips 的评论,没有使用该链接,但回答此问题的方法相同。

【讨论】:

  • 谢谢。这就是我真正想要的:)
【解决方案2】:

将下面的代码放在从 textBox 中检索图像路径的函数中,确保在对该路径执行任何其他操作之前放置它;

    string path = "Path to image";
    Bitmap bmp;//To validate the Image.
    try
    {
        bmp = new Bitmap(path);//Create a Bitmap object from the given path.
        if (bmp != null)
        {
            pictureBox1.Load(path);//Display the image to the user.
            //Now it's safe to store the image path in the database,
            //because we have validated it.
            bmp.Dispose();//Dispose the Bitmap object to free occupied resources.
            //Place you database related code here which uses the path we just validated.
        }

    }

    catch (ArgumentException)
    {
        MessageBox.Show("The specified image file is invalid.");
        //Show error image in PictureBox. 
        //(pictureBox1.Image="Path to error image").
        //Don't store image path,its invalid.
    }

    catch (FileNotFoundException)
    {
        MessageBox.Show("The path to image is invalid.");
        //Show error image in PictureBox. 
        //(pictureBox1.Image="Path to error image").
        //Don't store image path,its invalid.
    }

完成此操作后,您可以将代码放置在我显示注释的位置 //Place your database...。这可确保文件路径和图像在其他任何事情之前都经过验证使用它们。`此方法还检查图像文件是否实际上是图像而不是 .txt.exe 扩展名更改为 .jpg 或任何其他图像格式,正如您在 cmets 中提到的,您需要检查路径是否实际指向图像文件。

如果您需要的不仅仅是显示带有错误信息的MessageBox,您可以扩展异常处理机制。还有一点值得一提的是,before you display any image or do anything you will have to check if the url is valid one,to simplify this step you can try to download the file(it can be anything - an image,an executable,a text file or at least a web page,when it has been downloaded pass the path to that file(relative to filesystem) to this function.

希望它对你有用。

【讨论】:

    【解决方案3】:

    假设 Pic1 是您的控件的名称。验证然后你可以简单地使用,

    if(pic1.ImageLocation.Trim().Length>4)   // > 4 since a shortest valid image 
                                                file will be a.png or something 
                                                similar; length= 5
    {
       if(validExtensions(pic1.ImageLocation)
        {
           //then put the path to database
        }
    }
    

    更新

    //Mehod to valid image extensions
    private bool validExtensions(string url)
    {
       var imgs = new []{".jpg",".gif",".png",".bmp",".jpeg"};
       var ext = System.IO.Path.GetFileExtention(url); // see the correct method
                                                           in intellisense
        if(imgs.Contains(ext)
          return false;
    }
    

    更新 2

            OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter =  "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
            dialog.InitialDirectory = @"C:\";
            dialog.Title = "Please select an image file to encrypt.";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //Encrypt the selected file. I'll do this later. :)
            }  
    

    【讨论】:

    • 但是如果用户给了这个http://www.somedomain.com/images/r.htm或者是pictureBox不支持的图片文件呢?
    • @AishwaryaShiva 看到我更新的答案。感谢您指出缺失的部分
    • 但问题仍然没有解决。您的解决方案将运行良好,但它完全取决于文件名或 URL。因此,如果有人只是为了恶作剧而将文本文件“ABC.TXT”重命名为“ABC.JPG”并将其 URL 添加到 TextBox。该表单仍将被验证,并且该 URL 将保存在数据库中。当其他人打开此图像时,它将显示错误图像。但我想在发帖前停止。
    • @AishwaryaShiva ,然后我认为,您需要使用打开文件对话框和打开文件对话框将文本框设置为只读或禁用,仅过滤图像。搜索 OpenFileDialog 。查看更新的答案
    • 正如我在问题中所说,图像是从互联网获取的,用户在文本框中输入 URL。所以我不能使用 OpenFileDialog。
    猜你喜欢
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多