1

I am trying to write code that would remove the white background from jpeg-images, can save them in same format.

Here is my code so far:

string imgPath = Server.MapPath("~/App_Files/" + Profile.CompanyName + "/Temp/test.jpg");
Bitmap bmp = RADBase.ImageHandler.LoadImage(imgPath);
bmp.MakeTransparent(Color.White);
System.Drawing.Imaging.ImageFormat format = new System.Drawing.Imaging.ImageFormat(Guid.NewGuid());
bmp.Save(imgPath, format);

It removes the white background, but there are still white edges on the image. not sure how to get the complete white background removed?

Here is the image: http://cyphernet.co.za/bg-test/CGU3-black.jpg

Here is the result: http://cyphernet.co.za/bg-test/test.jpg

daniel59
  • 906
  • 2
  • 14
  • 31
CypherNet
  • 53
  • 1
  • 10
  • 1
    You just remove one white color `FFFFFF` (RGB-value), but there are also other colors that look white and their RGB-value ist different, for example `FFFFFE` – daniel59 May 03 '16 at 12:39
  • Definitely not easy, and I think hardly in scope for Stack Overflow... Still, see these questions+answers: http://stackoverflow.com/questions/5875035/remove-white-edges-from-image-c-sharp and http://stackoverflow.com/questions/2898050/how-to-determine-edges-in-an-image-optimally. – Peter B May 03 '16 at 12:44
  • Thanks for the suggestion @Fuzzi59 I'm going to give your solution below a try and see how it goes. will give you feedback there. – CypherNet May 03 '16 at 16:23

2 Answers2

3

One way would be to create a range of colors, which look white and check each pixel if it is a "white". Then you can set these pixels to Color.Transparent:

string imgPath = Server.MapPath("~/App_Files/" + Profile.CompanyName + "/Temp/test.jpg");
Bitmap source = new Bitmap(imgPath);
source.MakeTransparent();
for (int x = 0; x < source.Width; x++)
{
    for (int y = 0; y < source.Height; y++)
    {
        Color currentColor = source.GetPixel(x, y);
        if (currentColor.R >= 220 && currentColor.G >= 220 && currentColor.B >= 220)
        {
            source.SetPixel(x, y, Color.Transparent);
        }
    }
}
source.Save(imgPath);

This is a rough solution, because it is hard say, if one color is white or not. Maybe there you have to adjust the range for better result. Another point is that also some colors of the t-shirt look white, so these will be transparent too.

daniel59
  • 906
  • 2
  • 14
  • 31
-4

you should clean all white catefories, You can see categories below link. You can see especially grey and white categories (grey category = gri kategorisi, white category = beyaz category). You can remove colors with their RBG values.

Here Categories

SedatD
  • 1
  • 4