C# images cropping,splitting,saving

11,076

Depending on the .NET version, you could do one of the following to crop:

.NET 2.0

private static Image cropImage(Image img, Rectangle cropArea)
{
   Bitmap bmpImage = new Bitmap(img);
   Bitmap bmpCrop = bmpImage.Clone(cropArea,
   bmpImage.PixelFormat);
   return (Image)(bmpCrop);
}

Or .NET 3.5+

// Create an Image element.
Image croppedImage = new Image();
croppedImage.Width = 200;
croppedImage.Margin = new Thickness(5);

// Create a CroppedBitmap based off of a xaml defined resource.
CroppedBitmap cb = new CroppedBitmap(     
   (BitmapSource)this.Resources["masterImage"],
   new Int32Rect(30, 20, 105, 50));       //select region rect
croppedImage.Source = cb;                 //set image source to cropped

As you can see, it's a bit more simple than what you're doing. The first example clones the current image and takes a subset of it; the second example uses CroppedBitmap, which supports taking a section of the image right from the constructor.

The splitting part is simple maths, just splitting the image into 9 sets of coordinates and passing them into the constructor.

Share:
11,076
cheesebunz
Author by

cheesebunz

:D

Updated on June 30, 2022

Comments

  • cheesebunz
    cheesebunz almost 2 years

    as stated in subject, i have an image:

        private Image testing;
        testing = new Bitmap(@"sampleimg.jpg");
    

    I would like to split it into 3 x 3 matrix meaning 9 images in total and save it.Any tips or tricks to do this simple? I'm using visual studios 2008 and working on smart devices. Tried some ways but i can't get it. This is what i tried:

            int x = 0;
            int y = 0;
            int width = 3;
            int height = 3;
    
    
            int count = testing.Width / width;
            Bitmap bmp = new Bitmap(width, height);
    
    
            Graphics g = Graphics.FromImage(bmp);
    
    
            for (int i = 0; i < count; i++)
            {
                g.Clear(Color.Transparent);
                g.DrawImage(testing, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
                bmp.Save(Path.ChangeExtension(@"C\AndrewPictures\", String.Format(".{0}.bmp",i)));
                x += width;
            } 
    
    • cheesebunz
      cheesebunz almost 14 years
      i tried this too : string path = @"C\AndrewPictures"; bmp.Save(Path.ChangeExtension(path, String.Format(".{0}.bmp",i))); but i get Error 1 No overload for method 'Save' takes '1' arguments
  • allonym
    allonym almost 14 years
    +1 for cropping via Bitmap.Clone, it's what I'm using to extract small regions out of images for ROI display and it works well.
  • cheesebunz
    cheesebunz almost 14 years
    yes i've gone to this website switchonthecode.com/tutorials/… I think i'll stick to my method, just that i can't save the images...
  • cheesebunz
    cheesebunz almost 14 years
    And by croppedImage.Margin = new Thickness(5); does it means height? i do not have the method, am i missing some reference?