I’m working in an application to take photographies from a VideoView
control provided by LibVLCSharp
, and this part works good.
The user can decide to take a photo of what is appearing in the control, and after that, the photo gets post processed removing its background, and the problem comes from this action.
I’ve been trying with some code, and the best for me is the following one:
private static int RemoveBackground(string pathToPhoto)
{
Bitmap bmp = new(pathToPhoto);
//Ireate by corrdinates
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color currentColor = bmp.GetPixel(x, y);
//Establish the RGB colour range to remove pixels from
if (currentColor.R >=130 && currentColor.R <= 180 && currentColor.G >= 120 && currentColor.G <= 160 && currentColor.B >= 105 && currentColor.B <= 140)
bmp.SetPixel(x, y, Color.Transparent);
}
}
try
{
bmp.Save(pathToPhoto.Replace("jpg", "png"), ImageFormat.Jpeg);
bmp.Dispose();
return 1;
}
catch (Exception)
{
return 0;
}
}
With this method most of the background gets removed, but as you can see on the image below, some pixels still there.
Do you know about any NuGet package
or other native C#
ways to make my program capable of removing the whole background?
1