←Select platform

ConvertFromRegionHandle Method

Summary
Converts a GDI HRGN object to a LEADTOOLS Leadtools.RasterRegion.
Syntax
C#
C++/CLI
Python
public static RasterRegion ConvertFromRegionHandle( 
   IntPtr hrgn 
) 
public: 
static RasterRegion^ ConvertFromRegionHandle(  
   IntPtr hrgn 
)  
def ConvertFromRegionHandle(self,hrgn): 

Parameters

hrgn
The source GDI HRGN object. This parameter cannot be IntPtr.Zero.

Return Value

The LEADTOOLS Leadtools.RasterRegion object this method creates. You must dispose this object after using it.

Remarks

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+, and WPF. 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+.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
 
// Windows API to use in this example 
[DllImport("Gdi32")] 
private static extern IntPtr CreateEllipticRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 
[DllImport("Gdi32")] 
private static extern int DeleteObject(IntPtr hObject); 
[DllImport("Gdi32")] 
private static extern int OffsetRgn(IntPtr hrgn, int nXOffset, int nYOffset); 
 
public void RasterRegionHandleExample() 
{ 
   // 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 
   IntPtr gdiRegion = CreateEllipticRgn(100, 200, 100 + 400, 200 + 300); 
 
   // Create a RasterRegion from this region 
   using (RasterRegion region = RasterRegionConverter.ConvertFromRegionHandle(gdiRegion)) 
   { 
      // Add this region to the image 
      image.SetRegion(null, region, RasterRegionCombineMode.Set); 
   } 
 
   DeleteObject(gdiRegion); 
 
   // 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+ 
      gdiRegion = RasterRegionConverter.ConvertToRegionHandle(region, null); 
   } 
 
   // Move the region 100 pixels to the right and bottom 
   OffsetRgn(gdiRegion, 100, 100); 
 
   // Re-set it into the image, fill again and save 
   // Create a RasterRegion from this region 
   using (RasterRegion region = RasterRegionConverter.ConvertFromRegionHandle(gdiRegion)) 
   { 
      // Add this region to the image 
      image.SetRegion(null, region, RasterRegionCombineMode.Set); 
   } 
 
   DeleteObject(gdiRegion); 
 
   // 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:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.3.31
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Drawing Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.