简体   繁体   中英

Solid black image after converting bitmap to monochrome on .NET Compact Framework

My goal is to take a picture using the camera on a Windows Mobile device and convert that image to a monochrome bitmap so it can print on a bluetooth printer using the .NET Compact Framework. I found the answer I was looking for here:

Convert image to 1 bpp bitmap in .net compact framework

When I added this code and tested with an image I took with the camera (.jpg file about 300K in size), I passed that image into the code from the code in the above stackoverflow solution and it completed with no errors. When viewing the bitmap, it is a solid black image.

Does the original bitmap passed into the code need to be a certain resolution or are there any other restrictions? The original image taken with the camera was black and white to begin with.

This is a tricky problem. What you really need to do to get reasonable results is implement a dithering algorithm that allows the printer to space white and black dots proportionally according to how "black" the original pixel was. If you limit your algorithm to converting each pixel to just white or black the output will not be a very good representation of the original photograph you took. With dithering, you analyse each pixel from the input and adjust the surrounding pixels on the output based on how dark the input pixel is.

A common dithering implementation is the Floyd-Steinberg algorithm, is described in it's basic form here:

Floyd-Steinberg Dithering

Lots more information is out there if you google for it.

As it's not exactly a trivial algorithm to implement, be prepared to spend a more than an afternoon to get it just right though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM