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 the bitmap tool, but we don’t have a built-in option for selecting a particular region and creating the image node based on the 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 the 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 the 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 an image
img = (Controller.ParentControl as Diagram).ExportDiagramAsImage(true);// Convert model coordinates to client coordinates
PointF bounds = Controller.ConvertFromModelToClientCoordinates(rectBounding.Location);// Crop the selected region as an 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 BitmapNode from the 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 / 100Dim img As Image = New Bitmap(CInt(Fix(Controller.Model.DocumentSize.Width)), CInt(Fix(Controller.Model.DocumentSize.Height)))
' Exporting the diagram as an image
img = (TryCast(Controller.ParentControl, Diagram)).ExportDiagramAsImage(True)' Convert model coordinates to client coordinates
Dim bounds As PointF = Controller.ConvertFromModelToClientCoordinates(rectBounding.Location)' Crop the selected region as an 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 BitmapNode from the selected region
Dim croppedNode As New BitmapNode(croppedImage, rectBounding)img.Dispose()
croppedImage.Dispose()Return croppedNode
End Function
Sample:
Conclusion
I hope you enjoyed learning about Customizing the BitmapTool to create BitmapNode from the selected region instead of browsing the image location with the help of OpenDialogBox.
You can refer to WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!