Solved

ImageIO.write produces incomplete image

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 122
create a gui in perl 3 67
Cipher Configuration on Apache HTTPD 4 50
nextBoolean(double p) for Random class 3 35
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

943 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

6 Experts available now in Live!

Get 1:1 Help Now