We help IT Professionals succeed at work.

Save all canvas area, not only the visible area

1,002 Views
Last Modified: 2013-12-29
Hello, Experts!
My question is: how can I save the canvas area, not only the visible area. See attached code. Thank you!!!
public void savePNG(String filename,Transport v,int _minX, int _minY,int _maxX, int _maxY) throws IOException {
	WhiteboardContext wb=this;
	//now save all shapes...
	BufferedImage img = new BufferedImage(_maxX-_minX,_maxY-_minY, BufferedImage.TYPE_INT_RGB);
	Graphics g=img.createGraphics();
    //wb.setXOffset(-_minX);
	//wb.setYOffset(-_minY);
    Graphics2D g2d = (Graphics2D)g;
    g2d.setBackground(Color.WHITE);
    g.fillRect(0,0,_maxX-_minX,_maxY-_minY);
 
	for(Enumeration e=v.getAllShapes().elements();
		e.hasMoreElements(); ) {
	    Tool.Shape s=(Tool.Shape)(e.nextElement());
	    s.updateState(wb);
	}
	for(Enumeration e=v.getAllShapes().elements();
		e.hasMoreElements(); )
    {
	    Tool.Shape s=(Tool.Shape)(e.nextElement());
	    s.paint(wb, g2d);
	}
	ImageIO.write(img, "png", new File(filename));
    System.out.println("saved...");
    }

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
afaicr you can't. You can only save visible areas
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
shouldn't be a problem (assuming they are all getting passed to you). What exactly is happening?

Author

Commented:
Ok, when I try to save the picture NOT from the upper left corner, I get the picture 1.
When I trye to save the picture FROM the upper left corner, I get the picture 2.
So the problem is that I can change the picture's size to save it, but only when I make visible the upper left corner (tha't, when I move the picture to the left upper corner). In other case the picture is not saved entirely. I gues i have problems with the coords.
Thanks!!!

Alex
080520090208019f9f197d-f4d7-4ba8.png
080520090208017d97916e-b354-406d.png
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes, you need to define the coords to include the complete image
You can scale it if you you want to control the resulting image size independent of the source

Author

Commented:
Could you provide me a code to do it? Thank you!
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
"What happens when you change

>>_maxX-_minX

for _maxX and

>>_maxY-_minY

for _maxY throughout?"

Doesn't work....
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
what is you you want to achieve?

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.