Resize BufferedImage of a Transparent GIF PNG JPG

i must be losing my mind.
i use this same code in another program and it works fine.
first the images were transparent PNGs but then i changed
them to non-transparent GIFs and then JPGs.
no errors are thrown - but the images are being drawn at
their original size.
ive tried 3 different resizing methods and none work.
why doesnt this code work:
BufferedImage wp = ImageIO.read(new File("" + dir + "wp.gif"));
resize(wp);
public void resize(BufferedImage bi){
  AffineTransform tx = new AffineTransform();
     double xsc = ( sx ) / ( (double) bi.getWidth() );
     double ysc = ( sy ) / ( (double) bi.getHeight() );
    tx.scale(xsc, ysc);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    bi = op.filter(bi, null);
}

public void resize(BufferedImage bi){
    bi = op.filter(bi, null);
}The variable in your "resize" method is a local variable. Reassigning a value to it does not change anything outside the method. You will have to return the new value of "bi" or reassign the instance variable to the filtered image:
    wp = op.filter(bi, null);
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageSizing
    BufferedImage orig;
    public ImageSizing(BufferedImage image)
        orig = image;
    private BufferedImage scale(BufferedImage bi)
        AffineTransform tx = new AffineTransform();
        tx.scale(0.80, 0.80);
        AffineTransformOp op = new AffineTransformOp(tx,
                                         AffineTransformOp.TYPE_BICUBIC);
        return op.filter(bi, null);
    private JPanel getPanel()
        JPanel panel = new JPanel(new GridLayout(1,0));
        panel.add(new JLabel(new ImageIcon(orig), JLabel.CENTER));
        panel.add(new JLabel(new ImageIcon(scale(orig)), JLabel.CENTER));
        return panel;
    public static void main(String[] args) throws IOException
        String path = "images/bclynx.jpg";
        BufferedImage bi = ImageIO.read(ImageSizing.class.getResource(path));
        ImageSizing test = new ImageSizing(bi);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(test.getPanel());
        f.pack();
        f.setLocation(200,200);
        f.setVisible(true);
}

