Jump to content



Photo

  • Please log in to reply
7 replies to this topic

#1 sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 25 March 2012 - 21:51

Maybe the method I am using is one of bad practice. I'm going to assume there is a better way of doing it. I have an image bound to a byte array (that will be designated by a byte array in a database but that is outside of the scope of this topic). The thing is it creates a memory stream but I can not close the memory stream and properly dispose of it because the image will no longer show since the memorystream will become unreadable/unusable.

public sealed class ImageIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] buffer = (value as byte[]);
            if (buffer == null)
                return null;
            MemoryStream mStream = new MemoryStream(buffer);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = mStream;
            bi.EndInit();

            /*mStream.Close();
            mStream.Dispose();
            mStream = null;*/ <---------------- can not use these lines, if I do the image will not show because the memory stream will become unusable.
            return bi;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage bi = (value as BitmapImage);
            byte[] buffer = new byte[bi.StreamSource.Position - 1];
            bi.StreamSource.Read(buffer, 0, (int)bi.StreamSource.Position - 1);
            return buffer;
        }
    }
Any ideas as to what I could do better?


#2 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Ontario, Canada
  • OS: Windows 7
  • Phone: Android (4.1.2)

Posted 25 March 2012 - 21:54

what are you trying to show? If I were you, I would save the image, then load it directly as opposed to a stream.

#3 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 25 March 2012 - 21:55

nevermind I forgot to use the cache option. For those who stumble across this problem set the cache option to OnLoad :)

#4 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 25 March 2012 - 21:56

The reason I'm not saving the images is because they are all stored in the databases as byte arrays and I don't want to use HD space.

#5 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Ontario, Canada
  • OS: Windows 7
  • Phone: Android (4.1.2)

Posted 25 March 2012 - 21:59

The reason I'm not saving the images is because they are all stored in the databases as byte arrays and I don't want to use HD space.


That's fair, though a few kb's isn't that much. Glad you got it working, and is a good tip if I need to do this.

#6 Riva

Riva

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 11-February 07

Posted 25 March 2012 - 22:01

Or use .Flush() on the memory stream before you dispose it.

#7 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 25 March 2012 - 22:09

It's not the cause of the problem, but in general you should avoid calling Dispose() manually: that's what the using statement is for. Here:

using (MemoryStream mStream = new MemoryStream(buffer)) {
  BitmapImage bi = new BitmapImage();
  bi.BeginInit();
  bi.StreamSource = mStream;
  bi.EndInit();
  return bi;
}
It's not simply because it looks cleaner, it's mainly because you're guaranteed Dispose() will be called even if an exception causes the method to exit early.

#8 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 10 April 2012 - 17:33

Thanks for the replies everyone :) good tip Dr_Asik!