4

I want to remove the white background color in a bitmap

Bitmap capcha = new Bitmap("C:/image.jpg");
pictureBox1.Image = capcha;

but I want to display in my pictureBox1 just the image without white that exists in the background

Ti Amo Laky
  • 755
  • 2
  • 8
  • 21

1 Answers1

11

try to set transparency in capcha like this:

Bitmap capcha = new Bitmap(@"C:/image.jpg");
capcha.MakeTransparent(Color.White);
pictureBox1.Image = capcha;

I hope it is what u need.

Kamil Nowak
  • 179
  • 4
  • 11
  • That'll give strange results on JPEG though, since it's full of compression artifacts that will be "not-quite-exactly-white" instead of "white". – Nyerguds Sep 21 '17 at 07:59
  • You are right, but question was 'how to remove white background'. You should avoid those 'not-quite-exactly-white' pixels or find other way. – Kamil Nowak Sep 22 '17 at 08:17
  • The problematic part here is of course, "white background"; the background on jpeg is _never_ just 'white', but you can assume they'll still want to remove _all_ of it. – Nyerguds Sep 25 '17 at 08:38
  • Apparently you are able to create .jpg image that has no intermediate pixels. You just need to adjust quality of your image (simple sample https://imgur.com/X4cQ1aZ) – Kamil Nowak Sep 27 '17 at 10:42
  • Gimp's "select by color" function set to zero-tolerance disagrees with you. Still plenty of rogue pixels in there, even on 100% quality. https://i.imgur.com/f45Sgeg.png – Nyerguds Sep 28 '17 at 09:01
  • It depends on you needs. For me several rogue pixels, even if exists is not a problem. It would be better if you propose other solution than complain about one which appears that fits (taking into account upvotes). – Kamil Nowak Sep 28 '17 at 10:28
  • 1
    Oh, I didn't mean to criticise; I was just making a remark that the use of the jpeg format complicates this operation. – Nyerguds Sep 28 '17 at 12:07