Results 1 to 3 of 3
  1. #1
    Spitfire777 is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Serializing BufferedImage with keeping transparency

    Hi,

    I serialize BufferedImage by using this class:

    public class ServerUserAvatarMessage extends ServerMessage {
    private static final long serialVersionUID = 6796478081410599600L;

    private boolean success = false;
    private boolean useDefaultAvatar = true;
    private String username = null;

    int width; int height; int[] pixels;

    public void setAvatar(BufferedImage bi) {
    width = bi.getWidth();
    height = bi.getHeight();
    pixels = new int[width * height];
    int[] tmp = bi.getRGB(0, 0, width, height, pixels, 0, width);
    }

    /**
    * @return the avatar
    */
    public BufferedImage getAvatar() {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    bi.setRGB(0, 0, width, height, pixels, 0, width);
    return bi;
    }

    /**
    * @return the username
    */
    public String getUsername() {
    return username;
    }

    /**
    * @param username the username to set
    */
    public void setUsername(String username) {
    this.username = username;
    }

    /**
    * @return the success
    */
    public boolean isSuccess() {
    return success;
    }

    /**
    * @param success the success to set
    */
    public void setSuccess(boolean success) {
    this.success = success;
    }

    /**
    * @return the useDefaultAvatar
    */
    public boolean isUseDefaultAvatar() {
    return useDefaultAvatar;
    }

    /**
    * @param useDefaultAvatar the useDefaultAvatar to set
    */
    public void setUseDefaultAvatar(boolean useDefaultAvatar) {
    this.useDefaultAvatar = useDefaultAvatar;
    }
    }


    But if i send png files using transparency, transparency is not displayed by the output file. :(

  2. #2
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Your getAvatar method returns a BufferedImage that doesn't support transparency. That's probably the problem.

    On a side note, I think most people would probably encode the BufferedImage into any one of the available formats (jpeg, png, gif, ect...) and save/send the encoded byte[] array instead of saving/sending the int[] rgb array. The space requirements of images in raw format (read: the int[] rgb array) is not a thing you should underestimate.
    Last edited by Maxideon; 12-02-2010 at 05:20 AM.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Change

    Java Code:
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    to

    Java Code:
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Similar Threads

  1. Alternating Transparency Based on Double Variable
    By YellowPainting in forum Java 2D
    Replies: 2
    Last Post: 11-05-2010, 09:59 PM
  2. Keeping data in memory
    By roud9 in forum New To Java
    Replies: 1
    Last Post: 11-02-2010, 01:28 AM
  3. ImageIcon Transparency
    By Lingerz in forum New To Java
    Replies: 2
    Last Post: 06-04-2010, 03:53 PM
  4. Setting an image's transparency?
    By aaroffl in forum AWT / Swing
    Replies: 1
    Last Post: 12-03-2008, 01:01 PM
  5. Replies: 0
    Last Post: 07-25-2008, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •