←Select platform

AddColorToRegion Method

Summary
Creates or updates the image region by adding a region that consists of all the pixels of a specified color.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void AddColorToRegion( 
   RasterColor color, 
   RasterRegionCombineMode combineMode 
) 
- (BOOL)addColorToRegion:(LTRasterColor *)color  
             combineMode:(LTRasterRegionCombineMode)combineMode  
                   error:(NSError **)error 
public void addColorToRegion( 
  RasterColor color,  
  RasterRegionCombineMode combineMode 
) 
public: 
void AddColorToRegion(  
   RasterColor color, 
   RasterRegionCombineMode combineMode 
)  
def AddColorToRegion(self,color,combineMode): 

Parameters

color
Specifies the color to use for the region.

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

Remarks

You can use this method to simulate the use of a transparent color as follows:

  1. Call AddColorToRegion, with the transparent color in the  color parameter and RasterRegionCombineMode.SetNot in the combineMode parameter.
  2. Call PaintRegion to paint the resulting region, which includes everything in the image, except the transparent color.

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 RasterRegionCombineMode.

The AddColorToRegion function can use the Extended Grayscale mask. For more information, refer to Grayscale Images

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.

This method supports unsigned data images, but not signed ones.

In the Document and Medical toolkits, the COLORREF value can represent a 16 bit grayscale value if RasterImage is a 12 or 16-bit grayscale bitmap. To avoid confusion with an RGB value, set the COLORREF_GRAY16 mask (0x04000000). In this case (0x0400YYYY), the lower 16 bits (0xYYYY) of the COLORREF value represent the 16-bit grayscale value. (0x0400FFFF is 16-bit white and 0x04000000 is 16-bit black.) This is not a standard Windows value. Therefore, although LEADTOOLS methods will recognize a COLORREF having this format, but Windows methods will not.

Example

This example will load an image, adds a region all pixels of a specified color. It then fills the region with blue before saving it back to disk.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Core; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Dicom; 
using Leadtools.Drawing; 
using Leadtools.Controls; 
using Leadtools.Svg; 
 
 
public void AddColorToRegionExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_AddColorToRegion.bmp"); 
 
   // Load the image 
   RasterImage image = codecs.Load(srcFileName); 
 
   // Posterize the image to decrease the number of colors 
   PosterizeCommand posterize = new PosterizeCommand(16); 
   posterize.Run(image); 
 
   // Specify a pixel in the upper left of the displayed image 
   LeadPoint pt = new LeadPoint(image.ImageWidth / 8, image.ImageHeight / 8); 
 
   // Adjust the point in case the view perspective is not TopLeft 
   pt = image.PointToImage(RasterViewPerspective.TopLeft, pt); 
 
   // Get the color of the specified pixel 
   RasterColor regionColor = image.GetPixelColor(pt.Y, pt.X); 
 
   // Create a region that includes all pixels of that color 
   image.AddColorToRegion(regionColor, RasterRegionCombineMode.Set); 
 
   // Fill the region with blue 
   FillCommand fill = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Blue)); 
   fill.Run(image); 
 
   // Save the image 
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24); 
 
   image.Dispose(); 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.core.*; 
import leadtools.svg.*; 
import leadtools.imageprocessing.CloneCommand; 
import leadtools.imageprocessing.FillCommand; 
import leadtools.imageprocessing.FlipCommand; 
import leadtools.imageprocessing.GrayscaleCommand; 
import leadtools.imageprocessing.color.InvertCommand; 
import leadtools.imageprocessing.color.PosterizeCommand; 
 
 
public void addColorToRegionExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1.cmp"); 
   String destFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1_AddColorToRegion.bmp"); 
 
   // Load the image 
   RasterImage image = codecs.load(srcFileName); 
 
   // Posterize the image to decrease the number of colors 
   PosterizeCommand posterize = new PosterizeCommand(16); 
   posterize.run(image); 
 
   // Specify a pixel in the upper left of the displayed image 
   LeadPoint pt = new LeadPoint(image.getImageWidth() / 8, image.getImageHeight() / 8); 
 
   // Adjust the point in case the view perspective is not TopLeft 
   pt = image.pointToImage(RasterViewPerspective.TOP_LEFT, pt); 
 
   // Get the color of the specified pixel 
   RasterColor regionColor = image.getPixelColor(pt.getY(), pt.getX()); 
 
   // Create a region that includes all pixels of that color 
   image.addColorToRegion(regionColor, RasterRegionCombineMode.SET); 
 
   // Fill the region with blue 
   FillCommand fill = new FillCommand(RasterColor.fromKnownColor(RasterKnownColor.BLUE)); 
   fill.run(image); 
   assertTrue(fill.getColor().equals(RasterColor.fromKnownColor(RasterKnownColor.BLUE))); 
 
   // Save the image 
   codecs.save(image, destFileName, RasterImageFormat.BMP, 24); 
 
   // Clean up 
   image.dispose(); 
   codecs.dispose(); 
 
   assertTrue("file unsuccessfully saved to " + destFileName, (new File(destFileName)).exists()); 
   System.out.printf("File saved successfully to %s%n", destFileName); 
} 
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.