How to save image?
Hi all,
I have drawn an Image using Graphics, and then I have drawn some rectangles on it.
How can I save that image with drawn rectangles?
any idea or link which should I refer?
Hi all,
I have drawn an Image using Graphics, and then I have drawn some rectangles on it.
How can I save that image with drawn rectangles?
any idea or link which should I refer?
Example 1:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class WriteImageType {
static public void main(String args[]) throws Exception {
try {
int width = 200, height = 200;
// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
Font font = new Font("TimesRoman", Font.BOLD, 20);
ig2.setFont(font);
String message = "www.java2s.com!";
FontMetrics fontMetrics = ig2.getFontMetrics();
int stringWidth = fontMetrics.stringWidth(message);
int stringHeight = fontMetrics.getAscent();
ig2.setPaint(Color.black);
ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);
ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
Example 2:
// Create an image to save
RenderedImage rendImage = myCreateImage();
// Write generated image to a file
try {
// Save as PNG
File file = new File("newimage.png");
ImageIO.write(rendImage, "png", file);
// Save as JPEG
file = new File("newimage.jpg");
ImageIO.write(rendImage, "jpg", file);
} catch (IOException e) {
}
// Returns a generated image.
public RenderedImage myCreateImage() {
int width = 100;
int height = 100;
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
// Draw graphics
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.black);
g2d.fillOval(0, 0, width, height);
// Graphics context no longer needed so dispose it
g2d.dispose();
return bufferedImage;
}
Thank you java_2006 for this but I want to know that when I draw any image on canvas after making changes on that canvas, how can I save that Image which is on that canvas?
Canvas is an AWT term. If thats what you are drawing on then you should be posting in the AWT forum so people know what you are talking about.
Most of your other questions are about Swing so your custom painting would be done on a class extending JComponent or maybe JPanel. In which case you should be posting in the Swing forum.
We can't give an answer if we don't know what type of application your are building.
@java_2006, it would be decent if you provide a link where you "borrowed" that code. Now it looks like you're implying you wrote it (sounds like: plagiarism).