LEADTOOLS GDI/GDI+ (Leadtools.Drawing assembly) Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.29
AddGdiDataToRegion Method
See Also 



image
The source image.
xform
Leadtools.RasterRegionXForm object that LEADTOOLS uses to translate between display coordinates and image coordinates. If you specify null (Nothing in Visual Basic) for this parameter, the scalar fields default to 1, the offsets default to 0, and the view perspective defaults to the image view perspective.
data
An array of System.Byte that defines the region to add.
dataOffset
Byte offset into data where the data starts.
combineMode
The action to take regarding the existing image region, if one is defined.
image
The source image.
xform
Leadtools.RasterRegionXForm object that LEADTOOLS uses to translate between display coordinates and image coordinates. If you specify null (Nothing in Visual Basic) for this parameter, the scalar fields default to 1, the offsets default to 0, and the view perspective defaults to the image view perspective.
data
An array of System.Byte that defines the region to add.
dataOffset
Byte offset into data where the data starts.
combineMode
The action to take regarding the existing image region, if one is defined.
Creates or updates a LEADTOOLS image region using the specified GDI+ region data.

Syntax

Visual Basic (Declaration) 
Public Shared Sub AddGdiDataToRegion( _
   ByVal image As RasterImage, _
   ByVal xform As RasterRegionXForm, _
   ByVal data() As Byte, _
   ByVal dataOffset As Integer, _
   ByVal combineMode As RasterRegionCombineMode _
) 
Visual Basic (Usage)Copy Code
Dim image As RasterImage
Dim xform As RasterRegionXForm
Dim data() As Byte
Dim dataOffset As Integer
Dim combineMode As RasterRegionCombineMode
 
RasterRegionConverter.AddGdiDataToRegion(image, xform, data, dataOffset, combineMode)
C# 
public static void AddGdiDataToRegion( 
   RasterImage image,
   RasterRegionXForm xform,
   byte[] data,
   int dataOffset,
   RasterRegionCombineMode combineMode
)
C++/CLI 
public:
static void AddGdiDataToRegion( 
   RasterImage^ image,
   RasterRegionXForm^ xform,
   array<byte>^ data,
   int dataOffset,
   RasterRegionCombineMode combineMode
) 

Parameters

image
The source image.
xform
Leadtools.RasterRegionXForm object that LEADTOOLS uses to translate between display coordinates and image coordinates. If you specify null (Nothing in Visual Basic) for this parameter, the scalar fields default to 1, the offsets default to 0, and the view perspective defaults to the image view perspective.
data
An array of System.Byte that defines the region to add.
dataOffset
Byte offset into data where the data starts.
combineMode
The action to take regarding the existing image region, if one is defined.

Example

This example will use AddGdiDataToRegion and GetGdiRegionData to add an elliptical region to a Leadtools.RasterImage, gets the region as a GDI byte array object then re-sets it in another image.

Visual BasicCopy Code
Public Sub GdiRegionDataExample()
   Dim codecs As New RasterCodecs()

   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
   Dim destFileName1 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_GdiRegionDataOriginal.bmp"
   Dim regionFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_GdiRegionData.bin"
   Dim destFileName2 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_GdiRegionDataLoaded.bmp"

   RegionDataBefore(codecs, srcFileName, destFileName1, regionFileName)
   RegionDataAfter(codecs, srcFileName, destFileName2, regionFileName)

   codecs.Dispose()
End Sub

Private Sub RegionDataBefore(ByVal codecs As RasterCodecs, ByVal imageFileName As String, ByVal destFileName As String, ByVal regionFileName As String)
   ' Load the image
   Using image As RasterImage = codecs.Load(imageFileName)
      ' Add a polygon region to the image
      Dim x1 As Integer = image.ImageWidth \ 4
      Dim y1 As Integer = image.ImageHeight \ 4
      Dim x2 As Integer = image.ImageWidth \ 3
      Dim y2 As Integer = image.ImageHeight \ 3

      Dim pts() As LeadPoint = _
      { _
         New LeadPoint(x1, y1), _
         New LeadPoint(x2, y1), _
         New LeadPoint(x1, y2), _
         New LeadPoint(x2, y2) _
      }

      image.AddPolygonToRegion(Nothing, pts, LeadFillMode.Winding, RasterRegionCombineMode.Set)

      ' Draw something on the image
      Dim command As New InvertCommand()
      command.Run(image)

      ' Save the region and the image
      codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24)

      Dim data() As Byte = RasterRegionConverter.GetGdiRegionData(image, Nothing)
      Using fs As FileStream = File.Create(regionFileName)
         fs.Write(data, 0, data.Length)
      End Using
   End Using
