Drawing a contrast line in an image

So, I have a snapshot of the video source that I get into the image, grab a Graphics object for it, and then draw a time stamp in the lower right corner of the image. There are no problems so far. However, I can’t guarantee what color the text will be behind, so no matter which brush I use, it will almost certainly come across some of the images on which it is drawn, making the text unreadable.

I am wondering if anyone knows of a method (either a method in .net or a good algorithm) to determine the best color for a line based on the image behind it.

Greetings

+3
source share
6 answers
 just draw the string 5 times.
 One time 1(or2) pixels to the left in black
 One time 1(or2) pixels to the right in black
 One time 1(or2) pixels above it in black
 One time 1(or2) pixels below it in black
 and the final time in white on the place where you want it
+7

- .

+2

64 , , - , XOR. " ".

, ControlPaint.DrawReversibleLine, .

CodeProject , XOR, interop to gdi32.dll.

+1

, , ( ) (, ).

, ( ), , median .

( - L1 ( , ) - L2), L2, , "" L1, , " ".

0

.

  • ( , ), , .
  • : , / , . , , , .
  • , , , , ( ), , , , "" .
  • Release the whole “draw text by pixel using API calls” approach and use advanced multi-layer layout methods, such as those available in the WPF design.

For some examples of the latter option, see slides 18 and 21 in OSM Advanced Mapping on SlideShare.

0
source

The following snippet shows how to invert the color (background), and then applies the Dinah clause to create the background using Graphics.DrawString ().

private static Color InvertColor(Color c)
{
  return Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B);
}
// In the following, constants and inplace vars can be parameters in your code
const byte ALPHA = 192;
var textColor = Color.Orange;
var textBrush = new SolidBrush(Color.FromArgb(ALPHA, textColor));
var textBrushBkg = new SolidBrush(Color.FromArgb(ALPHA, InvertColor(textColor)));
var font = new Font("Tahoma", 7);
var info = "whatever you wanna write";
var r = new Rectangle(10, 10, 10, 10);

// write the text
using (var g = Graphics.FromImage(yourBitmap))
{
  g.Clear(Color.Transparent);
  // to avoid bleeding of transparent color, must use SingleBitPerPixelGridFit
  g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
  // Draw background for text
  g.DrawString(info, font, textBrushBkg, r.Left - 1, r.Top - 1);
  g.DrawString(info, font, textBrushBkg, r.Left + 1, r.Top + 1);
  g.DrawString(info, font, textBrushBkg, r.Left + 1, r.Top - 1);
  g.DrawString(info, font, textBrushBkg, r.Left - 1, r.Top + 1);
  // Draw text
  g.DrawString(info, font, textBrush, r.Left, r.Top);
}
0
source

Source: https://habr.com/ru/post/1713737/


All Articles