Generate a PNG Image with transparent background in Java

Source: Internet
Author: User
Tags transparent image

Recently used Java to dynamically generate the background transparent image function, from the GIF and PNG select the PNG format, automatically add link address: http://www.my400800.cn to the picture on the site. The search results are summarized as follows:

1. Generate PNG Images

Int width = 400;

Int Height = 300;

// Create a bufferedimage object

Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );

// Obtain graphics2d

Graphics2d g2d = image. creategraphics ();

// Draw

G2d. setcolor (new color (255, 0, 0 ));

G2d. setstroke (New basicstroke (1 ));

G2d. Draw

// Release the object

G2d. Dispose ();

// Save the file

ImageIO. Write (image, "PNG", new file ("C:/test.png "));

Int width = 400;

Int Height = 300;

// Create a bufferedimage object

Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );

// Obtain graphics2d

Graphics2d g2d = image. creategraphics ();

// Draw

G2d. setcolor (new color (255, 0, 0 ));

G2d. setstroke (New basicstroke (1 ));

G2d. Draw

// Release the object

G2d. Dispose ();

// Save the file

ImageIO. Write (image, "PNG", new file ("C:/test.png "));

This is just a graphicalCodeThe background is black. How can we make the background transparent? The search fails, but the following code is found. It only sets the self-drawn image to transparent or translucent, and the background is not transparent, as follows:

2. Draw a translucent Image

Int width = 400;

Int Height = 300;

// Create a bufferedimage object

Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );

// Obtain graphics2d

Graphics2d g2d = image. creategraphics ();

// Set transparency

G2d. setcomposite (alphacomposite. getinstance (alphacomposite. src_atop, 1.0f); // 1.0f indicates transparency, and the value ranges from 0 to 1.0.

// Draw

G2d. setcolor (new color (255, 0, 0 ));

G2d. setstroke (New basicstroke (1 ));

G2d. Draw

// Release the object

// The transparency setting ends.

G2d. setcomposite (alphacomposite. getinstance (alphacomposite. src_over ));

G2d. Dispose ();

// Save the file

ImageIO. Write (image, "PNG", new file ("C:/test.png "));

The image drawn in this way should be transparent to the foreground, and the background is still black ,:(

I didn't see any useful code on the Internet. On csdn, I said I had implemented the code myself, but I didn't know how to implement it. I had to find it myself. It took more than half an hour, I checked almost all the methods and attributes of bufferedimage and graphics2d, and finally found the solution. I just added two lines of code, as shown below:

Int width = 400;

Int Height = 300;

// Create a bufferedimage object

Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );

// Obtain graphics2d

Graphics2d g2d = image. creategraphics ();

// ---------- Add the following code to make the background transparent -----------------

Image = g2d. getdeviceconfiguration (). createcompatibleimage (width, height, transparency. translucent );

G2d. Dispose ();

G2d = image. creategraphics ();

// ---------- The background Transparent Code ends -----------------

// drawing
g2d. setcolor (new color (255, 0, 0);
g2d. setstroke (New basicstroke (1);
g2d. draw
// release object
g2d. dispose ();
// save the file
ImageIO. write (image, "PNG", new file ("C:/test.png");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.