public SemanticHtmlGalleryCommand(CommandId commandId, IBlogPostEditingSite editingSite, TemplateHtmlDelegate templateHtmlDelegate, CommandManager commandManager, IHtmlEditorComponentContext componentContext)
            : base(commandId)
        {
            _editingSite      = editingSite;
            _componentContext = componentContext;

            _elements = new List <SemanticHtmlElementInfo>(7);
            _elements.Add(new SemanticHtmlElementInfo("p", _ELEMENT_TAG_ID.TAGID_P, Res.Get(StringId.Paragraph), SemanticHtmlPreviewManager.PreviewId_P, CommandId.ApplySemanticParagraph));
            _elements.Add(new SemanticHtmlElementInfo("h1", _ELEMENT_TAG_ID.TAGID_H1, Res.Get(StringId.Heading1), SemanticHtmlPreviewManager.PreviewId_H1, CommandId.ApplySemanticHeader1));
            _elements.Add(new SemanticHtmlElementInfo("h2", _ELEMENT_TAG_ID.TAGID_H2, Res.Get(StringId.Heading2), SemanticHtmlPreviewManager.PreviewId_H2, CommandId.ApplySemanticHeader2));
            _elements.Add(new SemanticHtmlElementInfo("h3", _ELEMENT_TAG_ID.TAGID_H3, Res.Get(StringId.Heading3), SemanticHtmlPreviewManager.PreviewId_H3, CommandId.ApplySemanticHeader3));
            _elements.Add(new SemanticHtmlElementInfo("h4", _ELEMENT_TAG_ID.TAGID_H4, Res.Get(StringId.Heading4), SemanticHtmlPreviewManager.PreviewId_H4, CommandId.ApplySemanticHeader4));
            _elements.Add(new SemanticHtmlElementInfo("h5", _ELEMENT_TAG_ID.TAGID_H5, Res.Get(StringId.Heading5), SemanticHtmlPreviewManager.PreviewId_H5, CommandId.ApplySemanticHeader5));
            _elements.Add(new SemanticHtmlElementInfo("h6", _ELEMENT_TAG_ID.TAGID_H6, Res.Get(StringId.Heading6), SemanticHtmlPreviewManager.PreviewId_H6, CommandId.ApplySemanticHeader6));

            _previewManager = new SemanticHtmlPreviewManager(_editingSite, templateHtmlDelegate, RibbonHelper.InGalleryImageWidth, RibbonHelper.InGalleryImageHeightWithoutLabel);

            _ids = new string[_elements.Count];
            for (int i = 0; i < _ids.Length; i++)
            {
                _ids[i] = _elements[i].HtmlId;

                int index = i;
                OverridableCommand command = new OverridableCommand(_elements[i].CommandId);
                command.Execute += (sender, e) => SelectedIndex = index;
                commandManager.Add(command);
            }
        }
        private void screenCapture_HtmlScreenCaptureAvailable(object sender, HtmlScreenCaptureAvailableEventArgsCore e)
        {
            if (e.CaptureCompleted && e.Bitmap != null)
            {
                // Ensure that the path exists
                try
                {
                    // Problem because this check is pumping messages on bg thread?
                    string directory = BlogEditingTemplate.GetBlogTemplateDir(_blogId);
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                }
                catch (Exception ex)
                {
                    Trace.Fail(ex.ToString());
                    return;
                }

                // Now we should have all of the element images
                foreach (string elementId in _screenCapture.Ids)
                {
                    using (Bitmap elementBitmap = _screenCapture.GetElementCaptureProperties(elementId).Bitmap)
                    {
                        // Warning. elementBitmap may be null.
                        string path = SemanticHtmlPreviewManager.GetPreviewBitmapPath(_blogId, elementId);

                        try
                        {
                            // We take the lock here to ensure that we don't try to read a halfway-written file on the UI thread.
                            lock (_previewLock)
                            {
                                ElementCaptureProperties properties = _screenCapture.GetElementCaptureProperties(elementId);
                                using (Bitmap previewBitmap = GetGalleryPreviewImageFromElementImage(elementBitmap, _previewImageWidth, _previewImageHeight, properties.BackgroundColor, properties.Padding, _isRtl))
                                {
                                    previewBitmap.Save(path);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine("Failed to get preview image for blog(" + _blogId + ") and element(" + elementId + "): " + ex);
                            try
                            {
                                File.Delete(path);
                            }
                            catch (Exception ex2)
                            {
                                Trace.WriteLine("Failed to delete preview image after failing to get it:" + ex2);
                            }
                            return;
                        }
                    }
                }

                try
                {
                    _editingSite.FrameWindow.Invoke(new ThreadStart(() => _editingSite.CommandManager.Invalidate(
                                                                        CommandId.SemanticHtmlGallery)), null);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("NotifyWeblogStylePreviewChanged failed: " + ex.Message);
                }
            }
        }