A generic error occurred in GDI+ when reading from MemoryStream

I am playing with TagLib#, an Open Source library that supports reading and updating ID3 metadata from MP3 files. I ran across the following error — “a generic error occurred in gdi+” — displaying a bitmap image I pulled from the MP3 file.

   TagLib.File file = TagLib.File.Create(@"foo.mp3");
   int pictureCount = file.Tag.Pictures.Length;
   System.Drawing.Bitmap bmp = null;
   if (pictureCount > 0)
   {
      System.IO.MemoryStream pictureBitstream = new System.IO.MemoryStream(
                                                    file.Tag.Pictures[0].Data.Data);
      bmp = new System.Drawing.Bitmap(pictureBitstream);
      pictureBitstream.Close();

      string fn = "foo.jpg";

      // create thumbnail
      using (System.Drawing.Image thumb =
                      bmp.GetThumbnailImage(10, 10,
                               new System.Drawing.Image.GetThumbnailImageAbort(
                                        delegate { return false; }),
                               IntPtr.Zero))
      {
         thumb.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
      }
   }

The “a generic error occurred in gdi+” message is not very helpful. I did some googling and came up with a few helpful posts on the matter, including several that appeared to be different problems. Looks like 4 Guys from Rolla have functional code.

My problem was in closing the stream too soon, which was not obvious to me at first, though makes some sense. The fix is to defer

      pictureBitstream.Close();

until after the Bitmap object is done with its work. I was initially under the illusion that once I’d created the stream for the image it read the whole image; of course that’s not true – it is not read in until it is read in.

I would submit this is a confusing UI design. Developers deserve better affordances. Good thing an exception was thrown; otherwise, this may have blown up later in a less obvious manner or at a less convenient time.

Coding Out Loud begins

“If one advances confidently in the direction of his dreams,
and endeavors to live the life which he has imagined,
he will meet with a success unexpected in common hours.”
Henry David Thoreau
from Walden

I am embarking on a coding adventure – and I plan to blog as I go. In effect, Coding Out Loud – thus the name of this blog. I do not know whether anyone will read this blog or follow my adventures, but I will give it a try anyway. I expect many of my posts will be the kind I would personally like to find – the kind I may even refer back to from time to time for reference – because it will in part be a repository of useful information, hard-won insights, interesting approaches, and documentation of thought processes for making trade-offs. My technology orientation right now is ASP.NET 2.0.

I have been a blog reader for a long time and a big fan of podcasting since its early years. For podcast listening, since September 2007 I have owned a sixth-generation iPod Video with 160 GB of space (bigger than hard drives on most of my computers – including my work computer – and I am a professional developer!); I do watch some video on this (e.g., TED), but I do like me some big hard drive (my older iPod nano constantly ran out of space from too many podcasts).

Since I am now embarking on blogging for myself the first time, I will try to make sense of features of WordPress (categories or tags?), technorati, feedburner, and others of the many technologies & services out there. Which should I care about – and why. This process may spawn blog posts.

I also do have a day job but I will not blog on it. So my adventures will be “off hours” – mornings, nights, weekends mostly – on my solo projects.

So, in summary, this blog will encompass my adventures coding + my adventures blogging (or podcasting someday..).

So the journey begins…