Windows > Software Development for Windows Client Forums > Windows Imaging Component (WIC) > Unreliable code to generate thumbnails with original metadata
Ask a questionAsk a question
 

Proposed AnswerUnreliable code to generate thumbnails with original metadata

  • Sunday, May 10, 2009 2:29 PMXavierP Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I want to generate "thumbnails" (I mean here small images keeping the original metadata). Here is my code below. It works most of the times but randomly it raises an exception saying "Can't write datas".

                MemoryStream mem;
                BitmapImage ReducedImage;

                try
                {
                    byte[] buffer = File.ReadAllBytes(path);
                    // by reading the data into an in-memory buffer, we prevent the file from being read in the UI thread
                    // -- which speeds up access dramatically!
                    using (mem = new MemoryStream(buffer))
                    {
                        // Create a small version of it !
                        ReducedImage = new BitmapImage();
                        ReducedImage.BeginInit();
                        ReducedImage.DecodePixelWidth = DataManager.TManager.ThumbSize;
                        ReducedImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                        ReducedImage.CacheOption = BitmapCacheOption.Default;
                        ReducedImage.StreamSource = mem;
                        ReducedImage.EndInit();
                        ReducedImage.Freeze();

                        // Create a thumbnail for next time !!!

                        JpegBitmapEncoder enco = new JpegBitmapEncoder();
                        enco.QualityLevel = 30;

                        MemoryStream mem2 = new MemoryStream(buffer);
                        BitmapFrame _image = BitmapFrame.Create(mem2, BitmapCreateOptions.None, BitmapCacheOption.None);
                        BitmapMetadata metadata = _image.Metadata as BitmapMetadata;
                        BitmapFrame bf = BitmapFrame.Create(ReducedImage, null, metadata, null);
                       
                        enco.Frames.Add(bf);
                        string ftb = DataManager.TManager.ThumbPath(path);
                        FileStream stream;
                        string dir_name = DirectoryName(ftb);
                        if (!Directory.Exists(dir_name))
                            Directory.CreateDirectory(dir_name);
                        stream = new FileStream(ftb, FileMode.Create);
                        enco.Save(stream);
                        stream.Flush();
                        stream.Close();
                    }
                    return true;

    PS: Called from a WPF application.

    Any ideas ?
    Thanks !

All Replies

  • Wednesday, November 04, 2009 6:26 AMBen Vincent [Hotmail] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    Here's some code that you can adapt for your needs. It copies metadata from one file to another but you could do this all before saving the file.

    http://www.tassography.com/blog.aspx?b=63367240823

    Ben

  • Wednesday, November 18, 2009 7:41 PMElliott Prechter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Two things that would really help to figure this out: (1) Remove the try block so you can get the exact line that is throwing the exception and (2) find the specific image that, when passed through this, will cause it to fail.

    Since this is such an old thread I'm going to go ahead and mark this as potentially answered by Bin Vincent's post, but please re-activate the thread if this is still an open issue.