private void RenderImageWorker()
        {
            IRenderTemplate renderTemplate = new GalleryCoverRenderTemplate();

            Random r             = new Random();
            double imageRotation = r.Next(_imageRotationMin, _imageRotationMax);
            double titleRotation = r.Next(_titleRotationMin, _titleRotationMax);
            double titleOffsetX  = r.Next(_titleOffestXMin, _titleOffestXMax);
            double titleOffsetY  = r.Next(_titleOffestYMin, _titleOffestYMax);
            double imageOffsetX  = r.Next(_imageOffestXMin, _imageOffestXMax);
            double imageOffsetY  = r.Next(_imageOffestYMin, _imageOffestYMax);

            renderTemplate.ApplyImage(_coverImagePath);
            renderTemplate.ApplyTitle(_title);
            renderTemplate.FormatTitle(titleRotation, titleOffsetX, titleOffsetY);
            renderTemplate.FormatImage(imageRotation, imageOffsetX, imageOffsetY);

            renderTemplate.ResolveFonts(_contentPath);

            UserControl renderControl = (UserControl)renderTemplate;

            // Update layout after all the details have been applied.
            renderControl.Measure(new Size(renderControl.Width, renderControl.Height));
            renderControl.Arrange(new Rect(new Size(renderControl.Width, renderControl.Height)));

            // Render the image to a bitmap.
            RenderTargetBitmap bitmapRenderer = new RenderTargetBitmap((int)renderControl.ActualWidth, (int)renderControl.ActualHeight, 96, 96, PixelFormats.Pbgra32);

            bitmapRenderer.Render(renderControl);

            // Encode the bitmap as png.
            PngBitmapEncoder png = new PngBitmapEncoder();

            png.Frames.Add(BitmapFrame.Create(bitmapRenderer));

            // Output the png to the file.
            using (MemoryStream memoryStream = new MemoryStream())
            {
                png.Save(memoryStream);
                this.imageBuffer = memoryStream.ToArray();
            }

            if (bitmapRenderer.Dispatcher.Thread.IsAlive)
            {
                bitmapRenderer.Dispatcher.InvokeShutdown();
            }
        }
        private void RenderImageWorker()
        {
            IRenderTemplate renderTemplate = new GalleryCoverRenderTemplate();

            Random r = new Random();
            double imageRotation = r.Next(_imageRotationMin, _imageRotationMax);
            double titleRotation = r.Next(_titleRotationMin, _titleRotationMax);
            double titleOffsetX = r.Next(_titleOffestXMin, _titleOffestXMax);
            double titleOffsetY = r.Next(_titleOffestYMin, _titleOffestYMax);
            double imageOffsetX = r.Next(_imageOffestXMin, _imageOffestXMax);
            double imageOffsetY = r.Next(_imageOffestYMin, _imageOffestYMax);

            renderTemplate.ApplyImage(_coverImagePath);
            renderTemplate.ApplyTitle(_title);
            renderTemplate.FormatTitle(titleRotation, titleOffsetX, titleOffsetY);
            renderTemplate.FormatImage(imageRotation, imageOffsetX, imageOffsetY);

            renderTemplate.ResolveFonts(_contentPath);

            UserControl renderControl = (UserControl)renderTemplate;

            // Update layout after all the details have been applied.
            renderControl.Measure(new Size(renderControl.Width, renderControl.Height));
            renderControl.Arrange(new Rect(new Size(renderControl.Width, renderControl.Height)));

            // Render the image to a bitmap.
            RenderTargetBitmap bitmapRenderer = new RenderTargetBitmap((int)renderControl.ActualWidth, (int)renderControl.ActualHeight, 96, 96, PixelFormats.Pbgra32);
            bitmapRenderer.Render(renderControl);

            // Encode the bitmap as png.
            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(bitmapRenderer));

            // Output the png to the file.
            using (MemoryStream memoryStream = new MemoryStream())
            {
                png.Save(memoryStream);
                this.imageBuffer = memoryStream.ToArray();
            }

            if (bitmapRenderer.Dispatcher.Thread.IsAlive)
            {
                bitmapRenderer.Dispatcher.InvokeShutdown();
            }
        }