Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.56/5 (4 votes)
See more:
How can I write text to a black and white image in C#?
Posted
Updated 3-Dec-09 21:11pm
v3

wrote:
Is it anyway to write text on a black & white image using C#.


Dynamically Write Text On An Image[^]

I was able to find this article by doing a simple search of the site.
 
Share this answer
 
You can write the text into an 24 bit image and then convert it to 1 bit like that:
How to convert a TTF character to an 8bpp BMP file[^]
 
Share this answer
 
Check out "Bitonal (TIFF) Image Converter for .NET". You can create a "bitonal" (i.e., monochrome) image from a byte array. How you fill that array with color data is up to you. You can load it from a bitonal file (you can also get the byte array data from an image) or you could draw a 32-bit image using a Graphics object then get the raw color data (in the byte array) and manually convert it to 1-bit.

The problem you are seeing when converting from color to 1-bit black and white is likely due to dithering. Dithering is intended to improve the quality of most natural images (e.g., scenery and portraits), but it can actually reduce the quality with computer generated images (e.g., text). When you do the manual conversion from color to 1-byte, just make sure to not dither. For more information on dithering, scroll to the bottom of this Wikipedia page and look at the images they give as examples of various forms of dithering.
 
Share this answer
 
v2
I've faced with the same problem now. ))) ('_')

Seems like yes, we should do it manually, but I do know how..

Will write the code and post later.. ;P
 
Share this answer
 
Hi, Thanks for the reply, but it is NOT the answer. The similar code are everywhere, but it doesn't work in my case. What I did is:

Bitmap myBitmap = new Bitmap(x, y, PixelFormat.Format1bppIndexed);
Graphics g = Graphics.FromImage(myBitmap);

the second line will throw an exception since the Graphics object cannot be created from an indexed image (B&W here). What I wanted to do is to write some text on this image and save it as G4 compressed TIFF. I can do this operation with a color image with the same resolution as screen (as the sample code you pointed), but in result big file which I don't want.

This limitation also shows that in C#, seems one cannot modify a B&W image using Graphics (it is not an option to work on a color image first, then convert it to B&W since it will lost a great deal of details even for text image, already tried).

Just wonder if any resolution in .NET or C# world, otherwise, I have to switch back to old C/C++ world which I'm free to do this.
 
Share this answer
 
v3
Corinna John's answer is to convert the image after drawing text. I've tried this method before I posted this question, the result is very poor, barely readable, which is logical, think about it, converting from 24bit color to 2 colors, how much details being lost, the text is not pure black in 24 bit image, and become broken after conversion, so this method is also not working. Some more it is slow since it is comparing pixel by pixel, I'm talking about 300dpi A3 size image here, and one million of them.

Abhishek Sur's answer is still repeating the previous answers. I know how to write text on image (true color or screen compitable) using C#, just don't know how to write text (or perform whatever GDI operation) on a black & white image using C#.

Anyway, thanks a lot for all the answers, appreciated! looks like there is no solution in C#. I had no choice but go ahead with Win32 C/C++, and it works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900