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

  • Home
  • SEARCH
  • 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 546499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:52:24+00:00 2026-05-13T10:52:24+00:00

I am trying to create a transparent PNG image from a BufferedImage in Java.

  • 0

I am trying to create a transparent PNG image from a BufferedImage in Java.

The PNG will be loaded into another piece of the software, that does not support the alpha channel.

That should be fine, because, according to Chapter 8, section 5, part 4 of the PNG book, I can achieve transparency by specifying a pixel value to be transparent. This works by creating a tRNS header in the png file.

I am unsure how to translate this technical detail to Java code. The actual image itself is monochrome; each pixel is going to be black or white. I would like to replace each white pixel with a transparent pixel, without using the alpha channel. May someone push me in the right direction please?

  • 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-13T10:52:24+00:00Added an answer on May 13, 2026 at 10:52 am

    You can use the following code.

    Create a monochrome and transparent BufferedImage:

    public static BufferedImage createTransparentMonochromeBufferedImage(int w, int h)
    {
        // The color map contains the colors black and white
        byte[] cMap = {0, 0, 0, (byte)255, (byte)255, (byte)255};
        // Create an IndexColorModel setting white as the transparent color
        IndexColorModel monochrome = new IndexColorModel(8, 2, cMap, 0, false, 1);
        // Return a new BufferedImage using that color model 
        return new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, monochrome);
    }
    

    Save the BufferedImage in a PNG file:

    public static void saveBufferedImageAsPNG(BufferedImage img, String filename)
                                                                 throws IOException{
        File file = new File(filename); 
        ImageIO.write(img, "png", file);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a transparent png image and layer various other pngs and
I'm trying to create a web page with a transparent image that appears overtop
I'm trying to get a transparent png frame image to hover over an img
Im trying to get two alpha-24 transparent .png files to crossfade into each other.
Trying to create a transparent gif with PIL. So far I have this: from
I am trying to create a transparent link which sits over an image (a
I'm trying to create a transparent (no button background) ImageButton that has a custom
I'm trying to create an image with a transparent background to display on a
Hey having some trouble trying to maintain transparency on a png when i create
im trying to create a semi-transparent form which is displayed in a panel. i

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.