I'm working to a software that need a strange feature. I choose a png image like image attached and I need to place uniformly a certain number of points on black surface. I started with loop each pixel and change it's color to black, only for design, but now I need to think an algorithm to fill it with points (like 200 points (pixels with red color). You have a idea how to do this ?
Now in my mind is to count black pixels, then do something like this blackPixelsPerPoint = blackPixels / numberOfPoints. After this I now i need to have a red point every blackPixelsPerPoint.
The result need to be something like N letter
The points need to have almost same space between them and fill all the black surface if is possible (depend by number of points).
I'm using a C# printdocument class. My printer's driver is set to print at 1000 DPI. I am placing strings down the page at various intervals. I am using a PageUnit of Pixel (so 1,000 pixels = 1 inch?) to place these DrawString()s down the paper. Higher in the paper, they appear fine; however as you get lower, they aren't quite as far down as they should be. By the bottom of the paper, it's off a quarter of an inch.
Any ideas where I should start to resolve this issue?
For example: the DrawString() that should be Y coordinates 4147 (pixles) down (at 1000 DPI, should be about 4.147 inchs down) is actually only about 4.010 inches down~.
I have strings formatted using the code below
String.Format("{0,-10} {1,10}", "Kills:", kills);
String.Format("{0,-10} {1,10}", "Points:", points);
String.Format("{0,-10} {1,10}", "$:", currency);
From what I understood, the first part of the strings should be left justified with a 10 space buffer and then the integer variables should be printed right justified with a 10 space buffer.
However when attempt to draw the strings using SpriteBatch.DrawString, nothing aligns properly.
The left aligned side prints properly, but the right aligned side centres on a certain point, for example if kills = 50 and points = 5002, the 50 will be centered over the 00 in the 5002...
What is going on?
Quite simply, I suspect you're not using a monospaced font. When different characters have different widths, you can't use spaces to line things up. (Your sample works when using Console.WriteLine for example, as the console has a fixed width font by default.)
You'll either have to use a monospaced font, or you'll have to draw the strings separately - draw each string to fit the relevant area. I don't know anything about XNA, but I'd expect you to either have to measure the width of the string before you draw it (so you can subtract it from the right-hand edge, for example) or specify some sort of layout value which indicates "right-align this string with a particular point".
Most likely you draw the text with a proportional font. Bear in mind that characters don't have the same width, so you cannot align texts with spaces.
As I do not have reply privileges, (or some such thing, as there is no reply button for answers), but I would like to contribute, I will post this answer.
Jon's answer mentioned measuring the string, this is possible by spriteFont.MeasureString(string s);. This returns a Vector2, the X portion of which is the width of the rendered text. (Y is height, which could be helpful for other things) This allows you to use a font other than a monospace font.
Here is an example of using MeasureString:
I'm not really sure what the question is asking, but if you wanted a single line of text similar to "Kills:50 Points:5002" but width two different spritebatch calls you could do the following (note I typed this directly into stackoverflow, so there may be minor syntax errors):
float killStringWidth = spriteFont.MeasureString(killString).X;
spriteBatch.DrawString(spriteFont, killString, new Vector2(0,0), Color.White );
spriteBatch.DrawString(spriteFont, pointString, new Vector2(killStringWidth + 10, 0), Color.White );
How does the font size work in sprite fonts in C# XNA? For example maybe font size 10 is drawn from the top to bottom of a lowercase charecter by default 10 pixels tall when output with SpriteBatch's drawString. I've noted that some charecters are much wider or taller than most, extending a considrable distance downwards or to the right of a normal charecter.
I believe it depends on the font. You can use SpriteFont.MeasureString to determine the rendered dimensions of text.
I'm building a Bitmap by layering images one above another, and when I'm done I want to write text around the edges. The top and bottom are simple because they're written horizontally, but I'd prefer to write the text on the left and right sides vertically so they don't take up as much space.
The Graphics.DrawString method doesn't allow you to specify an angle of rotation; what other methods exist?
I think you may get some pointers from this answer about rotating text for printing that I wrote a while ago.
Here is a tutorial that may help
http://www.c-sharpcorner.com/Blogs/BlogDetail.aspx?BlogId=580
I believe StringFormatFlags.DirectionVertical is what you are looking for
Maybe you can rotate the bitmap 90 degrees and write the text to the top of the bitmap - then rotate again and write the next side's text - this would give you text running CW/CCW around the edges.
If you want it horizontally across the top and bottom and vertically on the left and right, I suggest measuring (or assumning) the size of the largest character you need to write and then use this to position the drawn text - one character at a time - first on the left side then the right then moving down one char and repeating. You could of course just right the left side in totality and then the right side. just use the width of the char to inset from the right side and the height of the char to offset vertically between chars.
e.g.
1 2 OR 1 4 GIVING C D
3 4 2 5 A O
5 6 3 6 T G
Probably not the most elegant solution, but may help you.
You can use a logical font for this. But it's a pain - you're better off using Fredrik's answer (unless you're doing this in the Compact Framework, where RotateTransform isn't available).