Similar Messages

  • Resizing a transparent GIF

    Hi There,
    I am having trouble getting a transparent gif to resize correctly while preserving the transparency.
    I am using the following GIF library to write my image to file:
    http://www.shetline.com/cgi-bin/java_formatter.cgi?src=../html/java/org/shetline/io/GIFOutputStream.java&tabwidth=3&log=true
    If I load in a gif image and send it straight to the GIFOutputStream, it preserves the transparent level. If I resize the image and send that to the GIFOutputStream, it loses the transparency. This is all happening in a JavaBean called by a Servlet (I am running in headless mode).
    The code to resize the image reads:
    Image image = Toolkit.getDefaultToolkit().createImage(originalImage);
    MediaTracker mediaTracker = new MediaTracker(new Container());
    mediaTracker.addImage(image, 0);
    mediaTracker.waitForAll();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedImage thumbImage = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
    // Encode the image as a GIF
    Color transparency = getGIFTransparency(thumbImage);
    if (transparency == null) {
    GIFOutputStream.writeGIF(out, thumbImage, 5);
    } else {
    GIFOutputStream.writeGIF(out, thumbImage, 5, transparency);
    The getGIFTransparency method reads:
    private Color getGIFTransparency(Image thisImage) {
    MediaTracker mediaTracker = new MediaTracker(new Container());
    mediaTracker.addImage(thisImage, 0);
    try {
    mediaTracker.waitForID(0);
    } catch (InterruptedException e) {
    Logger.addError(e.toString());
    PixelGrabber grabber = new PixelGrabber(thisImage, 0, 0,
    thisImage.getWidth(this),
    thisImage.getHeight(this),
    false);
    try {
    grabber.grabPixels();
    } catch (Exception e) {
    Logger.addError(e.toString(), this, "getGIFTransparency");
    if (grabber.getColorModel() != null) {
    ColorModel colorModel = (ColorModel) grabber.getColorModel();
    if (colorModel.getTransparency() != -1) {
    return new Color(colorModel.getRGB(colorModel.getTransparency()));
    return null;
    Any ideas on what I am doing wrong?
    Thanks in advance,
    Ben

    I'm having a similar problem, not exactly with Java 2D but I didn't find a JAI forum:
    when I create a new image from 2 BufferedImages, I also have no transparency (and just "add" changes colors because all components of the color are added to each other and green + red = yellow on the screen). I think it has something to do with the initialisation of BufferedImage, but I haven't solved the problem yet. The API says something about BufferedImage being opaque by default. I suppose you have to use a ColorModel in the constructor which is saying that your image is Transparency.Translucent.
    If this solves the problem (or if something else works), please do post here, cause I'm looking for a solution too!
    Greetings,
    Saskia.

  • I have a pdf file 50 pages and want add to 1st page a logo (icon png or gif or jpg), well I have cs4

    I have a pdf file 50 pages and want add to 1st page a logo (icon png or gif or jpg), well I have cs4 master collection... how do it?

    what is "pinch"? what cs4 program(s)
    to use for this?

  • Logo image distored when creating a transparent gif and png

    Hi all,
    Creating a logo for my department with little or no graphic design exp.  I'm experiencing issues when saving the file as a transparent GIF or PNG.  The GIF removes the 300 dpi setting that the print shop needs to turn this into a poster and the PNG is creating some jagged edges around my logo.  The image is 2000 pixels and about 7 in. by 7 in.
    Any help would be appreciated.  I can email the PSD to anyone if they can help.  Here is the PNG:
    And here is the GIF:

    There was no abuse, just an observation. Would an abusive person be trying to help you? Is there a crime in suggesting that one prepare themselves with more experience before taking on a project like this?
    Aside from low resolution, there were a few other signs that you might be in over your head with a logo design. Logos are typically created in a vector program (not Photoshop).
    I cannot imagine a reputable print shop asking you for a PNG or GIF file when there are so many better formats.

  • How to convert raster images like( PNG, JPG,GIF, BMP, TIFF etc) into SVG

    Hi Friends!
    With a hope that any Java Guru from SUN Forum can help me to solve my query, I'm posting my thread here.
    I have a project with feature to add image. My requirement is to convert any selected image file like (PNG,JPG,BMP,Tiff) into SVG Formate before adding it to Frame.
    I found one software from the link :
    http://delineate.sourceforge.net/
    Which covert raster to SVG.
    But I don't how can i use it runtime ?
    I want to convert during runtime process. like when user select option for to add Image then it call a procedure to add Image by converting any raster to svg & finally add to Frame.
    Your any help/suggestions wold be appreciated
    Regards
    Kirti

    There are basically two approaches:
    1. Embed the raster image in the SVG
    2. Vectorize the image
    1. Use Batik, or just write the XML yourself. Create some SVG files with embedded images, the syntax isn't to difficult. You could do it with a PrintWriter.
    2. Good luck to you. Writing a good vectorizer is
    - difficult
    - often pointless
    - though a great challenge if you're in to it
    The appllication you mention actually is only a front-end. it doesn't execute the vectorization itself. It uses Potrace and autotrace, which are native applications.

  • Regarding transparent gif creation

    To anyone who knows more about this subject, I'm looking for some sample code.
    I've been having trouble redrawing an image as a gif while introducing transparent regions.
    The following method results in blackness where the transparency should be:
        [http://www.exampledepot.com/egs/java.awt.image/DrawOnImage.html]
    The next method accomplishes the transparency, but the non-transparent areas suffer in quality (I am drawing a BufferedImage of an outdoor scene after the transparency is drawn - it come out grainy). I think this is because in creating a new ColorModel to establish the transparent pixels, the original colors are approximated.
        [m-10305410]
    Can anyone show me how to establish a transparent gif with the original quality of the non-transparent areas preserved?
    Thanks.

    I think I can accomplish what you suggest by creating the bufferedImage as type TYPE_INT_ARGB.
    Although it doesn't show the creation of the BufferedImage in the first link of the two above, specifying TYPE_INT_ARGB doesn't seem to make a difference.
    The second link seems to provide a way around this by setting the transparency through use of a new ColorModel.
    This is guesswork. If it's a false assumption that initializing a BufferedImage with TYPE_INT_ARGB provides alpha support, let me know.
    By grainy, I mean pixelated.
    The code I used can be found at:
         [http://forums.sun.com/thread.jspa?threadID=5333259&tstart=45]
    I had posted the subject there earlier, but I think I didn't get to the point soon enough (no replies).
    You can cut and paste that code and try the two methods bWITH_TRANSPARENCY=true & bWITH_TRANSPARENCY=false and see the difference in the quality of the images produced. Just change "c:\\myjpg.jpg"; to point at a JPG file on your system.

  • GIF to JPG always saving as CMYK

    Hi,
    I'm having very little luck trying to save a transparent GIF to a JPG.
    Try executing the code below to pull the GIF image im testing with
    and saving as a JPG. For me this image always ends up as a
    CMYK JPG which my browser can't open but Photoshop can.
    Any help would be appreciated.
    Thanks
    Kallen
    public static void main(String args[]) throws Exception
            BufferedImage  image = ImageIO.read(new URL("http://mry-img002.catalogcity.com/lotr.gif").openStream());
            ImageIO.write(image,"jpeg",new FileOutputStream("C:\\lotr.jpg"));
    }

    Thanks for all your help, but I eventually found the problem. You need to ColorConvert the BufferedImage that is read in to be RGB like so.
    BufferedImage dst = new BufferedImage(src.getWidth(),src.getHeight(),BufferedImage.TYPE_INT_RGB);
    ColorConvertOp xformOp=new ColorConvertOp(null);
    xformOp.filter(src,dst);

  • Framing image with transparent background png frame

    hi,
    i'm trying to find a way to frame images with transparent background png frames...
    what i'm doing now is
    1-drawing my image on a panel
    2-creating a 2nd image using the frame's filename, stretching this 'frame-image' to a size slighlty larger than that of my main image and drawing the 'frame-image'
    the problems with this method are:
    1-depending on the width of the frame, the frame sometimes hides parts of the image (thick frame), and sometimes there is a gap between the frame and the image (thin frame).
    2-if the image file containing the frame is larger than the frame (Ex: The image is 300x300, the frame is centered in this image and is 200x200; as opposed to the image is 200x200 and the frame takes up all the space), when i position the 'frame-image' near the top left corner of the image i want to frame, the frame appears at the wrong place (shifted down and to the right). This is due to the fact that i'm placing the top corner of the created 'frame-image' and not the frame, who is not in the top corner of my 'frame-image'.
    Is there a way to do what i'm trying to do???
    My ideas (which i don't know how to achieve are)
    1-To 'analyse' my transparent background png file and
         1-only keep the frame,
         2-calculate the frame's thickness
    OR
    2-Let java do the analyzing for me and tell it to frame my image with the frame in the png file
    please feel free to ask for more explanations if my description/question is confusing,
    thanks.

    Have you looked into the Border interface? If what you really want to do
    is put a custom border on a component, you may be able to do it this way.
    Anyway, here is some code that stacks and centres 2 images. It's not hard to do.
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    public class Example extends JComponent {
        private BufferedImage backgroundImage;
        private BufferedImage foregroundImage;
        public Example(BufferedImage backgroundImage, BufferedImage foregroundImage) {
            this.backgroundImage = backgroundImage;
            this.foregroundImage = foregroundImage;
        public Dimension getPreferredSize() {
            int w = backgroundImage.getWidth();
            int h = backgroundImage.getHeight();
            return new Dimension(w, h); //assuming this is bigger
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            //paint both, centred
            int x0 = (w-backgroundImage.getWidth())/2, y0 = (h-backgroundImage.getHeight())/2;
            g.drawImage(backgroundImage, x0, y0, null);
            int x1 = (w-foregroundImage.getWidth())/2, y1 = (h-foregroundImage.getHeight())/2;
            g.drawImage(foregroundImage, x1, y1, null);
        public static void main(String[] args) throws IOException {
            URL url1 = new URL("http://weblogs.java.net/jag/Image54-large.jpeg");
            URL url2 = new URL("http://weblogs.java.net/jag/DukeSaltimbanqueSmall.jpeg");
            JComponent comp = new Example(ImageIO.read(url1), ImageIO.read(url2));
            final JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(comp);
            f.pack();
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

  • Transparent GIFs for the web

    Hope someone can help with this.
    I'm basically trying to optimize a transparent image for the
    web. I have a straight PNG vesion here, which is what I'm trying to
    do, but it's around 96k - so a bit high.
    It looks like :
    example
    one
    I tried exporting it as an 8 bit PNG, which got the file size
    down, and looks OK in Safari, but looks hopeless in IE (6) :
    example
    2
    So I tried exporting as a transparent GIF, but it only looks
    like these :
    example
    3
    example
    4
    Any tips on how I can optimize it a bit better for the web,
    and retain it's original appearance?
    Cheers.

    As I have asked several times on the DW forum, how did you
    export the PNG
    image?
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.dreamweavermx-templates.com
    - Template Triage!
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    http://www.macromedia.com/support/search/
    - Macromedia (MM) Technotes
    ==================
    "Iain71" <[email protected]> wrote in
    message
    news:fan87d$n8r$[email protected]..
    > Hope someone can help with this.
    >
    > I'm basically trying to optimize a transparent image for
    the web. I have a
    > straight PNG vesion here, which is what I'm trying to
    do, but it's around
    > 96k -
    > so a bit high.
    >
    > It looks like :
    >
    >
    http://www.handprintdvd.co.uk/webdesign/carcarenew/homepagetemp.shtml
    >
    > I tried exporting it as an 8 bit PNG, which got the file
    size down, and
    > looks
    > OK in Safari, but looks hopeless in IE (6) :
    >
    >
    http://www.handprintdvd.co.uk/webdesign/carcarenew/homepage.shtml
    >
    > So I tried exporting as a transparent GIF, but it only
    looks like these :
    >
    >
    http://www.handprintdvd.co.uk/webdesign/carcarenew/homepagetemp3.shtml
    >
    >
    http://www.handprintdvd.co.uk/webdesign/carcarenew/homepagetemp4.shtml
    >
    > Any tips on how I can optimize it a bit better for the
    web, and retain
    > it's
    > original appearance?
    >
    > Cheers.
    >

  • Create Graph then export to Gif / PNG

    Hi all,
    Is it possible to use java 2D to create some graph (such as some diagram) then covert it into gif / png which allow user to read in browser.
    Any sample code I can refer?
    pls help.
    thanks
    Regards,
    Kin

    Toggle the transparency grid in your comp to make sure that you are creating an alpha channel.
    When you render set to the output module to RGB + Alpha and Millions of Colors + and set Color to Straight.

  • What is it with transparent GIFs and Black????

    I'm trying to use transparent gifs in a little game! ;o) as an introduction to graphical java!
    Problem is when I load the images using the toolkit and then setting them as say the Icon for a panel, they are surrounded by Black?! In addition when I try to display them using an BufferedImage they are also surrounded by black?!!!
    Ive been tearing my hair out trying to understand why - and have sucessfully failed :D
    Can anyone help?!
    Pleeeeeeeeeeeeease!
    Thanks

    Can't seem to get that working?!
    If I want to put that image as the icon for the whole window how would I do that?
    ImageIcon tempIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage("images/icon.gif"));
    f.setIconImage(tempIcon.getImage());where f is a JPanel.
    The above code does not work?! I just get the black surround to the gif as before!>?

  • How to import transparent gifs in after effects cc

    I just upgraded form after effects cs5 to cc and one thing i cant understand is why i cant directly import transparent gifs as i could in cs5. I heard that you have to make a png sequence from fireworks but i dont want to go through all of that. I would appreciate if someonecan tell me how to easily import transparent animated gifs into after effects cc as you could in sc5

    You can't and that is that. This stuff used Quicktime components and simply too much has changed. You need to work with image sequences exported from Photoshop or whatever other program based on your GIF.
    Mylenium

  • Creating transparent gifs - but still retaining some opacity

    Hello,
    I'm having some issues with creating transparent gifs.
    I am creating a box with a single color in it w/ the opacity of 30% but when I save it as a gif file, it makes it completely opaque.
    I would like to save it as a gif but still making it somewhat transparent.
    Is this only possible with PNG files?
    Thank you advance...
    J

    JiggyxJ wrote:
     ...I'm just gonna have to go with PNG but my dev team won't be happy since they have to figure out a way to make the PNG file work with IE6.
    Point those dunderheads in this direction. You'll look like a hero:
    http://24ways.org/2007/supersleight-transparent-png-in-ie6

  • Transparent Gif Background?

    Hello,
    I've been trying to make a gif with a transparent background, however I've been having a few problems with it.
    While the gif does start fine and the background is transparent at the beginning, after a few frames the background color would come up and then after a few frames more it'll be transparent again, then colored again.
    It's really troubling since I've been stalking every forum I could find on the subject, however my research had deemed fruitless.
    The best I could find out is that when I change its palette on the 'Publish settings' to either Web 216 or Web Snap Adaptive--the background stays transparent! However in return, the colors of the gif turn really ugly. The only palette setting I could go with that won't ruin its colors is 'Adaptive' of course, but I really want it to be transparent and it would absolutely not give me that choice. The other option for the palette choice is Custom which also makes the background transparent--however I don't know what palette to use for it since even if I chose the photo itself as a palette resource the colors still come out undesireable. I'm not really sure what the best 'palette choice' is for custom. (do you have a specific file to use for that?)
    Anyways I would love it if I could get some help figuring this out! It seems simple enough, but I really can't for the love of me figure it out.

    Normally in transparent GIF creation only one color can be used to define the transparency.  It sounds like somewhere along the line your background color is changing such that it no longer matches the transparency color.

  • Best way to edit out a white background and make a transparent gif

    I have an image I purchased but need to make it into a gif. It already has a white background but when I use the magic wand it chops little bits of the edge of the images up.
    Is there an easier way of editing the white out of this background and making a transparent gif?
    I am using Adobe Photoshop CS2
    Image is attached to take a look.
    Thanks, I appreciate any help with this.
    Nick

    Worked perfect at a tolerance of 10, didn't know what that was for before
    Thanks!
    Nick

Maybe you are looking for

  • Can I share purchased ibooks between family members?

    My daughter and I have ipads and I'd like to share ibooks...is this possible?

  • Deleted iPod Selection--all songs on iPod disappeared--help!

    I had too many songs in my library to fit on my iPod, so my iPod created a selection of songs. Then I added and deleted many songs to the library. However, when I plugged in my iPod so it would be updated from the revised iTunes library, nothing woul

  • Printer won't quit after printing

    After I print anything from my eMac to the HP laser printer it is hooked up to, the printer icon stays in the dock with the little arrow under it showing that it is open. It won't quit automatically; I have to manually go in and quit the printer. I t

  • HT1688 I can't set up my iphone 3GS correctly

    I had to restore my iphone last night and when it was done restoring i could not set it up.

  • STARTING TO GET REALLY TICKED OFF

    Ok well it seems that alot of the time when i connect my new 3rd gen nano that Widowns and Itunes will accept it but then it will say the battery is completly charged and will stop charing even when its not even half charged. Whats going on?