Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. How to detect invalid image in a ImageBrush

How to detect invalid image in a ImageBrush

Scheduled Pinned Locked Moved WCF and WF
helptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adgonz
    wrote on last edited by
    #1

    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?

    S S 2 Replies Last reply
    0
    • A adgonz

      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?

      S Offline
      S Offline
      sivaddrahcir
      wrote on last edited by
      #2

      The BitmapImage has a DownloadFailed[^] event that you can use.

      A 1 Reply Last reply
      0
      • A adgonz

        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?

        S Offline
        S Offline
        schiebel t
        wrote on last edited by
        #3

        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!");
                }
        
        A 1 Reply Last reply
        0
        • S sivaddrahcir

          The BitmapImage has a DownloadFailed[^] event that you can use.

          A Offline
          A Offline
          adgonz
          wrote on last edited by
          #4

          Hi, Thanks, but it seems that this event is not fired (because of local file?) I also tried DecodeFailed, but neither is fired.

          1 Reply Last reply
          0
          • S schiebel t

            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!");
                    }
            
            A Offline
            A Offline
            adgonz
            wrote on last edited by
            #5

            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.

            S 1 Reply Last reply
            0
            • A adgonz

              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.

              S Offline
              S Offline
              sivaddrahcir
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups