Sanjay Bathre
Sanjay Bathre

Reputation: 450

how to remove a section of image and move below part up in C#

I have a image in which i have the rectangle dimension of its which is needed to be removed from the image, how i can do that using the image processing library available in C#, or you have any kind of reference for this requirement please let me know and help me i am attaching the image for the problem statement.enter image description here

i have A image and i want B image after processing, how can i do that please help me. thanks in advance

Upvotes: 1

Views: 970

Answers (1)

Thomas Voß
Thomas Voß

Reputation: 1165

I would suggest to use the Graphics Object and The Bitmap class.

  1. Load your source Bitmap into Memory.
  2. Create a new Bitmap with the right height
  3. Create Graphics instances from both of your Bitmaps https://msdn.microsoft.com/de-de/library/system.drawing.graphics.fromimage(v=vs.110).aspx
  4. Draw the source Bitmap to the destination Bitmap https://msdn.microsoft.com/de-de/library/558kfzex(v=vs.110).aspx
  5. Draw a filled Rectangle with Color.Transparent to the source Bitmap. It must be located at 0/0 and must have the size x4/y4. https://msdn.microsoft.com/de-de/library/c6ksfcek(v=vs.110).aspx
  6. Draw the source bitmap to the destination picture again, but this time you locate it at 0/-(y3-y1)
  7. Finally save the Bitmap to a file again or do whatever you want to do with it.

An important setting for this approch is the Graphics.ComposingMode. For the first draw to the destination and the source bitmap it must be set to SourceCopy. For the second draw to the destination it must be set to SourceOver. https://msdn.microsoft.com/de-de/library/system.drawing.drawing2d.compositingmode(v=vs.110).aspx

Additionally both bitmaps must have a pixelFormat with Alpha channel e.g. Format32bppArgb https://msdn.microsoft.com/de-de/library/system.drawing.imaging.pixelformat(v=vs.110).aspx

So what am I doing:

  1. Im drawing the big picture into the small one. The bottom part is cropped.

  2. I make the upper part transparent and draw the picture with a negative y coordinate to the destination. By this the bottom part of the picture is pasted over the remaining visible part from the first draw.

If you have any problem with this introduction, feel free to ask.

EDIT: I felt like trying myself... Here's the working code:

        int cropFromY = 240;
        int cropToY = 334;

        // Change location if required...
        using (FileStream fs = new FileStream(@"D:\Test\pic.png", FileMode.Open, FileAccess.Read))
        {
            // Step 1
            // load Bitmap from File
            var loadedBitmap = new Bitmap(fs);

            // Step 1 (extended)
            // As the loadedBitmap might have a wrong PixelFormat, we convert it to have an Alpha channel
            var source = new Bitmap(loadedBitmap.Width, loadedBitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using (Graphics gSource = Graphics.FromImage(source))
            {
                gSource.DrawImageUnscaled(loadedBitmap, 0, 0);
            }

            // Step 2
            // Create destination Picture
            var destination = new Bitmap(source.Width, source.Height - (cropToY - cropFromY), System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // Step 3
            // Create the two graphics objects
            using (Graphics gSource = Graphics.FromImage(source))
            {
                using (Graphics gDestination = Graphics.FromImage(destination))
                {
                    // Step 4
                    // Copy picture source to destination => Crop Bottom
                    gDestination.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                    gDestination.DrawImageUnscaled(source, 0, 0);

                    // Step 5
                    // Make the top part (which is already drawn to the destination) transparent
                    gSource.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                    gSource.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, source.Width, cropToY));

                    // Step 6
                    // Draw the source bitmap to the destination picture again, but this time you locate it at 0/-(y3-y1)
                    gDestination.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
                    gDestination.DrawImageUnscaled(source, 0, -(cropToY - cropFromY));

                    // Step 7 
                    // Save new picture. Change location if required...
                    destination.Save(@"D:\Test\pic2.png");
                }
            }
        }

Original:

Original picture

Resulting picture:

Resulting picture

Cheers Thomas

Upvotes: 1

Related Questions