Results 1 to 5 of 5
- 06-25-2011, 07:06 PM #1
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
How to scale an image to desired size?
Hello all,
As the heading says, i have a label in which i want to show a picture, i have take the picture as an inputStream from the database and put it as an image icon on a label.
my code is:
But if i have an image of size larger than my label size than only some part is visible in the viewport of label..Java Code:/** * Setting icon on label imageLabel * */ if (imageInputStream != null){ try { Image image = ImageIO.read(admin.image); imageLabel.setIcon(new ImageIcon(image)); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Error: "+ex.getMessage(), "Error in reading Image", JOptionPane.ERROR_MESSAGE); } }
What i want to do is to automatically scale the image to desired label size without distorting image as much as possible..Also the image taken as input is not of same size..
How can i achieve this??
Any help? suggestions? tutorials? Articles?
- 06-25-2011, 08:32 PM #2
First off, if you use an appropriate layout the JLabel will be sized appropriately based on the size of the Icon.
If due to other constraints you need the image to be displayed at a fixed size, it may be better to achieve this via custom painting on a JComponent or JPanel. Choose a suitable overload of Graphics#drawImage(...).
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
- 06-26-2011, 07:09 PM #3
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
That lesson will help me for other stuffs in my project, for now particularly, i define a method to get sclaed image of whatever image i get from database to desired scale:Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Any other suggestions for above method?Java Code:public static BufferedImage getScaledImage(BufferedImage pImage, int pWidth, int pHeight) { int type = pImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : pImage.getType(); BufferedImage resizedImage = new BufferedImage(pWidth, pHeight, type); Graphics2D graphics = resizedImage.createGraphics(); graphics.setComposite(AlphaComposite.Src); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.drawImage(pImage, 0, 0, pWidth, pHeight, null); graphics.dispose(); return resizedImage; }
Thanks!
- 06-26-2011, 09:04 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Java Code:Image.getScaledInstance(...);
- 06-26-2011, 09:07 PM #5
Similar Threads
-
Error in converting colour image to gray scale
By LankanSniper in forum Java 2DReplies: 9Last Post: 12-14-2009, 04:30 PM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
Image size in java
By Sharath in forum New To JavaReplies: 1Last Post: 04-01-2009, 01:06 PM -
How to Scale an Image with the help of Affine Transformation?
By sayan751 in forum AWT / SwingReplies: 3Last Post: 03-21-2009, 09:34 PM -
how to set an image size
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks