/// <summary> /// Sets the export scale /// </summary> private void SetScale(ContextConfigurator config, bool useViewport) { // get the scale value specified in the options double userScale = (double)Handler.GetValue(BOUNDS, SCALE); // consider the zoom level if (useViewport) { userScale *= graphControl.Zoom; } // look if a fixed size has been specified string sizeChoice = (string)Handler.GetValue(BOUNDS, SIZE); switch (sizeChoice) { case (SPECIFY_WIDTH): int newWidth = (int)Handler.GetValue(BOUNDS, WIDTH); config.Scale = config.CalculateScaleForWidth((int)(userScale * newWidth)); break; case (SPECIFY_HEIGHT): int newHeight = (int)Handler.GetValue(BOUNDS, HEIGHT); config.Scale = config.CalculateScaleForHeight((int)(userScale * newHeight)); break; default: config.Scale = (float)userScale; break; } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { INode listBoxNode = value as INode; if (listBoxNode != null) { graphControl.Graph.Clear(); var size = FlowChartNodeStyle.GetNodeTypeSize(listBoxNode); var node = graphControl.Graph.CreateNode(new RectD(0, 0, size.Width, size.Height), listBoxNode.Style, listBoxNode.Tag); double labelHeight = 0; foreach (var label in listBoxNode.Labels) { graphControl.Graph.AddLabel(node, label.Text, label.LayoutParameter, label.Style, label.PreferredSize, label.Tag); labelHeight = Math.Max(labelHeight, label.GetLayout().Height); } graphControl.FitGraphBounds(new InsetsD(5)); ContextConfigurator cc = new ContextConfigurator(graphControl.ContentRect); var renderContext = cc.CreateRenderContext(graphControl); Transform transform = cc.CreateWorldToIntermediateTransform(); System.Windows.Media.Geometry clip = cc.CreateClip(); var visualContent = graphControl.ExportContent(renderContext); VisualGroup container = new VisualGroup() { Transform = transform, Clip = clip, Children = { visualContent } }; VisualBrush brush = new VisualBrush(container); return(new DrawingImage(new GeometryDrawing(brush, null, new RectangleGeometry(new Rect(0, 0, size.Width, size.Height + labelHeight))))); } else { return(null); } }
/// <summary> /// Creates a new instance the given <paramref name="from">source node</paramref> /// and the possible target <paramref name="nodes"/>. /// </summary> /// <remarks> /// Pressing the "Show Path" button will set the DialogResult.OK and close the dialog. /// It is up to the caller to fetch the ToItem and get the shortest path. /// </remarks> /// <param name="from">The source node.</param> /// <param name="nodes">The nodes to choose the target from.</param> public ShortestPathDialog(INode from, List <INode> nodes) { From = from; Nodes = nodes; InitializeComponent(); toComboBox.DataSource = Nodes; // render the From node in the canvasControl var bounds = new RectD(0, 0, fromControl.Width, fromControl.Height); var layout = new RectD(0, 0, 240, 80); canvasControl.Size = new Size((int)bounds.Width, (int)bounds.Height); canvasControl.HorizontalScrollBarPolicy = ScrollBarVisibility.Never; canvasControl.VerticalScrollBarPolicy = ScrollBarVisibility.Never; canvasControl.Graph.CreateNode(layout, from.Style, from.Tag); canvasControl.ContentRect = bounds; canvasControl.FitContent(); // render the canvasControl in a Bitmap bitmap = new Bitmap(canvasControl.Size.Width, canvasControl.Size.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bitmap)) { g.SmoothingMode = SmoothingMode.HighQuality; g.Clear(Color.Transparent); ContextConfigurator cc = new ContextConfigurator(canvasControl.ContentRect); var renderContext = cc.CreateRenderContext(canvasControl, g); canvasControl.RenderContent(renderContext, g); } fromControl.Image = bitmap; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var graph = value as IGraph; if (graph == null) { return(null); } graphControl.Graph = graph; graphControl.FitGraphBounds(); var size = graphControl.ContentRect; var cc = new ContextConfigurator(graphControl.ContentRect); cc.Scale = Math.Min(cc.CalculateScaleForWidth(size.Width), cc.CalculateScaleForHeight(size.Height)); var renderContext = cc.CreateRenderContext(graphControl); Transform transform = cc.CreateWorldToIntermediateTransform(); Geometry clip = cc.CreateClip(); var visualContent = graphControl.ExportContent(renderContext); var container = new VisualGroup { Transform = transform, Clip = clip, Children = { visualContent } }; var brush = new VisualBrush(container); return(new DrawingImage(new GeometryDrawing(brush, null, new RectangleGeometry(new Rect(0, 0, size.Width, size.Height))))); }
private VisualGroup CreateVisual(CanvasControl canvas) { RectD rect = canvas.ContentRect; ContextConfigurator configurator = new ContextConfigurator(rect) { Margins = new InsetsD(0, 0, 0, 0) }; // scale down if necessary if (ActualHeight > 0 && ActualHeight > 0) { if (ActualHeight < rect.Height || ActualWidth < rect.Width) { configurator.Scale = Math.Min(ActualWidth / rect.Width, ActualHeight / rect.Height); } RectD bounds = RectD.FromCenter(rect.Center, new SizeD(ActualWidth, ActualHeight) * (1 / configurator.Scale)); configurator.WorldBounds = bounds; } IRenderContext vc = configurator.CreateRenderContext(canvas); Transform transform = configurator.CreateWorldToIntermediateTransform(); Visual visual = canvas.ExportContent(vc); return(new VisualGroup { Children = { visual }, Clip = Clip, Transform = transform }); }
/// <summary> /// Paint the style representation. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void styleListBox_DrawItem(object sender, DrawItemEventArgs e) { ListBox listBox = (ListBox)sender; int i = e.Index; e.DrawBackground(); INode node = listBox.Items[i] as INode; var bounds = e.Bounds; InsetsD insets = new InsetsD(2); var canvasControl = new GraphControl(); canvasControl.Size = new Size(bounds.Width - (int)insets.HorizontalInsets, bounds.Height - (int)insets.VerticalInsets); Bitmap bm = new Bitmap(canvasControl.Size.Width, canvasControl.Size.Height); Graphics g = Graphics.FromImage(bm); canvasControl.HorizontalScrollBarPolicy = ScrollBarVisibility.Never; canvasControl.VerticalScrollBarPolicy = ScrollBarVisibility.Never; var dummyNode = canvasControl.Graph.CreateNode(node.Layout.ToRectD(), node.Style, node.Tag); foreach (var label in node.Labels) { canvasControl.Graph.AddLabel(dummyNode, label.Text, label.LayoutParameter, label.Style, label.PreferredSize, label.Tag); } foreach (var port in node.Ports) { canvasControl.Graph.AddPort(dummyNode, port.LocationParameter, port.Style, port.Tag); } canvasControl.ContentRect = new RectD(0, 0, 70, 70); canvasControl.FitContent(); e.DrawBackground(); g.SmoothingMode = SmoothingMode.HighQuality; g.Clear(Color.White); ContextConfigurator cc = new ContextConfigurator(canvasControl.ContentRect); var renderContext = cc.CreateRenderContext(canvasControl, g); canvasControl.RenderContent(renderContext, g); var listGraphics = e.Graphics; var oldClip = listGraphics.Clip; listGraphics.IntersectClip(bounds); listGraphics.Clear(listBox.BackColor); listGraphics.DrawImage(bm, bounds.X + (int)insets.Left, bounds.Y + (int)insets.Top, bm.Width, bm.Height); listGraphics.Clip = oldClip; e.DrawFocusRectangle(); canvasControl.Dispose(); g.Dispose(); bm.Dispose(); }
/// <summary> /// Gets the export configuration /// </summary> private ContextConfigurator GetConfig(RectD worldBounds, bool useViewport) { ContextConfigurator config = new ContextConfigurator(worldBounds) { Projection = graphControl.Projection }; SetScale(config, useViewport); // get the margins int leftMargin = (int)Handler.GetValue(MARGINS, LEFT_MARGIN); int rightMargin = (int)Handler.GetValue(MARGINS, RIGHT_MARGIN); int topMargin = (int)Handler.GetValue(MARGINS, TOP_MARGIN); int bottomMargin = (int)Handler.GetValue(MARGINS, BOTTOM_MARGIN); config.Margins = new InsetsD(leftMargin, topMargin, rightMargin, bottomMargin); return(config); }
/// <summary> /// Creates the visual for the label. /// </summary> /// <remarks> /// Implementation of LabelStyleBase.CreateVisual. /// </remarks> /// <param name="context">The render context.</param> /// <param name="label">The label to which this style instance is assigned.</param> /// <returns>The visual as required by the <see cref="IVisualCreator.CreateVisual"/> interface.</returns> protected override VisualGroup CreateVisual(IRenderContext context, ILabel label) { // Updates the dummy label which is internally used for rendering with the properties of the given label. UpdateDummyLabel(context, label); // creates the container for the visual and sets a transform for view coordinates var container = new VisualGroup(); // ReSharper disable once PossibleUnintendedReferenceComparison if (container.Transform != context.IntermediateTransform) { container.Transform = context.IntermediateTransform; } RenderDataCache cache = CreateRenderDataCache(context, label); container.SetRenderDataCache(cache); var creator = InnerLabelStyle.Renderer.GetVisualCreator(dummyLabel, InnerLabelStyle); // create a new IRenderContext with a zoom of 1 var cc = new ContextConfigurator(context.CanvasControl.ContentRect); var innerContext = cc.CreateRenderContext(context.CanvasControl); //The wrapped style should always think it's rendering with zoom level 1 var visual = creator.CreateVisual(innerContext); if (visual == null) { return(container); } // add the created visual to the container container.Children.Add(visual); // if the label is selected, add the selection visualization, too. if (cache.Selected) { UIElement selectionVisual = CreateSelectionVisual(innerContext, dummyLabel.GetLayout()) as UIElement; if (selectionVisual != null) { selectionVisual.IsHitTestVisible = false; } container.Children.Add(selectionVisual); } return(container); }
/// <summary> /// Paint <see cref="IGraph">component</see> as elements of the palette. /// </summary> private static void OnDrawItem(object sender, DrawItemEventArgs e) { if (sender is ListBox listBox && listBox.Items[e.Index] is IGraph component) { var bounds = e.Bounds; InsetsD insets = new InsetsD(10); // create a GraphControl that shows the component var componentControl = new GraphControl { Size = new Size(bounds.Width - (int)insets.HorizontalInsets, bounds.Height - (int)insets.VerticalInsets), HorizontalScrollBarPolicy = ScrollBarVisibility.Never, VerticalScrollBarPolicy = ScrollBarVisibility.Never, Graph = component }; componentControl.FitGraphBounds(); // create a bitmap with the same size as the GraphControl Bitmap bm = new Bitmap(componentControl.Size.Width, componentControl.Size.Height); Graphics g = Graphics.FromImage(bm); e.DrawBackground(); g.SmoothingMode = SmoothingMode.HighQuality; g.Clear(Color.White); // render the content of the GraphControl into the bitmap ContextConfigurator cc = new ContextConfigurator(componentControl.ContentRect); var renderContext = cc.CreateRenderContext(componentControl, g); componentControl.RenderContent(renderContext, g); // render the image as an element of the palette var listGraphics = e.Graphics; var oldClip = listGraphics.Clip; listGraphics.IntersectClip(bounds); listGraphics.Clear(listBox.BackColor); listGraphics.DrawImage(bm, bounds.X + (int)insets.Left, bounds.Y + (int)insets.Top, bm.Width, bm.Height); listGraphics.Clip = oldClip; e.DrawFocusRectangle(); componentControl.Dispose(); g.Dispose(); bm.Dispose(); } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { INode listBoxNode = value as INode; if (listBoxNode == null) { return(null); } graphControl.Graph.Clear(); var node = graphControl.Graph.CreateNode(listBoxNode.Layout.ToRectD(), listBoxNode.Style, listBoxNode.Tag); foreach (var label in listBoxNode.Labels) { graphControl.Graph.AddLabel(node, label.Text, label.LayoutParameter, label.Style, label.PreferredSize, label.Tag); } graphControl.FitGraphBounds(); ContextConfigurator cc = new ContextConfigurator(graphControl.ContentRect); cc.Scale = Math.Min(cc.CalculateScaleForWidth(size.Width), cc.CalculateScaleForHeight(size.Height)); var renderContext = cc.CreateRenderContext(graphControl); Transform transform = cc.CreateWorldToIntermediateTransform(); System.Windows.Media.Geometry clip = cc.CreateClip(); var visualContent = graphControl.ExportContent(renderContext); VisualGroup container = new VisualGroup { Transform = transform, Clip = clip, Children = { visualContent } }; VisualBrush brush = new VisualBrush(container); return (new DrawingImage(new GeometryDrawing(brush, null, new RectangleGeometry(new Rect(0, 0, size.Width, size.Height))))); }
public ApplicationInstaller() { _useRealDatabase = false; _contextConfigurator = new ContextConfigurator(); }
public ApplicationInstaller(string connectionString) { _useRealDatabase = true; _contextConfigurator = new ContextConfigurator(connectionString); }
private void ImageExportToFile() { GraphControl control = graphControl; // check if the rectangular region or the whole viewport should be printed bool useRect = (bool)handler.GetValue(OUTPUT, EXPORT_RECTANGLE); RectD bounds = useRect ? exportRect.ToRectD() : control.Viewport; // get the format string formatChoice = (string)Handler.GetValue(OUTPUT, FORMAT); string format = Formats[formatChoice]; // check whether decorations (selection, handles, ...) should be hidden bool hide = (bool)handler.GetValue(OUTPUT, HIDE_DECORATIONS); if (hide) { // if so, create a new graph control with the same graph control = new GraphControl { Graph = graphControl.Graph, FlowDirection = graphControl.FlowDirection }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); if (format.Equals("XPS")) { // create the exporter XpsExporter emfImageExporter = new XpsExporter(config); exporter = emfImageExporter; saveFileDialog.Reset(); saveFileDialog.Filter = "XPS files (*.xps)|*.xps"; if (saveFileDialog.ShowDialog(this) == true) { string fileName = saveFileDialog.FileName; if (File.Exists(fileName)) { File.Delete(fileName); } using (FileStream stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite)) { exporter.Export(control, stream); } } } else { var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = control.Background ?? Brushes.White; } exporter = pixelImageExport; saveFileDialog.Reset(); saveFileDialog.Filter = format + "|*." + format.Substring(6); if (saveFileDialog.ShowDialog(this) == true) { FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write); exporter.Export(control, stream); stream.Flush(); stream.Close(); } } }
private void ImageExportToFile() { GraphControl control = graphControl; // check if the rectangular region or the whole viewport should be exported bool useRect = (bool)handler.GetValue(OUTPUT, EXPORT_RECTANGLE); RectD bounds = useRect ? exportRect.ToRectD() : control.Viewport; // get the format string formatChoice = (string)Handler.GetValue(OUTPUT, FORMAT); string format = Formats[formatChoice]; // check whether decorations (selection, handles, ...) should be hidden bool hide = (bool)handler.GetValue(OUTPUT, HIDE_DECORATIONS); if (hide) { // if so, create a new graphcontrol whith the same graph control = new GraphControl { Graph = graphControl.Graph }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); if (format.Equals("EMF")) { var transparentBackground = (bool)Handler.GetValue(EMF, TRANSPARENT); // create the exporter EmfImageExporter emfImageExporter = new EmfImageExporter(config); if (!transparentBackground) { emfImageExporter.Background = new SolidBrush(graphControl.BackColor); } exporter = emfImageExporter; saveFileDialog.Reset(); saveFileDialog.Filter = "EMF files (*.emf)|*.emf"; if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write); exporter.Export(control, stream); stream.Flush(); stream.Close(); } } else { // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = new SolidBrush(control.BackColor); } exporter = pixelImageExport; saveFileDialog.Reset(); saveFileDialog.Filter = format + "|*." + format.Substring(6); if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write); try { exporter.Export(control, stream); } catch (IOException exception) { MessageBox.Show(exception.Message, "I/O Error", MessageBoxButtons.OK); } finally { stream.Flush(); stream.Close(); } } } }
/// <summary> /// Update the visual previously created by <see cref="CreateVisual"/>. /// </summary> /// <remarks> /// Implementation of LabelStyleBase.UpdateVisual. /// </remarks> /// <param name="context">The render context.</param> /// <param name="oldVisual">The visual that has been created in the call to <see cref="CreateVisual"/>.</param> /// <param name="label">The label to which this style instance is assigned.</param> /// <returns>The visual as required by the <see cref="IVisualCreator.CreateVisual"/> interface.</returns> /// <seealso cref="CreateVisual"/> protected override VisualGroup UpdateVisual(IRenderContext context, VisualGroup oldVisual, ILabel label) { // get the data with which the old visual was created RenderDataCache oldCache = oldVisual.GetRenderDataCache <RenderDataCache>(); // get the data for the new visual RenderDataCache newCache = CreateRenderDataCache(context, label); oldVisual.SetRenderDataCache(newCache); // create a new visual if the cache has been changed or the container's contents seem to be wrong bool cacheChanged = !newCache.Equals(oldCache); if (cacheChanged) { return(CreateVisual(context, label)); } if (oldVisual == null || oldVisual.Children.Count != (newCache.Selected?2:1)) { return(CreateVisual(context, label)); } var visual = oldVisual.Children[0]; if (visual == null) { return(CreateVisual(context, label)); } // Updates the dummy label which is internally used for rendering with the properties of the given label. UpdateDummyLabel(context, label); // create a new IRenderContext with a zoom of 1 var cc = new ContextConfigurator(context.CanvasControl.ContentRect); var innerContext = cc.CreateRenderContext(context.CanvasControl); // ReSharper disable once PossibleUnintendedReferenceComparison if (oldVisual.Transform != context.IntermediateTransform) { oldVisual.Transform = context.IntermediateTransform; } // update the visual created by the inner style renderer var creator = InnerLabelStyle.Renderer.GetVisualCreator(dummyLabel, InnerLabelStyle); var updatedVisual = creator.UpdateVisual(innerContext, visual); if (updatedVisual == null) { // nothing to display -> return nothing return(null); } // ReSharper disable once PossibleUnintendedReferenceComparison if (updatedVisual != visual) { oldVisual.Remove(visual); oldVisual.Add(updatedVisual); } // if selected: update the selection visual, too. if (newCache.Selected) { var oldSelectionVisual = oldVisual.Children[1]; Visual selectionVisual = UpdateSelectionVisual(innerContext, oldSelectionVisual, dummyLabel.GetLayout()); // ReSharper disable once PossibleUnintendedReferenceComparison if (oldSelectionVisual != selectionVisual) { oldVisual.Children.Remove(oldSelectionVisual); oldVisual.Children.Add(selectionVisual); } } return(oldVisual); }
private ImageSource ImageExport() { GraphControl control = graphControl; // check if the rectangular region or the whole viewport should be exported bool useRect = (bool)handler.GetValue(OUTPUT, EXPORT_RECTANGLE); RectD bounds = useRect ? exportRect.ToRectD() : control.Viewport; // get the format string formatChoice = (string)Handler.GetValue(OUTPUT, FORMAT); string format = Formats[formatChoice]; // check whether decorations (selection, handles, ...) should be hidden bool hide = (bool)handler.GetValue(OUTPUT, HIDE_DECORATIONS); if (hide) { // if so, create a new graph control with the same graph control = new GraphControl { Graph = graphControl.Graph, FlowDirection = graphControl.FlowDirection, Background = graphControl.Background, Projection = graphControl.Projection }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); if (format.Equals("XPS")) { // create the exporter XpsExporter xpsExporter = new XpsExporter(config); string tempFileName = Path.GetTempFileName(); using (var fileStream = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite)) { xpsExporter.Export(control, fileStream); } documentViewer.Visibility = Visibility.Visible; previewCanvas.Visibility = Visibility.Hidden; documentViewer.Document = new XpsDocument(tempFileName, FileAccess.Read).GetFixedDocumentSequence(); return(null); } else { // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = control.Background ?? Brushes.White; } exporter = pixelImageExport; var memoryStream = new MemoryStream(); try { exporter.Export(control, memoryStream); // reset the stream memoryStream.Position = 0; // and read back the image for display var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); if (previewCanvas.RootGroup.First != null) { previewCanvas.RootGroup.First.Remove(); } documentViewer.Visibility = Visibility.Hidden; previewCanvas.Visibility = Visibility.Visible; var image = new Image { Source = bitmapImage, Width = bitmapImage.Width, Height = bitmapImage.Height }; image.SetCanvasArrangeRect(new Rect(0, 0, image.Width, image.Height)); previewCanvas.RootGroup.AddChild(image, CanvasObjectDescriptors.Visual); previewCanvas.ContentRect = new RectD(0, 0, bitmapImage.Width, bitmapImage.Height).GetEnlarged(20); return(bitmapImage); } catch (IOException exception) { MessageBox.Show(exception.Message, "I/O Error", MessageBoxButton.OK); return(null); } } }
private Image ImageExport() { GraphControl control = graphControl; // check if the rectangular region or the whole viewport should be exported bool useRect = (bool)handler.GetValue(OUTPUT, EXPORT_RECTANGLE); RectD bounds = useRect ? exportRect.ToRectD() : control.Viewport; // get the format string formatChoice = (string)Handler.GetValue(OUTPUT, FORMAT); string format = Formats[formatChoice]; // check whether decorations (selection, handles, ...) should be hidden bool hide = (bool)handler.GetValue(OUTPUT, HIDE_DECORATIONS); if (hide) { // if so, create a new graph control with the same graph control = new GraphControl { Graph = graphControl.Graph, BackColor = graphControl.BackColor, Projection = graphControl.Projection }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); if (format.Equals("EMF")) { var transparentBackground = (bool)Handler.GetValue(EMF, TRANSPARENT); // create the exporter EmfImageExporter emfImageExporter = new EmfImageExporter(config); if (!transparentBackground) { emfImageExporter.Background = new SolidBrush(graphControl.BackColor); } exporter = emfImageExporter; var memoryStream = new MemoryStream(); exporter.Export(control, memoryStream); // reset the stream memoryStream.Position = 0; // and read back the metafile for display return(new Metafile(memoryStream)); } else { // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = new SolidBrush(control.BackColor); } exporter = pixelImageExport; var memoryStream = new MemoryStream(); try { exporter.Export(control, memoryStream); // reset the stream memoryStream.Position = 0; // and read back the image for display return(new Bitmap(memoryStream)); } catch (IOException exception) { MessageBox.Show(exception.Message, "I/O Error", MessageBoxButtons.OK); return(null); } } }