void generateImageCollage()
        {
            ItemProgressMax = 100;
            ItemProgress = 0;
            //ItemInfo = "Creating video preview image for: " + System.IO.Path.GetFileName(item.Location);

            FileStream outputFile = null;
            RenderTargetBitmap bitmap = null;
                      
            try
            {                                                                        
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                BitmapMetadata metaData = new BitmapMetadata("jpg");
                metaData.ApplicationName = App.getAppInfoString();
                metaData.DateTaken = DateTime.Now.ToString("R");

                int? maxGridHeight = AsyncState.IsMaxGridHeightEnabled ? new Nullable<int>(AsyncState.MaxGridHeight) : null;

                App.Current.Dispatcher.Invoke(() =>
                {
                    PictureGridImage gridImage = new PictureGridImage(AsyncState, AsyncState.Media.ToList(), AsyncState.IsUseThumbs);
                    bitmap = gridImage.createGridImage(AsyncState.MaxWidth, maxGridHeight);

                });

                encoder.Frames.Add(BitmapFrame.Create(bitmap, null, metaData, null));

                String outputPath = AsyncState.OutputPath + "\\" + AsyncState.Filename + ".jpg";

                outputFile = new FileStream(outputPath, FileMode.Create);
                encoder.QualityLevel = AsyncState.JpegQuality;
                encoder.Save(outputFile);

                ItemProgress = ItemProgressMax;
                TotalProgress = TotalProgressMax;
                InfoMessages.Add("Finished image collage: " + outputPath);

            }
            catch (Exception e)
            {
                InfoMessages.Add("Error creating image collage: " + e.Message);
                Logger.Log.Error("Error creating image collage: ", e);
            }
            finally
            {               
                if (bitmap != null)
                {
                    bitmap.Clear();
                    bitmap = null;
                }

                if (outputFile != null)
                {
                    outputFile.Close();
                }

                // because of the extreme amount of memory used by rendertargetbitmap 
                // make sure we have it all back before moving on to prevent out of memory spikes
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
               
            }
        }
Example #2
0
        void generateImageCollage()
        {
            ItemProgressMax = 100;
            ItemProgress    = 0;
            //ItemInfo = "Creating video preview image for: " + System.IO.Path.GetFileName(item.Location);

            FileStream         outputFile = null;
            RenderTargetBitmap bitmap     = null;

            try
            {
                JpegBitmapEncoder encoder  = new JpegBitmapEncoder();
                BitmapMetadata    metaData = new BitmapMetadata("jpg");
                metaData.ApplicationName = App.getAppInfoString();
                metaData.DateTaken       = DateTime.Now.ToString("R");

                int?maxGridHeight = AsyncState.IsMaxGridHeightEnabled ? new Nullable <int>(AsyncState.MaxGridHeight) : null;

                App.Current.Dispatcher.Invoke(() =>
                {
                    PictureGridImage gridImage = new PictureGridImage(AsyncState, AsyncState.Media.ToList(), AsyncState.IsUseThumbs);
                    bitmap = gridImage.createGridImage(AsyncState.MaxWidth, maxGridHeight);
                });

                encoder.Frames.Add(BitmapFrame.Create(bitmap, null, metaData, null));

                String outputPath = AsyncState.OutputPath + "\\" + AsyncState.Filename + ".jpg";

                outputFile           = new FileStream(outputPath, FileMode.Create);
                encoder.QualityLevel = AsyncState.JpegQuality;
                encoder.Save(outputFile);

                ItemProgress  = ItemProgressMax;
                TotalProgress = TotalProgressMax;
                InfoMessages.Add("Finished image collage: " + outputPath);
            }
            catch (Exception e)
            {
                InfoMessages.Add("Error creating image collage: " + e.Message);
                Logger.Log.Error("Error creating image collage: ", e);
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Clear();
                    bitmap = null;
                }

                if (outputFile != null)
                {
                    outputFile.Close();
                }

                // because of the extreme amount of memory used by rendertargetbitmap
                // make sure we have it all back before moving on to prevent out of memory spikes
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
        }