Customizing the BitmapTool to create BitmapNode from the selected region instead of browsing the image location with the help of OpenDialogBox
In Diagram control, we can create the image node using bitmap tool but we don’t have a built-in option for selecting the particular region and create the image node based on selected region. However we can achieve the desired requirement by customizing the Syncfusion.Windows.Forms.Diagram.BitmapTool and by creating the Bitmapnode from the selected region instead of browsing the image location with the help of OpenDialogBox
Step1:
The following code example shows creating the custom bitmaptool and setting the name for customized bitmaptool while initializing.
[C#]
public class CustomSelectTool : BitmapTool { private string toolName = "CustomBitmapTool"; public CustomSelectTool(DiagramController controller) : base(controller) { this.Name = toolName; } }
[VB]
Public Class CustomSelectTool Inherits BitmapTool Private toolName As String = "CustomBitmapTool" Public Sub New(ByVal controller As DiagramController) MyBase.New(controller) Me.Name = toolName End Sub End Class
Step 2:
To override the CreateNode() method in customized bitmaptool to create the bitmapnode from the selected region
The following code example to create the bitmapnode from the selected region:
[C#]
protected override Node CreateNode(System.Drawing.RectangleF rectBounding) { //calculating the zoom factor float scale = this.Controller.View.Magnification / 100; Image img = new Bitmap((int)(Controller.Model.DocumentSize.Width), (int)(Controller.Model.DocumentSize.Height)); // exporting the diagram as image img = (Controller.ParentControl as Diagram).ExportDiagramAsImage(true); //convert model coordinates to client coordinate PointF bounds = Controller.ConvertFromModelToClientCoordinates(rectBounding.Location); //Crop the Selected region as image Bitmap croppedImage = (img as Bitmap).Clone(new System.Drawing.Rectangle((int)(bounds.X), (int)(bounds.Y ), (int)(rectBounding.Size.Width * scale), (int)(rectBounding.Size.Height * scale)), PixelFormat.Format64bppArgb); //Creating the bitmap node from selected region BitmapNode croppedNode = new BitmapNode(croppedImage, rectBounding); img.Dispose(); croppedImage.Dispose(); return croppedNode; }
[VB]
Protected Overrides Function CreateNode(ByVal rectBounding As System.Drawing.RectangleF) As Node 'calculating the zoom factor Dim scale As Single = Me.Controller.View.Magnification / 100 Dim img As Image = New Bitmap(CInt(Fix(Controller.Model.DocumentSize.Width)), CInt(Fix(Controller.Model.DocumentSize.Height))) ' exporting the diagram as image img = (TryCast(Controller.ParentControl, Diagram)).ExportDiagramAsImage(True) 'convert model coordinates to client coordinate Dim bounds As PointF = Controller.ConvertFromModelToClientCoordinates(rectBounding.Location) 'Crop the Selected region as image Dim croppedImage As Bitmap = (TryCast(img, Bitmap)).Clone(New System.Drawing.Rectangle(CInt(Fix(bounds.X)), CInt(Fix(bounds.Y)), CInt(Fix(rectBounding.Size.Width * scale)), CInt(Fix(rectBounding.Size.Height * scale))), PixelFormat.Format64bppArgb) 'Creating the bitmap node from selected region Dim croppedNode As New BitmapNode(croppedImage, rectBounding) img.Dispose() croppedImage.Dispose() Return croppedNode End Function
Sample: