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");