Java BufferedImage to PNG imageAsBase64Png(Component c)

Here you can find the source of imageAsBase64Png(Component c)

Description

image As Base Png

License

Open Source License

Declaration

static String imageAsBase64Png(Component c) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;
import java.awt.image.*;

import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;

public class Main {
    static String imageAsBase64Png(Component c) {
        if (c.getWidth() <= 0 || c.getHeight() <= 0) {
            return "";
        }//from w ww .  jav a 2 s .c o m
        BufferedImage img = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_RGB);
        c.paint(img.getGraphics());
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        try {
            ImageIO.write(img, "png", buf);
        } catch (java.io.IOException ex) {
            return ex.toString();
        }
        return javax.xml.bind.DatatypeConverter.printBase64Binary(buf.toByteArray());
    }
}

Related

  1. imageToPNG(BufferedImage image)
  2. imageToPngFile(BufferedImage image, File pngFile)
  3. image(BufferedImage image)
  4. bufferedImageToFile(BufferedImage src, String path)