Make image transparent outside of selected area.

I have some code that paints a BufferedImage to screen with ability to draw a single rectangle in the image at any location.
I want the part of the image that is not in the rectangle to have some transparency.
How can I accomplish this?
my Current code is:
    public void paint(Graphics g) {
         Graphics2D g2 = (Graphics2D) g;
         g2.clearRect(0, 0, this.getWidth(), this.getHeight());
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
         if (snapshot != null) {
            g2.drawImage(snapshot, 0, 0, this);
         g2.setStroke(dashed);
         g2.setColor(Color.WHITE);
         rectX = start.getX();
         rectY = start.getY();
         rectWidth = end.getX() - start.getX();
         rectHeight = end.getY() - start.getY();
         if (rectWidth < 0) {
            rectX += rectWidth;
            rectWidth *= -1;
         if (rectHeight < 0) {
            rectY += rectHeight;
            rectHeight *= -1;
         g2.draw(new Rectangle2D.Double(rectX, rectY, rectWidth, rectHeight));
      }start and end are of type Point.
snapshot is a BufferedImage

You can draw portions of an image with the other graphics#drawImage methods. Most notably this one
drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
                     int sx1, int sy1, int sx2, int sy2,
                     ImageObserver observer)So you can draw the four regions of the image not included in the rectangle with a translucent AlphaComposite using the above method (4 times). And then you can use it one more time to draw the rectangular region with an opaque AlphaComposite.
Also, if start is of type Point then your rectangular coordinates are integers. This implies you can use g2.drawRect instead of creating a new Rectangle2D.Double object each paint call. It's just a side not though. It dosen't really matter much.
Edited by: Maxideon on Apr 6, 2009 11:25 PM
Too late, seems you found a way.
Edited by: Maxideon on Apr 6, 2009 11:26 PM
Ah, foolish me. I was thinking of the hard way. You can just draw the whole image at a certain transparency. And then draw the rectangular region as opaque.

Similar Messages

  • How do you make images transparent in inDesign?

    How do you make images transparent in Adobe InDesign CS5.5?

    Dittco wrote:
    It IS possible: Select the image. Open effects (Window > Effects). Switch from "normal" to "multiply". Viola! White background is gone.
    That only works with a white background. In addition, it introduces a transparency effect, with its associated problems.
    With a rather light background: Select the image. Open clipping path (Object > Clipping Path > Options). Select Type > Detect Edges; use Threshold and Tolerance to get as close as possible to the edge of your image. This may be difficult because of a too low resolution image, or too much fringe on the edge -- if all fails, use the Inset Frame value to force the mask "into" the image. Click OK, and the background is gone.
    But that's not the only way: you can always create a clipping path manually, and then you are in total control, not limited by transparency side effects, auto-edge detection, and busy backgrounds.

  • Selecting outside a selected area?

    I select and area using the elliptical marquee tool and if I try to delete, it deletes the inside area. I used to know a keyboard shortcut to change the selection to outside the selected area so I would be left with what is in the elliptical area, but I have forgotten it. Can someone please help me with this? Thank you.

    You are probably thinking of Inverse Selection: Shift + Ctrl + i (PC) / Shift + Cmd + i (MAC), that will invert the selection. Also available under the Select drop down menu.

  • Is there a way to make images transparent in Photoshop Elements 12?

    I just purchased the new software and don't have any experience with this program.  I'm trying to find out if I can make the background of images transparent.
    Does anyone know?

    Yes. First you must double-click the background layer to convert it to a regular layer. Then use whatever selection tools are appropriate to select your background and press delete. Unless you've you've changed default settings you should see a grey & white checker pattern - this denotes transparency.
    When saving you must save it to a format that supports transparency. For GIF and PNG use the File> Save for Web and make sure the transparency checkbox is ticked. For TIF use File> Save As and select the TIF format. On the same dialog that offers compression options, there is a transparency checkbox.
    Cheers,
    Neale
    Insanity is hereditary, you get it from your children
    If this post or another user's post resolves the original issue, please mark the posts as correct and/or helpful accordingly. This helps other users with similar trouble get answers to their questions quicker. Thanks.

  • How to make image transparent to background in Web Dynpro...

    Hi Experts,
    IN our web dynpro application, we need to have some fonts and sizes of texts to be displayed on the layout. Since web dynpro has limited options for the design and color of a Textview, we have decided to use the image instead.
    This image has exactly the same background color as that of web dynpro and is matching perfectly.
    But the problem is, it shows a border surrounding the image. We need to remove that border and make it completely transparent to the web dynpro background.
    I have tried different design options for Image element but it did not worked.
    Has anyone done this earlier..
    Regards,
    Anand

    Thanks Nitesh,
    I have downloaded the MIME image and it doesn't appear to have the border. This border is added by web dynpro application itself.
    Regards,
    Anand

  • How can I make images transparent in my documents?

    I've got InDesign CS5.5 and like to save my banners, etc. that I create as a jpeg but when I try to use them on something else it has a white background.  How can I make sure the backgrounds are transparent?

    I didn't know that about the png in CS6.  Thanks for letting me know.
    Unfortunately, I don't have Photoshop.  I just recently purchased Photoshop Elements.  I haven't really done much on it as I have other projects I need to work on. Do you know if I would be able to convert in that program?
    Wish I could afford a newer version of InDesign but I can't.

  • How to crop selected area?

    Is it possible to crop the selected area of a page, because now if i go to Tools>Pages>Crop it can only crop the area that is outside the selected area?
    Thanks!

    This delete the content inside the selected area but doesn't crop the area, but i guess i could use it.
    How can i add content from the clipboard and retain the format? I am trying to create pdf file with content that i copy from html pages but when i paste the text it paste it as plain text, doesn't keep the format. 

  • Epson scan documents but cant select area!

    Epson Stylus CX5500 Snow Leopard latest 10.6.1
    I can scan some pages but but some pages the software that comes with snow leopard selects an area and only scans that. I turned off detect area and makes no difference. If I go to the show details page It says there was an error. It still allows me to make adjustments in the like select area or scan black and white but it stills selects whatever area it wants to and wont let me simply scan a whole page most of the time.
    I plugged the printer into my wifes brand new macbookpro and it does exactly as described above and that uses the snow leopard drivers the system auto installs as well so it can be a problem with the computer
    thanks

    For any generic Apple driver problem, this is the procedure to sort things out :
    Found the solution though :
    1 - Reinitialize Printers
    2 - Re-install all drivers from Epson
    3 - Reconfigure printer by clicking "+" in the System Preferences > Printers and Fax.
    4 - Be VERY careful to choose your printer in the menu, as it will appear under Apple driver
    and under Epson name. For mine, Epson name ended with TCP/IP (even though I was configuring in Wifi).
    5 - Click and it's done. I have again the draft options and so on.
    I did it for one Epson Multifunction and one Samsung laser printer. It works.
    Message was edited by: Hellstan

  • I have 4 equal oblong shape created with borders How do I go about knowing what size the selection area is so that I can crop an image to fit. I don't want to use paste in then adjust the bounding box to suit

    I have 4 equal oblong shape created with borders How do I go about knowing what size the selection area is so that I can crop an image to fit. I don't want to use paste in then adjust the bounding box to suit

    What do you mean a moderator

  • I am trying to select more than one of my images to order prints?  What button do I hold down to make my computer let me select more than one at a time?

    I want to order prints of my images but can only select one at a time?  How do I get my computer to let me select several prints to order?

    If you hold the Ctl (Command) key while clicking the files you want, you can select multiple individual files. If there are numerous files one after the other or a bulk batch you want to select, if you click the first file to select, then hold down the Shift key and click the last file you want to select, everything between the first file selected and the last file selected will be highlighted.
    Hope this helps

  • I just bought an Epson Stylus Pro 3880 and the printed images look yellow. I calibrate my MacBook Pro monitor with a ColorMunki Display. I want to make sure my Lightroom print settings are correct.

    I just bought an Epson Stylus Pro 3880 and the printed images look yellow. I calibrate my MacBook Pro monitor with a ColorMunki Display. I want to make sure my Lightroom print settings are correct.

    This what I have been trying......  Maybe my eyes are screwed up or don't understand the process....
    What I have been trying to do is squint and make the apple disappear by moving the sliders around in each step. Is that correct?

  • How can I blur a selection on a layer (gaussian blur) without the colors/transparency outside the borders of the selection bleeding through?

    I am a digital artist, and I switch between programs a lot because sometimes photoshop just doesn't do everything, but I would really like to know if photoshop can help me with this. Is there a way for me to blur a selection without the edges becoming transparent (or color from outside the selection bleeding in). This really bothers me, and my other programs don't have this issue.

    Would align to pixel grid help?
    Left is not aligned, Right is aligned to pixel grid

  • Select a small area and then use the selected area to paint other areas of the image

    Hello
    My problem is a little hard to explain but I try.
    What I want is to select a small area and then use the selected area to paint other areas of the image. Do not know how to do it and if it's stamp tools or pen tools to be used?

    Howdy.
    Sounds like you're looking for the Clone Stamp Tool. Set to Aligned, the sampling point resets when you release the mouse after painting. Untic Aligned and the sampling point stays in the original spot for the next stroke. It's a good idea to clone onto a separate layer with Sample All Layers selected. Then you don't lose original pixels. Allows you a do over later.
    FWIW.
    Peace,
    Lee

  • Make a color in image transparent on the fly

    Hi I am trying to load an image and at runtime pick the white
    background of the image as transparent. Can any one point me to the
    flex/flash libraries that may help w/ this? basically I have 2
    images both of them have white background. I want to be able to
    drag and drop them so that the main objects of the images are side
    by side. But the problem is each image has thick white borders. so
    i figure if i can dynamically choose white color in the image to be
    transparent then I can overlap them and the white borders will be
    transparent and won't get in the way. Any expert can point me to
    the right direction?

    doug777, thanks for the reply. My issue is that the image I
    am trying to place next to each other is some images off the web
    i.e. I don't have control over the image transparent color. that's
    why I want to find a way to on the fly set which color in the
    images will be transparent. I was hoping flex has some library for
    that

  • When using image processor my images to select are grayed out

    when using image processor my images to select are grayed out, when they wernt yesterday? any clues? Have I done something stupid!
    many thanks
    carol

    Nothing to do with professional or not, but I do hope you know where your computer's power switch is. System information can be obtained this way:
    Working with your Operating System’s Tools
    Furthermore we need info where you store your files, what format they are in, where they came from, what you did the last time this worked, if possible...
    Mylenium

Maybe you are looking for

  • InDesign for pic in email html?

    Hi. I picked up the adobe a few months back to create business cards and fliers etc... and maybe do some photoshop things. I have the cs4 package with fireworks,bridge,photo shop,illustrator, indesign etc.... The cards and fliers worked out well. I c

  • Action script problem

    I have an .as file with this code within it so when the send_pb button is pushed then onSendMessage causes the message to be sent.....I'd like the enter key on the keyboard to do the same thing. I tried various ways to do it within the .as file but n

  • Not seeing details in tcpdump of port 1521

    Hi: Not strictly an Oracle question but there's a strong Oracle tie-in. So... I'm trying to verify that encryption between my client and server is happening by using tcpdump and was just wondering has anyone else had done this? I see packet data but

  • Upgrade CTM or Downgrade CTC ?

    Hi all, we've been implement DWDM ring consist of 4 nodes ONS15454SDH (ver 7.2) and recently we've added a new DWDM ROADM consist of 5 nodes (ver. 8.51). currently we're using CTM 8.0 to monitor existing ONS ver 7.2, but with new added nodes ver 8.51

  • Passing variables from php to flash and the opposite

    Hi guys, im trying weeks now to solve this problem but nothing yet If someone could just tell me how to pass variables from flash to php and the opposite i would be thankful!!! Please help!