Hi,

Can someone tell me how can i edit the color of a pixel in a bmp image.

The actual scenario is that i have a white bmp image and i take input from the user. If user enter 'A' the i have to edit the white color pixels in such a way that A appears red in the center of the image

Thanks

Dani AI

Generated

Quick, practical approach for this exact task (paint a red "A" centered in a white BMP)

As described, the easiest route is to load the BMP into a BufferedImage, draw a red character in the center with Java2D, then save the image. is right that an external BMP-aware library can help if your runtime lacks BMP writers, but most of the work is handled by Java classes: read with ImageIO, draw with Graphics2D and FontMetrics (to center the glyph), then write out.

Example workflow (works in modern JDKs)

  1. Read the BMP with ImageIO.read(...).
  2. If the image is indexed/paletted, convert it to TYPE_INT_ARGB or TYPE_INT_RGB to avoid palette/quantization issues.
  3. Create a Graphics2D, enable text antialiasing, set Color.RED and an appropriate Font.
  4. Use FontMetrics to compute x and y so the glyph is centered, then drawString.
  5. Dispose the Graphics2D and write the result with ImageIO.write(...).

Sample Java snippet:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

BufferedImage img = ImageIO.read(new File("in.bmp"));
if (img.getType() != BufferedImage.TYPE_INT_ARGB && img.getType() != BufferedImage.TYPE_INT_RGB) {
    BufferedImage tmp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D tg = tmp.createGraphics();
    tg.drawImage(img, 0, 0, null);
    tg.dispose();
    img = tmp;
}

Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.RED);
Font font = new Font("SansSerif", Font.BOLD, 72);
g.setFont(font);
FontMetrics fm = g.getFontMetrics();
String text = "A";
int x = (img.getWidth() - fm.stringWidth(text)) / 2;
int y = (img.getHeight() - fm.getHeight()) / 2 + fm.getAscent();
g.drawString(text, x, y);
g.dispose();

ImageIO.write(img, "bmp", new File("out.bmp"));

Troubleshooting and extra notes

  • If ImageIO.write(..., "bmp", ...) fails on your JRE, install the TwelveMonkeys ImageIO plugin for robust BMP read/write support (TwelveMonkeys).
  • For precise centering and measurement see FontMetrics and Graphics2D docs (FontMetrics, Graphics2D).
  • If you must change only white pixels that form the letter (not draw over the image), render the glyph to an alpha mask (GlyphVector or offscreen BufferedImage), iterate pixels, and replace only those white pixels under the mask. This is slightly more work but gives pixel-level control.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

Hi,

Can someone tell me how can i edit the color of a pixel in a bmp image.

The actual scenario is that i have a white bmp image and i take input from the user. If user enter 'A' the i have to edit the white color pixels in such a way that A appears red in the center of the image

Thanks

Use an external bmp library and link it to your project

Hi,

Thanks for the reply, but your reply is so brief that i didn't come to know that exactly what can i do. Can u please explain your point.

Regards

Use an external bmp library and link it to your project

Hi,

Thanks for the reply, but your reply is so brief that i didn't come to know that exactly what can i do. Can u please explain your point.

Regards

Member Avatar for Member #46692

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.