?
Solved

ImageIO.write produces incomplete image

Posted on 2004-08-20
5
Medium Priority
?
1,458 Views
Last Modified: 2010-08-05
I'm using Java 1.4.2-04 and ImageIO.write to create a jpeg.  Occassionaly the complete image is produced for a file size of about 150K. However, the image is usually incomplete with no consistency on what parts are drawn,

File file = new File(fileName);
BufferedImage b = new BufferedImage((int)diagramWidth, (int)diagramHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)b.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, (int)diagramWidth, (int)diagramHeight);
g.setColor(Color.black);
ColorModel cm = b.getColorModel();
...draw lost of text and shapes...
ImageIO.write(b, "jpg", file);

Thanks
0
Comment
Question by:John4343
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11851716
Unfortunately it's around

>>...draw lost of text and shapes...

that the problem is likely to lie ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11851741
... which you may be able to (and would be good to) confirm by

g.setColor(Color.red);
g.fillRect(0, 0, (int)diagramWidth, (int)diagramHeight);
// DON'T draw lots of text and shapes
ImageIO.write(b, "jpg", file);


and THEN check it
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11852180
Try to use BufferedImage.TYPE_INT_ARGB instead of  BufferedImage.TYPE_INT_RGB
I had similar troubles with the  BufferedImage.TYPE_INT_RGB
0
 
LVL 1

Accepted Solution

by:
SlimHealer earned 1000 total points
ID: 11854564
The problem is that the graphics pipeline doesn't know that you are finished using the graphics object you obtained with the call to getGraphics().

Add a

     g.dispose();

before the line:

   ImageIO.write(b, "jpg", file);

This should force java to flush the graphics pipleines.
0
 
LVL 92

Expert Comment

by:objects
ID: 11856875
>>>...draw lost of text and shapes...
>that the problem is likely to lie ;-)

Can you explain why you think that whats painted to the image is causing the problem, and what problems painting to the image can cause?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question