←Select platform

SetRegion Method

Summary
Sets or updates the RasterRegion object that describes this RasterImage current region.
Syntax
C#
Objective-C
C++/CLI
Java
Python
- (BOOL)setRegion:(nullable LTRasterRegionXForm *)xform  
           region:(nullable LTRasterRegion *)region  
      combineMode:(LTRasterRegionCombineMode)combineMode  
            error:(NSError **)error 
public void setRegion( 
   RasterRegionXForm xform,  
   RasterRegion region,  
   RasterRegionCombineMode combineMode 
) 
def SetRegion(self,xform,region,combineMode): 

Parameters

xform
RasterRegionXForm object that LEADTOOLS uses to translate between display coordinates and image coordinates. If you specify null for this parameter, the scalar fields default to 1, the offsets default to 0, and the view perspective defaults to the image view perspective.

region
A RasterRegion object to set or update into this RasterImage. If this parameter is an empty region or null, then the current region in the image will be deleted (this is the equivalent of calling MakeRegionEmpty.

combineMode
The action to take regarding the existing image region, if one is defined.

Remarks

To get the RasterRegion object that describes the current image region, use GetRegion.

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

The RasterRegion class provides a platform independent representation of a region of interest in an image that can be used in any platform supported by LEADTOOLS such as GDI, GDI+, and WPF. The RasterRegion class contains extensive methods and properties to manipulate the region data and save/load it to a byte array.

To convert a LEADTOOLS RasterRegion object to/from a device dependent region, you can use the following helper classes:

The RasterRegion class implements the IDisposable interface, so you must call Dispose on any region objects you create after using it.

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.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
 
 
public void RasterRegionExample() 
{ 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_WithRegion1.bmp"); 
   string destFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_WithRegion2.bmp"); 
 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      // Load the source image 
      using (RasterImage image = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)) 
      { 
         // Create a new rectangular RasterRegion 
         using (RasterRegion region = new RasterRegion(new LeadRect(20, 30, 100, 200))) 
         { 
            // Show this region properties 
            Console.WriteLine("IsEmpty: {0}\nBounds: {1}", region.IsEmpty, region.GetBounds()); 
 
            // Set it into the image 
            image.SetRegion(null, region, RasterRegionCombineMode.Set); 
 
            // Fill the image with a color and save it to disk to show the region 
            FillCommand cmd = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Yellow)); 
            cmd.Run(image); 
 
            codecs.Save(image, destFileName1, RasterImageFormat.Bmp, 24); 
 
            // Clear this region 
            region.MakeEmpty(); 
 
            // Show its properties 
            Console.WriteLine("IsEmpty: {0}\nBounds: {1}", region.IsEmpty, region.GetBounds()); 
 
            // Set it into the image and fill again with red, notice 
            // that this will fill the whole image since 
            // the region is now empty 
            image.SetRegion(null, region, RasterRegionCombineMode.Set); 
 
            cmd = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Red)); 
            cmd.Run(image); 
 
            codecs.Save(image, destFileName2, RasterImageFormat.Bmp, 24); 
         } 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import static org.junit.Assert.assertTrue; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.FillCommand; 
 
 
public void rasterRegionExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1.cmp"); 
   String destFileName1 = combine(LEAD_VARS_IMAGES_DIR, "Image1_WithRegion1.bmp"); 
   String destFileName2 = combine(LEAD_VARS_IMAGES_DIR, "Image1_WithRegion2.bmp"); 
 
   RasterCodecs codecs = new RasterCodecs(); 
   // Load the source image 
   RasterImage image = codecs.load(srcFileName, 0, CodecsLoadByteOrder.BGR_OR_GRAY, 1, 1); 
 
   // Create a new rectangular RasterRegion 
   RasterRegion region = new RasterRegion(new LeadRect(20, 30, 100, 200)); 
 
   // Show this region properties 
   System.out.println("IsEmpty: " + region.isEmpty() + "\nBounds: " + region.getBounds()); 
 
   // Set it into the image 
   image.setRegion(null, region, RasterRegionCombineMode.SET); 
 
   // Fill the image with a color and save it to disk to show the region 
   FillCommand cmd = new FillCommand(RasterColor.fromKnownColor(RasterKnownColor.YELLOW)); 
   cmd.run(image); 
 
   codecs.save(image, destFileName1, RasterImageFormat.BMP, 24); 
   // Clear this region 
   region.makeEmpty(); 
 
   // Show its properties 
   System.out.println("IsEmpty: " + region.isEmpty() + "\nBounds: " + region.getBounds()); 
 
   // Set it into the image and fill again with red, notice 
   // that this will fill the whole image since 
   // the region is now empty 
   image.setRegion(null, region, RasterRegionCombineMode.SET); 
   cmd = new FillCommand(RasterColor.fromKnownColor(RasterKnownColor.RED)); 
   cmd.run(image); 
 
   codecs.save(image, destFileName2, RasterImageFormat.BMP, 24); 
 
   assertTrue("Image unsuccessfully saved", (new File(destFileName2)).exists()); 
   System.out.println("Command run and image saved to " + destFileName2); 
} 
Requirements

Target Platforms

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

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