aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to change the dimensions of an image object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change the dimensions of an image object" Watch "How to change the dimensions of an image object" New topic
Author

How to change the dimensions of an image object

Bob Sale
Greenhorn

Joined: Jul 04, 2013
Posts: 15
I have an instance of a Buffered Image object, myImg, and need to change its dimensions. For example, if the dimensions are 200 wide and 100 high, I need to change it to 100 wide by 200 high.
I know about drawImage(sourceImg, 0, 0, newWidth, newHeight, null) but this just adjusts the width and height of the sourceImg that will be added to myImg (the dimensions of myImg will remain 200x100).

My application is a method to rotate an image by 90deg, which is initially contained in myImg. After rotating the image, I want it to end up back in myImg with the new dimensions of myImg as 100x200.

Performing the rotation is not the issue here. The issue is resizing myImg so that its dimensions will match those of the rotated image.

In my current implementation, the image is rotated correctly and is added to myImg, but what appears in myImg is just the first 100x100 pixels of the rotated image. To the right of the rotated image, I have the remnants of what was in myImage before the rotation, and the bottom half of the rotated image is clipped off because the dimensions of myImg has remained 200x100 instead of the necessary 100x200.

Can anyone help me out on this?

Thanks,

Bob
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
How are you performing the rotation? Are you first creating a new image with newly switched dimensions?


Ping & DNS - updated with new look and Ping home screen widget
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
I believe you will need to create a new BufferedImage with your new dimensions. Then you will need to paint the rotated image to this new image.

Or maybe you can use the Rotated Icon.
Bob Sale
Greenhorn

Joined: Jul 04, 2013
Posts: 15
Here is the code for my rotation method. I pass the image that I wish to rotate (wkgImg) to the method through the bi argument. wkgImg is declared to be global, so the final result of the rotation ends up as bi and hence wkgImg. It all works except that bi (wkgImg) is not being resized to accommodate the dimensions of the rotated image. The original dimensions of wkgImg are retained.

public void rotateImage90(BufferedImage bi){
BufferedImage tempImg = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
Graphics2D g2 = tempImg.createGraphics();
g2.drawImage(bi, 0, 0, bi.getWidth(), bi.getHeight(), null);
g2.dispose();
Graphics2D g2bi = bi.createGraphics();
g2bi.translate(tempImg.getHeight(), 0);
g2bi.rotate(Math.toRadians(90));
g2bi.drawImage(tempImg, 0, 0, tempImg.getWidth(), tempImg.getHeight(), null);
g2bi.dispose();
}

I had already tried the suggestion that Bob Camick made by adding bi = new BufferedImage(tempImg.getHeight(), tempImg.getWidth(), tempImg.getType()); just before Graphics2D g2bi = bi.createGraphics();

The code compiles but does not do anything. I think that when the new instance of bi is created, it is not associated with the argument bi -- is this possible?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
Post your SSCCE that demonstrates the problem.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Bob Sale wrote:I think that when the new instance of bi is created, it is not associated with the argument bi -- is this possible?


This doesn't make any sense to me. What do you mean by "the new instance of bi"? And what do you mean by "associated with the argument bi"?

However I do think that you should return the rotated image as the result of the method instead of declaring it to return nothing. Otherwise you're trying to modify the image which is referred to by the "bi" parameter, which your code doesn't do. Your code creates a new image.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change the dimensions of an image object
 
Similar Threads
Need help on print , scrollbar for my applet
ideas wanted on making this custom JTable cell renderer more efficient
Just wondering
Adjusting the "clickable" area of JButton
Rotating a JWindow with multiple JPanels