Back to Devexpress

XtraDiagramExtensions.DrawDetachedItem(DiagramControl, DiagramItem, GraphicsCache) Method

windowsforms-devexpress-dot-xtradiagram-dot-extensions-dot-xtradiagramextensions-dot-drawdetacheditem-x28-diagramcontrol-diagramitem-graphicscache-x29.md

latest5.2 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.Win.Diagram

Declaration

csharp
public static void DrawDetachedItem(
    this DiagramControl diagram,
    DiagramItem diagramItem,
    GraphicsCache graphicsCache
)
vb
<ExtensionAttribute>
Public Shared Sub DrawDetachedItem(
    diagram As DiagramControl,
    diagramItem As DiagramItem,
    graphicsCache As GraphicsCache
)

Parameters

NameTypeDescription
diagramDiagramControl

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.

csharp
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);
        }
    }
}
vb
Imports DevExpress.Diagram.Core
Imports DevExpress.Utils.Drawing
Imports DevExpress.XtraDiagram.Designer
Imports DevExpress.XtraDiagram.Extensions
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Public Module BitmapHelper

     _
    Public Sub DrawWithGraphics(ByVal image As Image, ByVal draw As Action(Of GraphicsCache))
        Using graphics As Graphics = System.Drawing.Graphics.FromImage(image)
            Using cache As New GraphicsCache(New DXPaintEventArgs(graphics,
            Rectangle.Truncate(graphics.ClipBounds)))
                draw(cache)
            End Using
        End Using
    End Sub
End Module

Public Sub GenerateBitmap()
    Dim item = New DiagramShape() With {
        .Width = 120,
        .Height = 130,
        .BackgroundId = DiagramThemeColorId.Accent5
    }
    Using stream = New MemoryStream()
        Using bitmap = New Bitmap(200, 200)
            bitmap.DrawWithGraphics(Sub(cache)
                cache.TranslateTransform(10, 25)
                Diagram.DrawDetachedItem(item, cache)
            End Sub)
            bitmap.Save(stream, ImageFormat.Png)
        End Using
    End Using
End Sub

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

See Also

XtraDiagramExtensions Class

XtraDiagramExtensions Members

DevExpress.XtraDiagram.Extensions Namespace