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



Converts between a LEADTOOLS Leadtools.RasterRegion and GDI HRGN and System.Drawing (GDI+) System.Drawing.Region objects.

Object Model

RasterRegionConverter Class

Syntax

Visual Basic (Declaration) 
Public MustInherit NotInheritable Class RasterRegionConverter 
Visual Basic (Usage)Copy Code
Dim instance As RasterRegionConverter
C# 
public static class RasterRegionConverter 
C++/CLI 
public ref class RasterRegionConverter abstract sealed 

Example

Visual BasicCopy Code
Public Sub RasterRegionConverterExample()
   ' Load an image 
   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
   Dim dstFileName1 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_Ellipse.jpg"
   Dim dstFileName2 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_EllipseTranslated.jpg"

   Dim codecs As New RasterCodecs()

   Dim image As RasterImage = codecs.Load(srcFileName)

   ' We will add an ellipse region to the image

   ' Using GDI+, create an elliptical region
   Dim gdipRegion As Region

   Using path As New GraphicsPath()
      path.AddEllipse(100, 200, 400, 300)
      gdipRegion = New Region(path)
   End Using

   ' Create a RasterRegion from this region
   Using region As RasterRegion = RasterRegionConverter.ConvertFromRegion(gdipRegion)
      ' Add this region to the image
      image.SetRegion(Nothing, region, RasterRegionCombineMode.Set)
   End Using

   gdipRegion.Dispose()

   ' Fill the image with a color
   Dim cmd As New FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Red))
   cmd.Run(image)

   ' Save it
   codecs.Save(image, dstFileName1, RasterImageFormat.Jpeg, 24)

   ' Now get the GDI+ region from the image
   Using region As RasterRegion = image.GetRegion(Nothing)
      ' Convert it to GDI+
      gdipRegion = RasterRegionConverter.ConvertToRegion(region, Nothing)
   End Using

   ' Move the region 100 pixels to the right and bottom
   gdipRegion.Translate(100, 100)

   ' Re-set it into the image, fill again and save
   ' Create a RasterRegion from this region
   Using region As RasterRegion = RasterRegionConverter.ConvertFromRegion(gdipRegion)
      ' Add this region to the image
      image.SetRegion(Nothing, region, RasterRegionCombineMode.Set)
   End Using

   gdipRegion.Dispose()

   ' Fill the image with a color
   cmd = New FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Yellow))
   cmd.Run(image)

   ' Save it
   codecs.Save(image, dstFileName2, RasterImageFormat.Jpeg, 24)

   image.Dispose()

   codecs.Dispose()
End Sub
C#Copy Code
public void RasterRegionConverterExample()
   {
      // Load an image 
      string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
      string dstFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Ellipse.jpg");
      string dstFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_EllipseTranslated.jpg");

      RasterCodecs codecs = new RasterCodecs();

      RasterImage image = codecs.Load(srcFileName);

      // We will add an ellipse region to the image

      // Using GDI+, create an elliptical region
      Region gdipRegion;

      using(GraphicsPath path = new GraphicsPath())
      {
         path.AddEllipse(100, 200, 400, 300);
         gdipRegion = new Region(path);
      }

      // Create a RasterRegion from this region
      using(RasterRegion region = RasterRegionConverter.ConvertFromRegion(gdipRegion))
      {
         // Add this region to the image
         image.SetRegion(null, region, RasterRegionCombineMode.Set);
      }

      gdipRegion.Dispose();

      // Fill the image with a color
      FillCommand cmd = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Red));
      cmd.Run(image);

      // Save it
      codecs.Save(image, dstFileName1, RasterImageFormat.Jpeg, 24);

      // Now get the GDI+ region from the image
      using(RasterRegion region = image.GetRegion(null))
      {
         // Convert it to GDI+
         gdipRegion = RasterRegionConverter.ConvertToRegion(region, null);
      }

      // Move the region 100 pixels to the right and bottom
      gdipRegion.Translate(100, 100);

      // Re-set it into the image, fill again and save
      // Create a RasterRegion from this region
      using(RasterRegion region = RasterRegionConverter.ConvertFromRegion(gdipRegion))
      {
         // Add this region to the image
         image.SetRegion(null, region, RasterRegionCombineMode.Set);
      }

      gdipRegion.Dispose();

      // Fill the image with a color
      cmd = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Yellow));
      cmd.Run(image);

      // Save it
      codecs.Save(image, dstFileName2, RasterImageFormat.Jpeg, 24);

      image.Dispose();

      codecs.Dispose();
   }

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

Remarks

This class lets you easily convert between a LEADTOOLS Leadtools.RasterRegion object and a GDI HRGN or System.Drawing (GDI+) System.Drawing.Region object.

The LEADTOOLS Leadtools.RasterRegion class provides a platform independent representation of an area of interest in a Leadtools.RasterImage that can be used in any platform supported by LEADTOOLS such as GDI, GDI+, WPF and Silverlight. Use this class To convert a LEADTOOLS Leadtools.RasterRegion object to/from a GDI HRGN and System.Drawing (GDI+) System.Drawing.Region object.

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

Inheritance Hierarchy

System.Object
   Leadtools.Drawing.RasterRegionConverter

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