Solved

ImageIO.write produces incomplete image

Posted on 2004-08-20
5
1,387 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
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 250 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now