• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with creating a BufferedImage and save it as bmp file

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Thomas Houseman
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You so much pete stein !
I will try that soon and let you know if it worked for me.

bye
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Thomas Houseman
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all , now it works.
Probably the "BMP" instead of ".bmp" solved it.
Have a great day.
 
reply
    Bookmark Topic Watch Topic
  • New Topic