1

I've written a photo viewer, and I want to superimpose text over the photo. I want the font or font color to make the text as legible as possible on top of the bitmap, no matter what the underlying bitmap looks like.

My current thinking is to take the region of the bitmap where the text will appear, and make some kind of "overall color" calculation for that area, and then set the font color to be something correspondingly contrasting.

However, this math is way over my head. Has anybody seen a method for making this type of "what's the average color of all of these pixels" calculation? Or is that not even the best approach?

EDIT: I'm moving the second portion of this to another question.

Cardinal Fang
  • 283
  • 2
  • 12

1 Answers1

3

You can use this to calculate average color of a region of bitmap: How to calculate the average rgb color values of a bitmap Do you store your image as a Bitmap?

You can also draw an outlined text. For example, white text with black outline. This will make text visible on most of backgrounds: How to Drawing Text with Outline onto Images?

Community
  • 1
  • 1
sasha_gud
  • 1,635
  • 13
  • 18
  • I probably should have mentioned that the text will not be very big; it's essentially image metadata. So I suspect the outline font won't work. However, that's a very useful post, and I bookmarked it :-) – Cardinal Fang Jul 31 '14 at 09:09
  • As for the "do I store the image as a bitmap", I'm pretty flexible. I read the image from file and assign it to a PictureBox image. If I can get a good calculation, I'll just throw a transparent label on the picturebox and set the font color correspondingly. – Cardinal Fang Jul 31 '14 at 09:14
  • This is great! I stole Till's code from http://stackoverflow.com/a/6185448/3784949. Looks like it's exactly what I need. Now... uh, how do I grab a region of the original bitmap and turn that into a bitmap I can feed to Till's code? I have the coordinates... just not sure how to make another bitmap from them... – Cardinal Fang Jul 31 '14 at 09:31
  • I think you can just modify that code a bit. Just pass `Rectangle` as a parameter for `CalculateAverageColor` function and modify `x` and `y` loops to loop from `Rectangle.Left` and `Rectangle.Top` to `Rectangle.Right` and `Rectangle.Bottom` correspondingly. – sasha_gud Jul 31 '14 at 11:15