Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 648273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:48:50+00:00 2026-05-13T21:48:50+00:00

Any help most appreciated, this is driving me insane. I’m doing exactly what I

  • 0

Any help most appreciated, this is driving me insane. I’m doing exactly what I can find from google etc. and it’s not working.

I’m having a lot of difficulty creating and writing BMP or PNG images in java. Either would do, I was going to go with BMP but I get an image which is correct in the lower section for what I want but the upper section is just a black block a lot of the time (not always).

With any other file format, including PNG, the resulting file is completely corrupted. Java says PNG, BMP and others are supported on my system according to the filetypessupported method.

If you’re wondering what it’s for, I’m trying to draw a representation of an area based on a robot’s sensors.

And if you’re wondering why I’m not using Graphics2d to draw a rectangle rather than doing it pixel by pixel, Graphics2d appears to be drawing rectangles that are bigger than what I ask for.

Here is my code for if I were to write a PNG (with a lot snipped out) and the bmp writing
commented out:

private BufferedImage bufImage = null;
private ImageIO imageIO = null;
private int blobSize = 5;
private String pngPath = f.getAbsolutePath() + "test.png";
private java.io.File f = new java.io.File("");
private String bmpPath = f.getAbsolutePath() + "test.bmp";

Map(int sizeX, int sizeY){
    bufImage = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGB);
    //set all pixels of bufImage to white
    for (int x = 0; x < sizeX; x++){
        for (int y = 0; y < sizeY; y++){
            bufImage.setRGB(x, y, Color.WHITE.getRGB());
        }
    }
    saveBMP();
}
public void addObstacle(int x, int y, int fiducial){        
    //invert y (y == 0 is upper left on bitmap)
    y = 1674 - y;

    //10*10 pixels
    for (int w = x; w < x + blobSize && w < sizeX && w >= 0 ; w++){
        for (int h = y; h < y + blobSize && h < sizeY && h >= 0; h++){
            try{
                bufImage.setRGB(w, h, Color.BLACK.getRGB());
                //System.out.println(h);
            }
            catch (ArrayIndexOutOfBoundsException e){
                System.out.println("x: " + w + " y: " + h);
            }
            //System.out.println(h);
        }
    }
    //bufImage.setRGB(x, y, Color.BLACK.getRGB());
    saveBMP();
}
public void saveBMP(){
    try {
        RenderedImage rendImage = bufImage;
        //ImageIO.write(rendImage, "bmp", new File(bmpPath));
        ImageIO.write(rendImage, "PNG", new File(pngPath));
        //ImageIO.write(rendImage, "jpeg", new File(jpegPath));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:48:50+00:00Added an answer on May 13, 2026 at 9:48 pm

    Maybe you could start with something that works and work from there?

    Note that for better performances you should create a “compatible” BufferedImage:

    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width,height,Transparency.TRANSLUCENT)
    

    whose “ARGB” shall depend on your OS.

    Here’s a code that creates a 320×160 all white .png file based on your code.

    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.IOException;
    
    public class SO {
    
        public static void main( final String[] args ) {
            final BufferedImage img = map( 320, 160 );
            savePNG( img, "/tmp/test.png" );
        }
    
        private static BufferedImage map( int sizeX, int sizeY ){
            final BufferedImage res = new BufferedImage( sizeX, sizeY, BufferedImage.TYPE_INT_RGB );
            //set all pixels of bufImage to white
            for (int x = 0; x < sizeX; x++){
                for (int y = 0; y < sizeY; y++){
                    res.setRGB(x, y, Color.WHITE.getRGB() );
                }
            }
            return res;
        }
    
        private static void savePNG( final BufferedImage bi, final String path ){
            try {
                RenderedImage rendImage = bi;
                //ImageIO.write(rendImage, "bmp", new File(bmpPath));
                ImageIO.write(rendImage, "PNG", new File(path));
                //ImageIO.write(rendImage, "jpeg", new File(jpegPath));
            } catch ( IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    

    Note that setRGB(…) always work in ARGB so:

      res.setRGB(x, y, Color.WHITE.getRGB() );
    

    and:

      res.setRGB(x, y, 0xFFFFFFFF );
    

    are equivalent.

    There are more efficient way to fill a solid background but knowing how to manipulate pixel ARGB components directly may come in handy once speed is needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any help here would be greatly appreciated. I have this table hospital Nurse |
Thanks in advance for any help you can provide on this issue! I manage
Any help would be appreciated, I'm trying to convert the code below to C#,
Any help is appreciated. When using cfimage to generate a captcha, we keep his
Any help at all is appreciated here folks. I'm building a web app in
Any help would be appreciated. I am looking for a way to programatically notified
Any help with this problem would be fantastic. I appreciate all contributions! Let us
I would appreciate any help on this issue. Lets say I want to load
This is probably quite simple but I've googled and can't find an answer. I
I can upload a single file without any issues but now I find that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.