Solved

ImageIO.write produces incomplete image

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 61
Desingning Refactoring existing code 2 32
ejb entity bean example issue 2 30
Setup GlassFish 4 37
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 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