This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Dynamic JPG with java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamic JPG with java" Watch "Dynamic JPG with java" New topic

Dynamic JPG with java

nuthan kumar
Ranch Hand

Joined: Feb 14, 2006
Posts: 47
Can anybody help me with the code regarding how to create a dynamic JPG file with text string as image using JAVA.
Joe Ess

Joined: Oct 29, 2001
Posts: 8836

Saving a Generated Graphic to a PNG or JPEG File

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
nuthan kumar
Ranch Hand

Joined: Feb 14, 2006
Posts: 47
Hi Thanks for your reply,
I am able to create JPG file but its not displaying text in the image.

public static void main(String[] args)
int width = 200;
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.fillRect(0, 0, width, height);

g2d.drawString("Sample-Text", 0, 0);
RenderedImage rendImage = bufferedImage;
try {
// Save as JPEG
File file = new File("newimage.jpg");
ImageIO.write(rendImage, "jpg", file);
} catch (IOException e) {

// Graphics context no longer needed so dispose it

// return bufferedImage;

Joe Ess

Joined: Oct 29, 2001
Posts: 8836

Have a look at the API documentation for java.awt.Graphics. The x,y position arguments for drawString is the baseline of the text, not the upper left-hand corner. You can probably see the tail of the "p" at the top of your image.
I agree. Here's the link:
subject: Dynamic JPG with java
Similar Threads
PA #1.....picture association
WA #1.....word association
Dynamic dispatch??????
dynamic data on image?
Does JPG Java Class Exist