Drawing transparent pixels into BufferedImage?

Hi,
I'm trying to do this:
1) Create a BufferedImage
2) Paint its background to black
3) Paint a rectangle using a fully-transparent color
4) Output this BufferedImage in the form of a PNG file using ImageIO
My expectation is for step 3 to paint in "transparent" but what seems to be happening is that Graphics interprets my paint command as "draw nothing, it won't change the image anyway". Or maybe the PNG writer in JDK 1.5 does not actually output transparent pixels? Either way, I'm not getting the results I want. Any ideas?
Thanks,
Gili

one should use Graphics.clearRect() for clearing a regionI'm not so sure about that. The API for clearRect states:
...the background color of offscreen images may be system dependent. Applications should use setColor
followed by fillRect to ensure that an offscreen image is cleared to a specific color.
So I tried to do as this suggests:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
public class Test0 {
    public static void main(String[] args) throws IOException {
        int W = 100, H = 100;
        BufferedImage bi = new BufferedImage(W, H, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, W, H);
        g.setColor(new Color(0,0,0,0));
        g.fillRect(W/4, H/4, W/2, H/2);
        g.dispose();
        ImageIO.write(bi, "png", new File("temp.png"));
}...and it doesn't work?! I'm thinking this is a bug. What to do? Can't get enough of that Porter-Duff:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class Test {
    public static void main(String[] args) throws IOException {
        int W = 100, H = 100;
        BufferedImage bi = new BufferedImage(W, H, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, W, H);
        g.setComposite(AlphaComposite.Clear); //<<---- magic pixie dust
        g.fillRect(W/4, H/4, W/2, H/2);
        g.dispose();
        ImageIO.write(bi, "png", new File("temp.png"));
        JLabel lbl = new JLabel(new ImageIcon(bi));
        lbl.setOpaque(true);
        lbl.setBackground(Color.GREEN);
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(lbl);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
}

Similar Messages

  • How to edit transparency pixels in a FilterPlugin?

    I want develop a fiter plugin of photoshop. while i am not clearly about some issues:
    (1) how does the photoshop provide the transparency data ( blank data ) to me ?
    in other words, i want obtain the "alpha" channel data, is this provided by the "inData" together with other channels?
    (2) I want to change the pixel's transparency, means i want change totally transparent pixels into
    opaque. how to do it ?
    (3) in the pluginName.r file,there are 7 entries in FilterCaseInfo like following. is there anything i must pay special attention to?
      FilterCaseInfo
        /* Flat data, no selection */
        inWhiteMat, outWhiteMat,
        doNotWriteOutsideSelection,
        filtersLayerMasks,
        worksWithBlankData,
        copySourceToDestination,
    By the way, When i invoke a filter (i develop it 2 yeas ago) on the transparency document , photoshop gives a messagebox said it canot invoke the fiter because the selection area is totally empty.
    if the area has both transparency and opaque pixels, then the plugin cause write access faled and made the photoshop crash ,  why does this happen, how to avoid it.
    Thanks a lot.!

    First off, don't confuse "alpha" with "transparency" they are different.
    If I create a RGB document with one layer and call the Dissolve example I get the following bits of information in the FilterRecord:
    planes = 4
    imageMode = 3 // plugInModeRGBColor
    filterCase = 4 // filterCaseEditableTransparencyNoSelection
    inLayerPlanes = 3
    inTransparencyMask = 1
    plane 0 = red, 1 = green, 2 = blue, 3 = transparency
    See DoFilter, the loop on the planes, and DissolveRectangle, the changing of the data on each plane, in the Dissolve.cpp example file.
    Now flatten the document and create an alpha channel and select all channels via the channels panel. Selecing just the alpha channel or any one channel will have different results.
    planes = 4
    imageMode = 3 // plugInModeRGBColor
    filterCase = 2 // filterCaseFlatImageWithSelection
    inLayerPlanes = 0
    inTransparencyMask = 0
    inNonLayerPlanes = 4
    There is some documentation about FilterCaseInfo in the Plug-in Resource Guide.pdf found in the documentation folder.

  • How to save GIF frames into BufferedImage with TYPE_BYTE_INDEXED?

