我从网上找了一部分代码,我用bmp的时候没有问题,但是用png怎么不是一片黑,就是一片白之类的,如果能用一段代码处理多种图片格式呢?代码如下:/** 
     * 将图片进行裁剪 
     * @author kongqz 
     * @param x 图片的x坐标 
     * @param y 图片的y坐标 
     * @param width 截取框的宽 
     * @param height 截取框的高  
     * */ 
    public static BufferedImage CutImage(BufferedImage sourceImage, int x,int y,int width,int height )
    { 
        Image croppedImage; 
        ImageFilter cropFilter; 
        //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API 
        //指定要裁剪的的文件的宽度和高度,以及起始坐标 
        cropFilter = new CropImageFilter(x,y,width,height); 
        //生成图片 
        croppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter)); 
         
        //获取创建后的图片的高度 
        int h1=croppedImage.getHeight(null); 
        int w1=croppedImage.getWidth(null); 
         
        BufferedImage bi=new BufferedImage(w1,h1,BufferedImage.TYPE_INT_RGB ); 
         
        Graphics g=bi.getGraphics(); 
        //在画图的时候可以设置背景色 
        g.drawImage(croppedImage, 0, 0,Color.white, null); 
        g.dispose();        return bi;
    } 

解决方案 »

  1.   

    /**
     * java创建图片的缩略图 create date:2009-5-27 author:Administrator
     * 
     * @param filename
     * @param thumbWidth
     * @param thumbHeight
     * @param quality
     * @param outFilename
     * @throws InterruptedException
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void createThumbnail(String filename, int thumbWidth,
    int thumbHeight, int quality, String outFilename)
    throws InterruptedException, FileNotFoundException, IOException { // load image from filename
    Image image = Toolkit.getDefaultToolkit().getImage(filename);
    MediaTracker mediaTracker = new MediaTracker(new Container());
    mediaTracker.addImage(image, 0);
    mediaTracker.waitForID(0); // use this to test for errors at this point:
    // System.out.println(mediaTracker.isErrorAny());
    // determine thumbnail size from WIDTH and HEIGHT
    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;
    if (thumbRatio < imageRatio) {
    thumbHeight = (int) (thumbWidth / imageRatio);
    } else {
    thumbWidth = (int) (thumbHeight * imageRatio);
    }
    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
    // save thumbnail image to outFilename
    BufferedOutputStream out = new BufferedOutputStream(
    new FileOutputStream(outFilename)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
    quality = Math.max(0, Math.min(quality, 100));
    param.setQuality((float) quality / 100.0f, false);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(thumbImage);
    out.close();
    }我测试了 jpg,png 都可以的
      

  2.   

    1楼的确可以的   java是支持png的
      

  3.   

    详细看看  FilteredImageSource这个类是否支持png