Fix for thumbnail problem in MyPictures (1 Viewer)

gloomyandy

MP Donator
  • Premium Supporter
  • September 15, 2006
    424
    14
    63
    Home Country
    United Kingdom United Kingdom
    Firstly sorry if this is not the correct way to submit a potential bug fix! My first attempt so be gentle!

    Ok I finally got fed up enough with the problem I reported below:
    https://forum.team-mediaportal.com/showthread.php?t=12077

    To try and do something about it. I grabbed a copy of VC# Express and the latest svn source and had a look. The code in question is in file GUIPictures.cs and is in methods CreateFolderThumb and LoadPicture.I found that on creating the thimbnails the DrawImage function was throwing an exception on the face of it there was no obvious reason as to why, the original image had been loaded ok and the background image was also ok.

    After a bit of digging I came across this web page:
    http://www.pixvillage.com/blogs/devblog/archive/2005/03/09/154.aspx
    Which also has a pointer to a MS knowledge base article.

    Basically what these say is that when you load an image from a file or a stream the actual reading of the image may be delayed until the image is used. Destroying the file or the stream before the image may result in "bad things happening". Now the code i(that loads the picture) is creating a stream (and then creating an image from this stream) using a "using block" so the stream will be disposed of when the block exits. The bad news is the function then goes on to return the image created from the stream to the calling function so the lifetime of the stream is < the lifetime of the image.

    To fix this I re-factored the code a little so that the drawing of the image takes place within a new function AddPicture which replaces the old LoadPicture. The code is shown below (I've attached the complete file to this post as a text file):

    Code:
      void AddPicture(Graphics g, string strFileName, int x, int y, int w, int h)
        {
          // Add a thumbnail of the specified picture file to the image referenced by g, draw it at the
          // given location and size.
          Image img = null;
          using (PictureDatabase dbs = new PictureDatabase())
          {
            int iRotate = dbs.GetRotation(strFileName);
    
            using (FileStream stream = new FileStream(strFileName, FileMode.Open))
            {
              img = Image.FromStream(stream, true, false);
    
              if (img != null)
              {
                if (iRotate > 0)
                {
                  RotateFlipType flipType;
                  switch (iRotate)
                  {
                    case 1:
                      flipType = RotateFlipType.Rotate90FlipNone;
                      img.RotateFlip(flipType);
                      break;
                    case 2:
                      flipType = RotateFlipType.Rotate180FlipNone;
                      img.RotateFlip(flipType);
                      break;
                    case 3:
                      flipType = RotateFlipType.Rotate270FlipNone;
                      img.RotateFlip(flipType);
                      break;
                    default:
                      flipType = RotateFlipType.RotateNoneFlipNone;
                      break;
                  }
                }
                g.DrawImage(img, x, y, w, h);
              }
            }
          }
        }

    This is then called as shown:

    Code:
                using (Bitmap bmp = new Bitmap(width, height))
                {
                  using (Graphics g = Graphics.FromImage(bmp))
                  {
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    
                    g.DrawImage(imgFolder, 0, 0, width, height);
                    int x, y, w, h;
                    x = 0; y = 0; w = thumbnailWidth; h = thumbnailHeight;
                    //Load first of 4 images for the folder thumb.
                    //Avoid crashes caused by damaged image files:
                    try
                    {
                        AddPicture(g, (string)pictureList[0], x + 10, y + 10, w, h);
                    }
                    catch (Exception)
                    {
                        Log.Info("Damaged picture file found: {0}. Try to repair or delete this file please!", (string)pictureList[0]);
                    }

    With the above changes I no longer get exceptions when the image is drawn to the thumbnail and thumbnails are created for all of my picture folders with no errors. It is worth noting that if the old LoadPicture function needed to rotate the picture then this would have resulted in the image being initialized from the stream while the stream still existed and so no exception would have been thrown. This fits perfectly with what I was seeing, some pictures used for thumbnails seemed to be ok others did not. On closer inspection the files that worked required rotation, the ones that failed did not.

    Hope this makes sense....

    Andy

    Added to SF as patch
    [ 1597327 ] Blank thumbnail for directories in MyPictures
    https://sourceforge.net/tracker/index.php?func=detail&aid=1597327&group_id=107397&atid=647927
     

    gloomyandy

    MP Donator
  • Premium Supporter
  • September 15, 2006
    424
    14
    63
    Home Country
    United Kingdom United Kingdom
    Does anyone have any comments on this? Should I have submitted this as a patch over at SF? Ie would have done this but I wanted to see if anyone had any comments on the problem I'd seen here with the us of images after the associated stream has been closed.

    Any feedback (even "you are wasting your time!") would be good....

    Andy
     

    mzemina

    Retired Team Member
  • Premium Supporter
  • February 23, 2005
    2,065
    14
    Tulsa, OK
    Home Country
    United States of America United States of America
    Have you tried to get onto the IRC chat and ask some of the developers there?
     

    patrick

    Portal Pro
    April 20, 2005
    608
    45
    Southeast
    Home Country
    United States of America United States of America
    Does anyone have any comments on this? Should I have submitted this as a patch over at SF? Ie would have done this but I wanted to see if anyone had any comments on the problem I'd seen here with the us of images after the associated stream has been closed.

    Any feedback (even "you are wasting your time!") would be good....

    Andy

    You can try IRC and/or I think most people post here that there
    is a patch and then upload the full modified CS file(s) to the patches
    section on SF as a zip with notes on the why and what the
    changes were.


    HTH,
    patrick
     

    gloomyandy

    MP Donator
  • Premium Supporter
  • September 15, 2006
    424
    14
    63
    Home Country
    United Kingdom United Kingdom
    Thanks guys. I used the IRC and got the problem confirmed. I've uploaded the patch to SF and modified the original post to ref. it.

    Andy
     

    Users who are viewing this thread

    Top Bottom