    Hi All,
    I have a code that saves every animated GIF frame into BufferedImage of TYPE_INT_ARGB. So an animated GIF will correspond to a list of images with 32-bit pixel size.
    I mainly refer to the code in
    http://forum.java.sun.com/thread.jspa?forumID=20&threadID=629277
    But what I need to do now is to store each frame into TYPE_BYTE_INDEXED image instead. What I'm doing is I get the IndexColorModel from the first frame, and that color model will be shared for all frames.
    As for the frames, I've been trying several ways:
    *1.* read it per usual into 32-bit pixel size image
    sourceImage        = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    RenderedImage renderedImg = reader.readAsRenderedImage(index, null);*reader is the ImageReader for the GIF file.
    then trying to convert the image into 8-bit pixel size by creating new destination image
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, ICM);and then draw the original (32-bit pixel size) onto the newly created image.
    Graphics2D g = destImage.createGraphics();
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
    g.drawImage(sourceImage, imageLeft, imageTop, null);*2.* read it as:
    BufferedImage sourceImage = reader.read(index);then create new 8-bit image and draw the source onto it as No. 1.
    The problem is, for No. 1 , the destination image always lose its transparency. And No. 2 only works well with some GIFs. But mostly the destination image will lose the transparency as well.
    I hope someone more familiar with java imaging can help point out to me where did I do wrong...
    I'm thinking maybe the way I draw the source onto the destination. Or maybe there's another way of getting GIF frames into TYPE_BYTE_INDEXED images..
    Thank you in advanced :)
    Edited by: irma_k on Dec 12, 2007 8:42 PM

    I found that java has weird issues with GIF's (ImageIO wont even write transparency to GIFs when writing to a file).
    But PNGs seemed to work a lot better. Not sure if it will fix your problem, as I don't know all that much about java imaging, but using PNGs seemed to work a lot better for me when I needed to draw transparency.

  • Transparency/Transparent Pixels.

    Okay, I'm trying to get transparency working properly, but it's being a pain. I need to know a few things, which I can't locate online.
    1) Is alpha 0 opaque or is alpha 255?
    2) In the java docs they always refer to alpha as between 0.0 and 1.0; is it correct to assume 1.0 = 255? I remember in Flash it was reversed.
    3) Is transparency(either Bitmask or Transluscent) enabled by default for whatever image/buffer an applet uses?
    4) Is there anything I have to do other than g.setComposite(AlphaComposite.Src); to make it work in theory?
    Here's the thing - if I check the transparent colour on my backbuffer, it has alpha 255. I've got the BufferedImage set up as TYPE_INT_ARGB, and I call setComposite before drawing. Despite that, it insists on copying pixels with 255 alpha when I call drawImage();
    I'm baffled as to why. I tried some Java2D tutorials, and they don't seem to work when I copy the code in and compile it? Well, maybe someone can link me to a tut that applies to Java 1.5/1.6

    Here's an example I threw together, it draws a 100*100 blue square, then a smaller pink (half-alpha red) square in the middle of it.
    package sandbox2;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import javax.swing.JFrame;
    public class TransEx extends javax.swing.JPanel {
        int width = 100;
        int height = 100;
        BufferedImage bimage;
        public TransEx() {
            this.setSize(width, height);
            // Create an image that supports arbitrary levels of transparency
            bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics2D g2d = bimage.createGraphics();
            // Make all filled pixels transparent
            Color blue = Color.BLUE;
            g2d.setColor(blue);
            g2d.fillRect(00, 00, 100, 100);
            Color pink = new Color(255, 0, 0, 63);
            g2d.setColor(pink);
            g2d.fillRect(20, 20, 60, 60);
            g2d.dispose();
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            frame.add(this);
            frame.setSize(200, 200);
            frame.setVisible(true);
        public void paintComponent(Graphics g) {
            g.drawImage(Toolkit.getDefaultToolkit().createImage(bimage.getSource()), 0, 0, this);
        public static void main(String[] args) {
            new TransEx();
    }I was using setComposite(AlphaComposite.Src); on the bimage's graphics context, and it was drawing the pink square as solid red. It worked when I took that out.

  • How to draw transparent boxes in different layers including Size inside box

    See this apps http://www.pixelwindowapp.com/
    How to do this thing inside photoshop using Shapes or from any other method. see added video on this page to understand.
    In photoshop i want to draw transparent boxes (round corner not necessary) in different layers including Size in pixel like this Adobe air apps http://www.pixelwindowapp.com/ do on desktop
    http://www.pixelwindowapp.com/

    Hi Anvano,
    You don't actually have to draw the live features to be able to access the attributes. You can just call getLiveFeatureAttrs(x, y, tolerance) on your MapViewer instance to get hold of the attributes, you can use this in getToolTipText() of your component to show the attributes in a pop-up. No drawLiveFeatures needed! This way you still have to add a theme twice (once for drawing, once for the attributes) but it only gets drawn once, just in the nice style.
    Ida

  • Use GIF with transparent pixels...

    Hi,
    I imported into my Library a gif with some transparent
    pixels.
    basically this gif represents a frozen frame and i would like
    to see
    thru like if user was watching thru frozen window.
    however, when i convert it to symbol, the transparent pixels
    are turned
    to non-transparent.
    how can i do to use transparent pizel under CS3/AS3 ?
    thanks a lot,
    RAF

    Careful with the cross-posting! I answered your post on the
    General Discussion forum.

  • Locking transparent pixels like in phothoshop?

    Hello,
    I am new to A.I ( I have the cs5 version I think)
    is it possible to lock transparent pixels of a layer like in phothoshop in A.I?
    Thank's in advanced.
    Best regards

    Use a mask.
    Either a clipping mask or an opacity mask.
    In Illustrator CS5: draw the mask shape. Then click on the Draw inside button and use what ever drawing tool you like to draw directly in the mask.
    See manual on clipping masks and draw inside mode.

  • Is there a way to lock transparent pixels for more than one layer at a time?

    I've tried selecting multiple layers to modify but the lock transparent pixels box becomes greyed out once I've selected more than one layer. Apologies in advance if this is deemed to be a stupid question but it would be greatly appreciated if someone knows of a solution. It would be very handy for some of the projects I'm currently developing.
    Thanks.

    I have not had a problem in Photoshop CC 2014 as long as I selected a layer with transparency, in my case the gray checkerboard. The button would dim if I selected an adjustment layer.

  • I want to delete all transparent pixels, how can I? CS6

    I would like to delete all transparent pixels surrounding my irregularly (non-square) image.  The TRIM function only trims to a square surrounding my image and not to the edge of the image itself.  Is there a work around for this?  I have looked around and haven't found one. 
    Thanks,
    JB

    Image have rectangular area they have a width and height boundaries as do layer.  Not all of the rectangular area need to have content or be fully opaque,  However the image boundary is a rectangle.  Some file formats are like a Photoshop Background layer do not support empty area or even transparency so those file formats only contain image that are rectangles and have no transparency. Some file formats support transparency and empty areas so those images can be any shape and contain transparency still the image boundary is rectangular. Then there is the GIF file format an oddball format. It only supports 256 Colors. Pixels are mapped to one of the 256 colors selected for the image.  If you want Transparency in a gif  the number of colors is reduced by 1 to 255  the 256 color now indicate no content its not real transparency it just a no color pixel in other words off or no content.
    So if you have areas that are 100% transparent there is nothing to delete the area is empty.  If you have pixels that contain some transparency and you delete those pixels you delete details are you sure you want to delete all pixels that contain transparency? You will loose things like drop shadows

  • I need to convert hundreds of TRANSPARENT PDFs into PNG or TIFF

    I need to convert hundreds of TRANSPARENT PDFs into PNG or TIFF. 
    That's not possible with Acrobat. It’s possible to convert hundreds of PDFs into PNG or TIFF using batch processing. But transparency is LOST! 
    Photoshop is the only software I know of which is able to keep transparency when converting from PDF to PNG.
    But now I have problems to get batch processing running.
    I made an Action with “Open file”, “Save as PSD” and “Export to PNG”. 
    Using this Action in batch-process-files with source as a folder with many PDFs and target as a different folder results in many opened PDFs within Photoshop. Nothing else happens:-(. 
    Any ideas? 
    Thanks
    Norbert

    Great:)
    It worked at once.
    Thank you so much for this link.
    Best regards
    Norbert

  • Need help importing corel draw 10 files into adobe indesign with editing ability.

    I would like to know if anyone knows of a way to import corel draw 10 files into adobe indesign

    Can't edit.
    Best bet..and still a longshot depending on the layout, is to open in
    Illustrator and do the editing there.
    Bob

  • Crop document by transparent pixels

    Hi,
    I have a question on you. I need crop document by transparent pixels.
    But I don't know, how to do it.
    Document has function crop but it need width and height.
    Do you know function with transparent pixels??
    Than you Domaneni

    Here is an example.
    app.activeDocument.trim ( TrimType.TRANSPARENT );// assumes you want to trim all edges

  • About sprite3D with fully transparent pixels

    I have a image with fully transparent pixels. want to use in a sprite3D object. But all of fully transparent pixels is rendered as white. There is no transparent at all. How can I do it or if the Sprite3D is not support transparent?
    best regards and Thanks any reply.
    The init code is :
    Image sprImg = Image.createImage("/spr.png");
    Appearance sprApp = new Appearance();
    Sprite3D spr3D = new Sprite3D(true,new Image2D(Image2D.RGBA,sprImg),sprApp);
    spr3D.setCrop(sprTexX,0,52,128);
    sprTran.setIdentity();
    sprTran.postTranslate(0.f,2.f,5.f);
    sprTran.postScale(2.f,4.f,1.f);

    Hi Sniper_bat,
    I want to do exactly the same thing, did you ever fix this problem?
    Dan

  • Trim Transparent Pixels

    I cropped the photo to 16x20. With this crop I ended up with some transparent pixels on the right and left edges. I used the Trim tool to remove the transparent pixels. Question - after removing the transparent pixels with the Trim tool do I still have a full 16x20 photo ready for printing OR because I chopped off the transparent pixels my photo has shrunk to a slightly smaller size?

    I started with a photo that I cropped to 16x20. With this 16x20 crop I had some transparent pixels. I trimmed the transparent pixels off. By trimming did I decreased the size of the 16x20? I did NOT recrop after the trimming.

  • Why are my transparent pixels becoming opaque (grey)?

    I have PSE 6, and am using it for digital scrapbooking. Some of the elements have transparent pixels, which allows you to see the background and layers beneath the elements. For some reason, when I drag one of these elements onto a background or another element, the transparent pixels become gray, creating an ugly grey box around the element. Also, I see when the new layer is created (on the background), by dragging the element onto the background, it becomes a "frame layer". Not sure exactly what that means, but I am thinking it might have something to do with this problem.   Hopefully someone has a suggestion
    thanks

    Have a look at  #5 in the link below which describes this "feature" and how to get around it.
    http://blog.hummiesworld.com/2007/10/bugs-and-complaints-of-photoshop.html

Maybe you are looking for

  • Photo/Edit-In CS6. psd image does not show in LR after save/close.

    Whenever I edit (a copy) of an image in Photoshop CS6 from Lightroom 4.3 (Photo/Edit-In), after I save and close the image in CS6 the new psd image does not show in Lightroom. In order to see the image in Lightroom, I need to close Lightroom and then

  • Firfox 4 - can we not save TABS when closing?? I just lost everything!

    I installed Firefox 4, had several TABs open before the upgrade. When Firefox 4 opened I had all the TABs. When I wanted to close Firefox there was no SAVE button, so I lost everything. Did a system restore and now when I click the Firefox icon nothi

  • Is it possible to trigger a popup from f:selectItem

    I have a use case, where I have a dropdown (selectOneChoice), which shows a list of existing oracle homes, and last label gives the user an option of 'create new location'. I tried something like below: <af:selectOneChoice id="ohSelector"> <f:selectI

  • Not receiving the email to reset security codes

    I have tried to send the email to myself to change my security questions but have not been receiving them. What is the next step as I don't want to make a new account as I have credits on my current account.

  • TS2200 Cannot find a destination volume

    I am trying to erase and reinstall Snow Leopard on an old MacBook. I installed a new HD 2 years ago and used the computer problem free. I first tried to erase the disk, but I was getting an input/output error. I found on this site that it might take