Solved

ImageIO.write produces incomplete image

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

688 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