How do I get Canvas image -> .gif/.jpg file?

  • Thread starter Miss Elaine Eos
  • Start date
M

Miss Elaine Eos

I have an image that I've drawn on a canvas.

I'd like to have a .gif or .jpg (or similarly java-readable image) file
on my hard disk that, when read back in, looks like said canvas.

Std java would be a big bonus.

What classes do I want to read more about in the docs?

Thanks! :)
 
C

Chris Smith

Miss said:
I have an image that I've drawn on a canvas.

I'd like to have a .gif or .jpg (or similarly java-readable image) file
on my hard disk that, when read back in, looks like said canvas.

Std java would be a big bonus.

java.awt.Robot contains a screen capture capability, but is not
guaranteed to be available. Far better than drawing the image to the
Canvas and then trying to read it back would be to get an Image and use
getGraphics to actually redraw the contents of the Canvas onto your
Image. Then use can use the javax.imageio package to save it.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Miss Elaine Eos

java.awt.Robot contains a screen capture capability, but is not
guaranteed to be available. Far better than drawing the image to the
Canvas and then trying to read it back would be to get an Image and use
getGraphics to actually redraw the contents of the Canvas onto your
Image. Then use can use the javax.imageio package to save it.

I guess I should've specified the problem a bit more...

I have a java task which occasionally creates an image (on a canvas),
that I want to save to disk, so that a web page can load said image.
For example, this process might be an activity usage graph.

So, I draw the graph, save it as .gif or .jpg as "usage.gif", then my
web-page loads and includes <img src="usage.gif">, which displays the
..gif on the page.

No, no, no... I don't want to have an applet just draw directly on my
web-page. I'm trying to create image-format files! Robot's screen
capture, in addition to spotty availability, has the problem of only
working on the screen. My image isn't actually displayed at the time I
hope to write it to disk -- it's in an offscreen buffer.

---

So, javax.imageio sounds promising, although initial quick-skim didn't
turn up anything like "writeGIFfile()" -- but I'll keep reading.

Thanks!
 
P

Phil...

You said "I" and "my disk" but I think that an applet
can only load files from the server.
 
C

Chris Smith

Miss said:
I guess I should've specified the problem a bit more...

I think I answered your question... let's see if we can find the
miunderstanding here.
I have a java task which occasionally creates an image (on a canvas),

By "on a canvas" do you mean a java.awt.Canvas object? That was my
assumption. Perhaps you mean something else.
No, no, no... I don't want to have an applet just draw directly on my
web-page. I'm trying to create image-format files! Robot's screen
capture, in addition to spotty availability, has the problem of only
working on the screen.

Well, my understanding was that you'd already drawn on the screen (on a
java.awt.Canvas) and were now trying to recover the image back again so
that you could save it in a file. I guess I was wrong about that.

In any case, I recommended against that solution anyway. If it doesn't
work for you, then don't use it. Instead, draw onto a BufferedImage and
use the java.imageio package.
So, javax.imageio sounds promising, although initial quick-skim didn't
turn up anything like "writeGIFfile()" -- but I'll keep reading.

It's one of:

ImageIO.write(RenderedImage,String,File)
ImageIO.write(RenderedImage,String,OutputStream)

Which you choose depends on your desires; the latter is more flexible,
but requires that you open and close the file's OutputStream on your
own. The middle parameter is unclear from the documentation, but can be
set to "GIF" or "JPG" or "PNG".

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Miss Elaine Eos

Chris Smith said:
I think I answered your question... let's see if we can find the
miunderstanding here.

Seems you did, I'm just having brain-problems, today. Sorry.
By "on a canvas" do you mean a java.awt.Canvas object? That was my
assumption. Perhaps you mean something else.

Actually, I meant to say on a java.awt.graphics object (no canvas), but
yeah.
It's one of:

ImageIO.write(RenderedImage,String,File)
ImageIO.write(RenderedImage,String,OutputStream)

Which you choose depends on your desires; the latter is more flexible,
but requires that you open and close the file's OutputStream on your
own. The middle parameter is unclear from the documentation, but can be
set to "GIF" or "JPG" or "PNG".

Exactly what I wanted -- just needed a good night's sleep and to look at
it again. Thanks!
 
Joined
Nov 21, 2006
Messages
1
Reaction score
0
good Afternoon
i have drawn a image on the canvas now i want to convert that canvas
with the drawn image into an image file how can i do that plesase help me
i ahve used robot class but i am getting the whole screen what i sholud do to overcome this problem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top