displaying image in jlabel
when displaying an image in a jlable. the jlabel takes on the size of the image. i have tried to set the maximum bounds to the size of the original size of the jlable and even the panel the jlable sits on, but each time that i go to display the picture, it is displayed huge and not the size of the jlabel it is placed in.
here is my creation code, my gui was created in netbeans.
pnImage.setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
pnImage.setFocusable(false);
pnImage.setMaximumSize(new java.awt.Dimension(395, 365));
lblImage.setFocusable(false);
lblImage.setMaximumSize(new java.awt.Dimension(395, 365));
javax.swing.GroupLayout pnImageLayout =new javax.swing.GroupLayout(pnImage);
pnImage.setLayout(pnImageLayout);
pnImageLayout.setHorizontalGroup(
pnImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pnImageLayout.setVerticalGroup(
pnImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
);
and here is how i display it.
Dimension d = pnImage.getSize();
System.out.println("d = " + d.toString());
pnImage.setMaximumSize(d);
lblImage.setMaximumSize(d);
ImageIcon icon =new ImageIcon(contact.getImage());
lblImage.setIcon(icon);
I've never done this (convert a byte array to an image), so I thought I'd have a go...import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageTest {
public static void main(String[] args) throws Exception {
// Grab some image...
File inFile = new File("resources/imageH.jpg");
int len = (int)inFile.length();
System.out.printf("file length = %d%n", len);
FileInputStream in = new FileInputStream(inFile);
// ... and create a byte array for testing
byte[] byteArray = new byte[len];
int num = in.read(byteArray);
in.close();
System.out.printf("bytes read = %d%n", num);
// (1) Create an image from the byte array - I don't
// know Imagio figures out what sort of image it is!
BufferedImage im = ImageIO.read(new ByteArrayInputStream(byteArray));
int w = im.getWidth();
int h = im.getHeight();
System.out.printf("width = %d, height = %d", w, h);
// (2) Scale to fit 100x100
if(w > h) {
w = (100 * w) / h;
h = 100;
} else {
h = (100 * h) / h;
w = 100;
}
System.out.printf(" --> %d,%d%n", w, h);
// (3) And create the icon
ImageIcon icon = new ImageIcon(im.getScaledInstance(w, h, Image.SCALE_SMOOTH));
System.out.printf("The icon is: %s%n", icon);
}
}
The output isfile length = 54918
bytes read = 54918
width = 500, height = 300 --> 166,100
The icon is: javax.swing.ImageIcon@5afd29Assuming your byte array is some (to ImageIO) recognisable image format, something
similar should work for you.