Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 GDI
How can I write text to a black and white image in C#?
Posted 3-Dec-09 18:59pm
rjtrex350
Edited 3-Dec-09 22:11pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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.. Poke tongue | ;-P
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100