Extract sub image from an Image using c#

Extract sub image from an Image using c#

In C#, you can extract a sub-image from an image using the Bitmap class and its Clone method to create a copy of the original image with a specific rectangular area.

Here's an example of how to extract a sub-image from an image:

using System.Drawing; // Load the original image Bitmap originalImage = new Bitmap("original_image.jpg"); // Define the rectangular area of the sub-image Rectangle subImageRect = new Rectangle(100, 100, 200, 200); // Create a new bitmap with the sub-image Bitmap subImage = originalImage.Clone(subImageRect, originalImage.PixelFormat); 

In this example, we first load the original image using the Bitmap class.

We then define a rectangular area for the sub-image using the Rectangle class. The rectangular area is specified by its top-left corner coordinates (100, 100) and its width and height (200x200).

Finally, we create a new bitmap with the sub-image using the Clone method of the original image. The Clone method takes two parameters: the rectangular area of the sub-image and the pixel format of the new bitmap. We pass the subImageRect and originalImage.PixelFormat parameters to create a new bitmap with the same pixel format as the original image.

The resulting subImage bitmap will contain the sub-image extracted from the original image.

Note that the Clone method creates a new bitmap with a copy of the specified rectangular area, so modifying the sub-image bitmap will not affect the original image. Also, make sure to dispose of the original and sub-image bitmaps properly after use, to release their resources and avoid memory leaks.

Examples

  1. "C# extract region from an image"

    • Description: Learn how to extract a specific region or sub-image from an image in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); Rectangle regionToExtract = new Rectangle(10, 10, 100, 100); Bitmap extractedImage = ExtractRegion(originalImage, regionToExtract); 
    public static Bitmap ExtractRegion(Bitmap original, Rectangle region) { Bitmap extractedImage = original.Clone(region, original.PixelFormat); return extractedImage; } 
  2. "C# crop image region using Graphics"

    • Description: Explore code examples for cropping a specific region from an image using Graphics in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); Rectangle regionToCrop = new Rectangle(20, 20, 150, 150); Bitmap croppedImage = CropImageWithGraphics(originalImage, regionToCrop); 
    public static Bitmap CropImageWithGraphics(Bitmap original, Rectangle region) { Bitmap croppedImage = new Bitmap(region.Width, region.Height); using (Graphics g = Graphics.FromImage(croppedImage)) { g.DrawImage(original, 0, 0, region, GraphicsUnit.Pixel); } return croppedImage; } 
  3. "C# extract part of an image by coordinates"

    • Description: Learn how to extract a part of an image based on specific coordinates in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); int startX = 30, startY = 30, width = 120, height = 120; Bitmap extractedImage = ExtractPartByCoordinates(originalImage, startX, startY, width, height); 
    public static Bitmap ExtractPartByCoordinates(Bitmap original, int startX, int startY, int width, int height) { Rectangle regionToExtract = new Rectangle(startX, startY, width, height); Bitmap extractedImage = original.Clone(regionToExtract, original.PixelFormat); return extractedImage; } 
  4. "C# get sub-image using Bitmap.Clone"

    • Description: Explore code examples for using Bitmap.Clone to get a sub-image from an original image in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); Rectangle subImageBounds = new Rectangle(40, 40, 80, 80); Bitmap subImage = GetSubImageWithClone(originalImage, subImageBounds); 
    public static Bitmap GetSubImageWithClone(Bitmap original, Rectangle bounds) { Bitmap subImage = original.Clone(bounds, original.PixelFormat); return subImage; } 
  5. "C# extract image region with LockBits"

    • Description: Learn how to use LockBits for extracting a region from an image in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); Rectangle regionToExtract = new Rectangle(50, 50, 100, 100); Bitmap extractedImage = ExtractRegionWithLockBits(originalImage, regionToExtract); 
    public static Bitmap ExtractRegionWithLockBits(Bitmap original, Rectangle region) { Bitmap extractedImage = new Bitmap(region.Width, region.Height); using (Graphics g = Graphics.FromImage(extractedImage)) { g.DrawImage(original, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel); } return extractedImage; } 
  6. "C# crop image sub-region with CopyTo"

    • Description: Explore code examples for cropping a sub-region from an image using CopyTo in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); Rectangle subRegion = new Rectangle(60, 60, 90, 90); Bitmap croppedImage = CropSubRegionWithCopyTo(originalImage, subRegion); 
    public static Bitmap CropSubRegionWithCopyTo(Bitmap original, Rectangle region) { Bitmap croppedImage = new Bitmap(region.Width, region.Height); using (Graphics g = Graphics.FromImage(croppedImage)) { g.DrawImage(original, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel); } return croppedImage; } 
  7. "C# extract image portion by percentage"

    • Description: Learn how to extract a portion of an image based on percentage dimensions in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); double startXPercentage = 10, startYPercentage = 10, widthPercentage = 30, heightPercentage = 30; Bitmap extractedImage = ExtractPortionByPercentage(originalImage, startXPercentage, startYPercentage, widthPercentage, heightPercentage); 
    public static Bitmap ExtractPortionByPercentage(Bitmap original, double startXPercentage, double startYPercentage, double widthPercentage, double heightPercentage) { int startX = (int)(original.Width * (startXPercentage / 100.0)); int startY = (int)(original.Height * (startYPercentage / 100.0)); int width = (int)(original.Width * (widthPercentage / 100.0)); int height = (int)(original.Height * (heightPercentage / 100.0)); Rectangle regionToExtract = new Rectangle(startX, startY, width, height); Bitmap extractedImage = original.Clone(regionToExtract, original.PixelFormat); return extractedImage; } 
  8. "C# crop image sub-region with GraphicsPath"

    • Description: Explore code examples for cropping a sub-region from an image using GraphicsPath in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); Rectangle subRegion = new Rectangle(70, 70, 80, 80); Bitmap croppedImage = CropSubRegionWithGraphicsPath(originalImage, subRegion); 
    public static Bitmap CropSubRegionWithGraphicsPath(Bitmap original, Rectangle region) { Bitmap croppedImage = new Bitmap(region.Width, region.Height); using (Graphics g = Graphics.FromImage(croppedImage)) using (GraphicsPath path = new GraphicsPath()) { path.AddRectangle(new Rectangle(0, 0, region.Width, region.Height)); g.SetClip(path); g.DrawImage(original, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel); } return croppedImage; } 
  9. "C# extract image sub-section with Pixel manipulation"

    • Description: Learn how to extract a sub-section from an image using pixel manipulation in C#.
    Bitmap originalImage = new Bitmap("original.jpg"); Rectangle subSection = new Rectangle(80, 80, 70, 70); Bitmap extractedImage = ExtractSubSectionWithPixelManipulation(originalImage, subSection); 
    public static Bitmap ExtractSubSectionWithPixelManipulation(Bitmap original, Rectangle region) { Bitmap extractedImage = new Bitmap(region.Width, region.Height); for (int x = 0; x < region.Width; x++) { for (int y = 0; y < region.Height; y++) { extractedImage.SetPixel(x, y, original.GetPixel(region.X + x, region.Y + y)); } } return extractedImage; } 

More Tags

top-n qcheckbox string-search esapi pose-estimation dart macos-sierra binning wireshark flutter-container

More C# Questions

More Everyday Utility Calculators

More Cat Calculators

More Internet Calculators

More Bio laboratory Calculators