aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to get Image from Canvas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to get Image from Canvas" Watch "how to get Image from Canvas" New topic
Author

how to get Image from Canvas

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2046
Hi,

I issue draw() on the Canvas.



I need later to get the image in that Canvas.

How do I get the image from the Canvas?

I will appeciate it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
What kind of object is "image" - can't you use that directly?


Ping & DNS - updated with new look and Ping home screen widget
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2046
Ulf Dittmer wrote:What kind of object is "image" - can't you use that directly?


(image is a jpg byte [])

Actually, I have only 1 target: resize a photo to a larger size, keeping the ratio.

but if the original dimension is not proportional to the target dimension, i want to fill extra area to make it be exact dimension as the target dimension.

this is why i am putting the image first into a Canvas, with the background color set, and the target dimension set for the Canvas.

Now I need to get the Image in the Canvas.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

I see so many things wrong with that method...
1) you are missing the call to super.paint(g). Without it the previously painted contents will not be thrown away.
2) unless the Canvas is not part of a user interface or it's parent container has no layout manager the call to setSize does nothing.
3) setBackground shouldn't be called from the paint method but before it's ever called. This call could (it doesn't know for as far as I know but it could in the future) cause a repaint, which in your case would lead to a StackOverflowError.

In fact, only the if statement with contents is right.

Jesus Angeles wrote:
Ulf Dittmer wrote:What kind of object is "image" - can't you use that directly?


(image is a jpg byte [])

You can't draw a byte[] directly. You can only draw an Image. You can use ImageIO.read to convert a byte[] (through a ByteArrayInputStream[]) into a BufferedImage.

Actually, I have only 1 target: resize a photo to a larger size, keeping the ratio.

but if the original dimension is not proportional to the target dimension, i want to fill extra area to make it be exact dimension as the target dimension.

this is why i am putting the image first into a Canvas, with the background color set, and the target dimension set for the Canvas.

Now I need to get the Image in the Canvas.

If I get it clear you just want to resize an existing image, right? Then drop the Canvas - use BufferedImage instead:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2046
re:"You can't draw a byte[] directly."

It is an Image. I mistakenly mentioned that it is a byte[].

Thanks to your reply on both threads. Yes, it is the same issue.

Ill try it out.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2046
Rob, Thanks. It worked well.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get Image from Canvas
 
Similar Threads
HELP!!!! how do i get image by extending Canvas object
How do I put an image on a canvas?
Image from Canvas
Write layouts to the bitmap file
Getting image using tomcat webserver