1. The package used Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intwidth = 100; intHeight = 100; //1. Create a BufferedImage object with no transparent colorBufferedImage bimage=Newbufferedimage (width, height, bufferedimage.type_int_rgb); //2. Create a BufferedImage object with transparent colorBimage=Newbufferedimage (width, height, bufferedimage.type_int_argb); //3. Create a BufferedImage object that is compatible with the screengraphicsenvironment GE=graphicsenvironment. getlocalgraphicsenvironment (); GraphicsDevice GS=Ge.getdefaultscreendevice (); GraphicsConfiguration GC=gs.getdefaultconfiguration (); //Create An image this does not support transparencyBimage=gc.createcompatibleimage (width, height, transparency.opaque); //Create an image that supports transparent pixelsBimage=gc.createcompatibleimage (width, height, transparency.bitmask); //Create an image that supports arbitrary levels of transparencyBimage=gc.createcompatibleimage (width, height, transparency.translucent); } //4. Of course we can also create a BufferedImage object in the graphical context Public voidPaint (Graphics g) {graphics2d g2d=(graphics2d) G; intwidth = 100; intHeight = 100; //Create An image this does not support transparencyBufferedImage bimage=g2d.getdeviceconfiguration (). Createcompatibleimage (width, height, transparency.opaque); //Create an image that supports transparent pixelsBimage=g2d.getdeviceconfiguration (). Createcompatibleimage (width, height, transparency.bitmask); //Create an image that supports arbitrary levels of transparencyBimage=g2d.getdeviceconfiguration (). Createcompatibleimage (width, height, transparency.translucent); }}2. Image clipping using BufferedImage: Public Static voidMain (string[] args) {Try { //loading from a specific filebufferedimage Bi= Imageio.read (NewFile ("C:\\test.jpg")); Bi.getsubimage (0, 0, 10, 10); } Catch(IOException e) {e.printstacktrace (); }2How to fetch the bufferedimagebufferedimage image= Imageio.read (NewFile ("1.gif"));3.BufferedImage---->byte[]imageio.write (BufferedImage image,string format,outputstream out); The method can solve the problem well; The parameter image represents the obtained bufferedimage The format of the parameter, such as "GIF", and so on, the parameter out represents the output stream, if it is to be converted into a byte array, the output stream is bytearrayoutputstream, and after execution, only Tobytearray () is needed to get byte[];4. Display BufferedImage Public voidPaint (Graphics g) {Super. Paint (g); G.drawimage (image); //image is bufferedimage type}
Usage of bufferedimage in Java