End Sub

Private Sub RegionDataAfter(ByVal codecs As RasterCodecs, ByVal imageFileName As String, ByVal destFileName As String, ByVal regionFileName As String)
   ' Load the image
   Using image As RasterImage = codecs.Load(imageFileName)
      ' Load the region from the file and apply it to the image
      Dim data() As Byte

      Using fs As FileStream = File.OpenRead(regionFileName)
         ReDim data(CType(fs.Length, Integer) - 1)
         fs.Read(data, 0, data.Length)
      End Using

      RasterRegionConverter.AddGdiDataToRegion(image, Nothing, data, 0, RasterRegionCombineMode.Set)

      ' Draw something on the image
      Dim command As New InvertCommand()
      command.Run(image)

      ' Save the image
      codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24)
   End Using
End Sub
C#Copy Code
public void GdiRegionDataExample()
   {
      RasterCodecs codecs = new RasterCodecs();

      string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
      string destFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_GdiRegionDataOriginal.bmp");
      string regionFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_GdiRegionData.bin");
      string destFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_GdiRegionDataLoaded.bmp");

      RegionDataBefore(codecs, srcFileName, destFileName1, regionFileName);
      RegionDataAfter(codecs, srcFileName, destFileName2, regionFileName);

      codecs.Dispose();
   }

   private void RegionDataBefore(RasterCodecs codecs, string imageFileName, string destFileName, string regionFileName)
   {
      // Load the image
      using(RasterImage image = codecs.Load(imageFileName))
      {
         // Add a polygon region to the image
         int x1 = image.ImageWidth / 4;
         int y1 = image.ImageHeight / 4;
         int x2 = image.ImageWidth / 3;
         int y2 = image.ImageHeight / 3;

         LeadPoint[] pts =
         {
            new LeadPoint(x1, y1),
            new LeadPoint(x2, y1),
            new LeadPoint(x1, y2),
            new LeadPoint(x2, y2)
         };

         image.AddPolygonToRegion(null, pts, LeadFillMode.Winding, RasterRegionCombineMode.Set);

         // Draw something on the image
         InvertCommand command = new InvertCommand();
         command.Run(image);

         // Save the region and the image
         codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24);

         byte[] data = RasterRegionConverter.GetGdiRegionData(image, null);
         using(FileStream fs = File.Create(regionFileName))
         {
            fs.Write(data, 0, data.Length);
         }
      }
   }

   private void RegionDataAfter(RasterCodecs codecs, string imageFileName, string destFileName, string regionFileName)
   {
      // Load the image
      using(RasterImage image = codecs.Load(imageFileName))
      {
         // Load the region from the file and apply it to the image
         byte[] data;

         using(FileStream fs = File.OpenRead(regionFileName))
         {
            data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
         }

         RasterRegionConverter.AddGdiDataToRegion(image, null, data, 0, RasterRegionCombineMode.Set);

         // Draw something on the image
         InvertCommand command = new InvertCommand();
         command.Run(image);

         // Save the image
         codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24);
      }
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}

Remarks

To update an existing region, specify how the new region is to be combined with the existing one using the combineMode parameter. For more information, refer to Leadtools.RasterRegionCombineMode.

This method can be used to copy the region data from one RasterImage to another, using the GetGdiRegionData method.

To get the region data as GDI+ System.Drawing.Drawing2D.RegionData object, use GetGdiPlusRegionData and AddGdiPlusDataToRegion.

To get the region data as platform independent byte array, use RasterRegion.GetData and RasterRegion.SetData.

For more information, refer to Creating a Region.

For more information, refer to Saving A Region.

For more information, refer to Working with the Existing Region.

For more information refer to RasterImage and GDI/GDI+.

Requirements

Target Platforms: Silverlight 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7, MAC OS/X (Intel Only)

See Also