C# 如何检测pictureBox是否成功显示图像?
我有一个C# 如何检测pictureBox是否成功显示图像?,c#,.net,winforms,validation,picturebox,C#,.net,Winforms,Validation,Picturebox,我有一个pictureBox,可以直接从internet加载图像。图像可以动态更改,并由用户在textBox中指定,该TextChanged事件将pictureBox中的图像更改为textBox中的URL。当用户单击submit按钮时,图像URL保存在数据库中。但在保存之前,我想验证图像,无论是成功显示图像还是显示错误图像来代替它。那个么我如何验证这个呢?假设Pic1是您控件的名称。为了验证,您可以简单地使用 if(pic1.ImageLocation.Trim().Length>4)
pictureBox
,可以直接从internet加载图像。图像可以动态更改,并由用户在textBox
中指定,该TextChanged
事件将pictureBox
中的图像更改为textBox
中的URL。当用户单击submit按钮时,图像URL保存在数据库中。但在保存之前,我想验证图像,无论是成功显示图像还是显示错误图像来代替它。那个么我如何验证这个呢?假设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. :)
}
将下面的代码放在从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.
}
完成此操作后,您可以将代码放置在我显示注释的位置。//放置您的数据库…。这确保文件路径和图像在其他任何操作使用它们之前经过验证。`此方法还检查图像文件是否真的是图像,而不是扩展名为.txt或.exe更改为.jpg或任何其他图像格式,如您在评论中所述,您需要检查路径是否实际指向图像文件
如果您需要的不仅仅是显示带有错误信息的消息框
,还可以扩展异常处理机制。另外值得一提的是,在显示任何图像或执行任何操作之前,您必须检查url是否有效,为了简化此步骤,您可以尝试下载文件(它可以是任何内容—图像、可执行文件、文本文件或至少是网页,下载后将该文件的路径(相对于文件系统)传递给此函数。
希望它对您有用。您可以使用LoadComplete事件查看它何时发生了更改,以及eventArg的错误是null(成功)还是notnull(失败) -
编辑:刚才看到了Dips的评论,没有使用该链接,但回答这个问题的方式相同。但是如果用户给出了这个
http://www.somedomain.com/images/r.htm
或pictureBox不支持的图像文件?@AishwaryaShiva请查看我的更新答案。感谢您指出缺少的部分,但问题仍然没有解决。您r解决方案可以很好地工作,但它完全取决于文件名或URL。因此,如果有人只是为了某种恶作剧而将文本文件“ABC.TXT”重命名为“ABC.JPG”并将其URL添加到文本框中。表单仍将被验证,该URL将保存在数据库中。当其他人打开此图像时,它将显示错误图像。但我想在发布之前停止此操作。@AishwaryaShiva,那么我想,您需要使用“打开文件”对话框和“打开文件日志”将文本框设置为只读或禁用框,仅过滤图像。搜索OpenFileDialog。请参阅更新的ANSA。我在问题中指出,图像是从internet获取的,用户在文本框中输入URL。因此我无法使用OpenFileDialog。请检查此链接。谢谢。这是我真正想要的:)
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;