Reputation: 450
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.
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
Reputation: 1165
I would suggest to use the Graphics Object and The Bitmap class.
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:
Im drawing the big picture into the small one. The bottom part is cropped.
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:
Resulting picture:
Cheers Thomas
Upvotes: 1