Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:58:35+00:00 2026-05-27T04:58:35+00:00

I have a method to replace a pixel of one color with transparency public

  • 0

I have a method to replace a pixel of one color with transparency

public Bitmap createTransparentBitmapFromBitmap(Bitmap bitmap,
          int replaceThisColor) {
        if (bitmap != null) {
          int picw = bitmap.getWidth(); 
          int pich = bitmap.getHeight();
          int[] pix = new int[picw * pich];
          bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

          int sr = (replaceThisColor >> 16) & 0xff;
          int sg = (replaceThisColor >> 8) & 0xff;
          int sb = replaceThisColor & 0xff;

          for (int y = 0; y < pich; y++) {   
            for (int x = 0; x < picw; x++) {
              int index = y * picw + x;
            /*  int r = (pix[index] >> 16) & 0xff;
              int g = (pix[index] >> 8) & 0xff;
              int b = pix[index] & 0xff;*/

              if (pix[index] == replaceThisColor) {

                if(x<topLeftHole.x) topLeftHole.x = x;  
                if(y<topLeftHole.y) topLeftHole.y = y;
                if(x>bottomRightHole.x) bottomRightHole.x = x;
                if(y>bottomRightHole.y)bottomRightHole.y = y;

                pix[index] = Color.TRANSPARENT;   
              } else {
                //break;
              }
            }
          }

          Bitmap bm = Bitmap.createBitmap(pix, picw, pich,
              Bitmap.Config.ARGB_8888);  

          return bm;
        }
        return null;
      }

Its called like this

backgroundBitmap = createTransparentBitmapFromBitmap(backgroundBitmap , Color.argb(255,255,255, 0));

The same color i have in a png file, where i want the transparent hole.The problem is it only replaces part of the color not all.
See screenshot https://docs.google.com/document/d/18aH43sFmsuuRu0QNfMTD1zek8sqWwH_pTauFofDZeIw/edit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:58:36+00:00Added an answer on May 27, 2026 at 4:58 am

    It looks like your picture has JPEG artifacts in it. Detecting an exact color will only really work if you only save your picture in a lossless format. PNG is a lossless format, but did you save an intermediate version of your picture as a JPEG (or in some other lossy format) after drawing the circle?

    Also; just a tip: you don’t need two loops, just loop through the array in one for loop.

    Edit: This new yellow looks like it’s been caused by anti-aliasing. Due to that, the edges of the circle aren’t the exact color you’re looking for and your code misses them. The only way around this is to turn off anti-aliasing while drawing the circle. Of course, this way you also won’t get a nice anti-aliased edge for your transparent hole.

    If you want that, you’ll probably have to use a separate mask for the hole (a JPEG for color and an 8-bit PNG for transparency should be a quite efficient combination – oh how much I wish there was an image format that readily allowed this in web browsers)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a method like this on my DocumentFilter public void replaceUpdate(int
i have prepared one custom view using onDraw method, my view class is, public
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
What is the best method to replace my urls on Render. I can have
I have this method: public static Expression<Func<MyEntity, bool>> MyMethod(string someId) { return o =>
I have a method to replace every character except those I specify. For example,
I know we have a replace method to replace a substring within a string
I have this method: function replaceRightClickIcefacesMethod() { var oldName = jQuery(.singlePaneOfGlassBlock).attr(oncontextmenu); oldName = oldName.replace('Ice.Menu.contextMenuPopup',
In a customer class loader, I have a method findClass as follows: public Class
I have a method called isStringOnlyWhitespace(): public static bool isStringOnlyWhitespace(string toEval) { string stringNoWhitespace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.