aspose file tools
The moose likes Java in General and the fly likes Problem with creating a BufferedImage and save it as bmp file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Problem with creating a BufferedImage and save it as bmp file" Watch "Problem with creating a BufferedImage and save it as bmp file" New topic
Author

Problem with creating a BufferedImage and save it as bmp file

Thomas Houseman
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Hi,
I'm trying to create a BufferedImage, add a text to it , and save it as a bmp picture.
But, the picture file created with size 0 and it's actually empty file (but it created).
This is the code :



What am I doing wrong ?!

Thanks !
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Well, the format name should be "BMP", not ".bmp", so you might give that a try. Otherwise, it's probably OK (except that you might be writing a white string on a white background, not sure.)


[Jess in Action][AskingGoodQuestions]
Thomas Houseman
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Hi.
It was actually black in the code, i fixed it.
Yet it doesn't work. A file has been created but with size 0.
Can anyone help please ? What am i doing wrong in the code ?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Does your BufferedImage constructor call compile? Don't you need to add the type of image you want to create? BufferedImage has constants that are used for this sort of thing, such as BufferedImage.TYPE_INT_RGB

for instance,

corrected the getting multiple Graphics objects
Thomas Houseman
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
pete stein wrote:Does your BufferedImage constructor call compile? Don't you need to add the type of image you want to create? BufferedImage has constants that are used for this sort of thing, such as BufferedImage.TYPE_INT_RGB


Sorry, I added it to the original code, YET it doesn't work.
H-E-L-P Please
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Thomas Houseman wrote:Sorry, I added it to the original code, YET it doesn't work.

Calm down, it'll be alright. Please see corrections to code above.
Thomas Houseman
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Thank You so much pete stein !
I will try that soon and let you know if it worked for me.

bye
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

I took your original code, fixed the imports, added a throws clause, compiled it and ran it. It gave me a 0 byte BMP file. Then I changed ".bmp" into "bmp" (as Ernest already suggested), compiled it and ran it. It gave me a 180,054 byte BMP file, with a black background and white letters.

When you call getGraphics(), it creates a new Graphics object. As such, your second call created a second Graphics object, that did not have the black color set. When drawing on a BufferedImage, you should do that as follows:
This gives me an all-black image. Sure, it has the text printed, but in black


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Thomas Houseman
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Thank you all , now it works.
Probably the "BMP" instead of ".bmp" solved it.
Have a great day.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem with creating a BufferedImage and save it as bmp file
 
Similar Threads
Overwriting on a jpg in a Jpanel;
Jpeg images not proper in MAC
creating Image
Draw [Inequality Number Line] with Java
ImageIO.write - TIF and BMP - Zero bytes