How to detect invalid image in a ImageBrush
-
I'm loading a image into a brush dynamically, but it seems the image isn't actually loaded until the brush is used, so my applicatiion doesn't detect the error, until later, when the user shows the control which uses this brush and a exception is thrown. I don't know on how to catch this exception locally on my control.
try { ImageBrush ib = new ImageBrush( new BitmapImage( new Uri(invalidImagePath, UriKind.RelativeOrAbsolute))); DockPanel.Background = ib; } catch (Exception) { // This doesn't happens }
How could I detect this situation?
-
I'm loading a image into a brush dynamically, but it seems the image isn't actually loaded until the brush is used, so my applicatiion doesn't detect the error, until later, when the user shows the control which uses this brush and a exception is thrown. I don't know on how to catch this exception locally on my control.
try { ImageBrush ib = new ImageBrush( new BitmapImage( new Uri(invalidImagePath, UriKind.RelativeOrAbsolute))); DockPanel.Background = ib; } catch (Exception) { // This doesn't happens }
How could I detect this situation?
The BitmapImage has a DownloadFailed[^] event that you can use.
-
I'm loading a image into a brush dynamically, but it seems the image isn't actually loaded until the brush is used, so my applicatiion doesn't detect the error, until later, when the user shows the control which uses this brush and a exception is thrown. I don't know on how to catch this exception locally on my control.
try { ImageBrush ib = new ImageBrush( new BitmapImage( new Uri(invalidImagePath, UriKind.RelativeOrAbsolute))); DockPanel.Background = ib; } catch (Exception) { // This doesn't happens }
How could I detect this situation?
hi, try this:
FileInfo fileInf = new FileInfo(ImagePath); if (fileInf.Exists) { ImageBrush ib = new ImageBrush( new BitmapImage( new Uri(invalidImagePath, UriKind.RelativeOrAbsolute))); DockPanel.Background = ib; } else { MessageBox.Show("Invalid image path!"); }
-
The BitmapImage has a DownloadFailed[^] event that you can use.
-
hi, try this:
FileInfo fileInf = new FileInfo(ImagePath); if (fileInf.Exists) { ImageBrush ib = new ImageBrush( new BitmapImage( new Uri(invalidImagePath, UriKind.RelativeOrAbsolute))); DockPanel.Background = ib; } else { MessageBox.Show("Invalid image path!"); }
-
Hi, this is what I did, but there are still some problems: - The file could exists, but be a invalid image. - The file exists at the moment this code executes, but it is deleted before the Brush needs it (this can happen on my application). Thanks.
Hi Antonio, When the file does exist, you can load it into memory by doing something like:
byte[] imageBytes = null;
imageBytes = File.ReadAllBytes(fileName);Next, when you want to create an ImageBrush to assign as a Background, you can do something like:
using (MemoryStream ms = new MemoryStream(imageBytes))
{
BitmapImage img= new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.StreamSource = ms;
img.EndInit();ImageBrush imBrush = new ImageBrush(); imBrush.ImageSource = img; DockPanel.Background = imBrush;
}
Use a try...catch in case the image is bad, or you may want to make that determination right after loading the image file into memory (depends upon your app). Note: you could also use BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad) to create an ImageSource instead of the BitmapImage and it's BeginInit and EndInit.
Blog: http://windowsclientdevelopment.spaces.live.com FAQs: http://windowspresentationfoundation.wikispaces.com http://windowsmobile.wikispaces.com http://vsto.wikispaces.com