Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

XtraDiagramExtensions.DrawDetachedItem(DiagramControl, DiagramItem, GraphicsCache) Method

Allows you to draw a diagram item outside the Canvas/Shapes Panel.

Namespace: DevExpress.XtraDiagram.Extensions

Assembly: DevExpress.XtraDiagram.v24.2.dll

NuGet Package: DevExpress.Win.Diagram

#Declaration

public static void DrawDetachedItem(
    this DiagramControl diagram,
    DiagramItem diagramItem,
    GraphicsCache graphicsCache
)

#Parameters

Name Type Description
diagram DiagramControl

A diagram that is the owner of the item.

diagramItem DiagramItem

A diagram item to draw.

graphicsCache GraphicsCache

A GraphicsCache object that provides painting facilities.

#Remarks

You can use the DrawDetachedItem method to create a custom Shapes Panel.

The CustomDrawItemEventArgs.Context property returns the DiagramDrawingContext.Detached value for items that are drawn by the DrawDetachedItem method.

The example below illustrates how to generate a bitmap from a diagram item.

using DevExpress.Diagram.Core;
using DevExpress.Utils.Drawing;
using DevExpress.XtraDiagram.Designer;
using DevExpress.XtraDiagram.Extensions;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public static class BitmapHelper {

    public static void DrawWithGraphics(this Image image, Action<GraphicsCache> draw) {
        using (Graphics graphics = Graphics.FromImage(image)) {
            using (GraphicsCache cache = new GraphicsCache(new DXPaintEventArgs(graphics,
            Rectangle.Truncate(graphics.ClipBounds)))) {
                draw(cache);
            }
        }
    }
}

public void GenerateBitmap() {
    var item = new DiagramShape() { Width = 120, Height = 130, BackgroundId = DiagramThemeColorId.Accent5 };
    using (var stream = new MemoryStream()) {
        using (var bitmap = new Bitmap(200, 200)) {
            bitmap.DrawWithGraphics(cache => {
                cache.TranslateTransform(10, 25);
                Diagram.DrawDetachedItem(item, cache);
            });
            bitmap.Save(stream, ImageFormat.Png);
        }
    }
}

The How to: Create a Custom Toolbox example illustrates the usage of the DrawDetachedItem method.

See Also