locked
Extract the color from bitmap into arry or Hashtable or................. RRS feed

  • Question

  • User1260372307 posted

    Hello Every one

    I am developing an application using c# where I need to upload and read the whole  image  pixels and do the following: 

    1.      Extract the color from  bitmap into arry  or Hashtable or.................2.      Find the top five repeated colors and remove the rest.  

    Any help is appreciated

    Thanks

    Tuesday, January 27, 2009 10:27 AM

Answers

  • User-1760427068 posted

     Phase II put white pixels everywhere that a color wasn't one of the top five.  Phase III puts transparency wherever a color is not one of the top five colors.

    Phase III works.  It has one line of code that I can't explain.

    It turns out we were already using argb for color (alpha red green blue) even though we were only putting in three values--apparently it assumes a=255 if you have only three values and it assumes they are RGB).

    For no transparency, alpha is 255.  If we want complete transparency, alpha is 0--we made a simple change from Phase2 to Phase3 of changing the declaration of white from

    255, 255, 255

    to being

    0, 255, 255, 255

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 11:14 PM

All replies

  • User-1760427068 posted

    The code posted below iterates through the bitmap and looks at colors then changes them to grayscales.  With some modification you could get the RGB values at each point and put them into a storage (3 ArrayLists, build a class and then just have one ArrayList, etc.)

    From what you said, I'm thinking you might want to has some level of tolerance within which you say two colors are "the same" and maybe have a second ArrayList that to keep score, and each pixel is compared against all others to count how many are "within color range" and then at the end you look for your top scorers (and probably has something so deal with the fact that several of them grouped together are also considered to be "the same color".

    The above may seem to be not exactly simple, but I think we can do it and it gives me something to think about while on the treadmill this evening if you are interested in trying this.  Here is the code:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;  //Bitmap needed this
    using System.Drawing.Imaging; //ImageFormat needed this
    using System.IO; // .delete needed this
    
    public partial class Color2BW : System.Web.UI.Page
    {
       public static Bitmap Grayscale(Bitmap bitmap)
        {
            //Declare myBitmap as a new Bitmap with the same Width & Height
            Bitmap myBitmap = new Bitmap(bitmap.Width, bitmap.Height);
    
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int x = 0; x < bitmap.Height; x++)
                {
                    //Get the Pixel
                    Color BitmapColor = bitmap.GetPixel(i, x);
                    //I want to come back here at some point and understand, then change, the constants
                    //Declare grayScale as the Grayscale Pixel
                    int grayScale = (int)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));
    
                    //Declare myColor as a Grayscale Color
                    Color myColor = Color.FromArgb(grayScale, grayScale, grayScale);
    
                    //Set the Grayscale Pixel
                    myBitmap.SetPixel(i, x, myColor);
                }
            }
            return myBitmap;
        }
        protected void AddPictureButton_Click(object sender, EventArgs e)
        {
            if (PictureUploadControl.HasFile)
            {
                PictureUploadControl.SaveAs(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName);
    
                Bitmap oldBitmap = new Bitmap(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName, false);
    
                Bitmap newBitmap = Grayscale(new Bitmap(oldBitmap)); 
                string name = "grayscale";
                newBitmap.Save(Server.MapPath("~/images/") + name + ".jpg", ImageFormat.Jpeg);
    
                oldBitmap.Dispose();
    
                //we will delete the old
                File.Delete(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName);
    
                GrayscaledPhoto.ImageUrl = "images/" + name + ".jpg";
            }
        }
    }

     

     

     

    I probably should post the front page code to -- also, credit for this goes to Danny, a friend of mine.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Color2BW.aspx.cs" Inherits="Color2BW" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    
        <p>Please upload your picture: 
        <asp:FileUpload id="PictureUploadControl" Width="400" runat="server" />        
        <asp:Button runat="server" id="AddPictureButton" text="Upload" onclick="AddPictureButton_Click" /><br />
        </p> 
        
        <asp:Image ID="GrayscaledPhoto" runat="server" /><br />
    
        </form>
    </body>
    </html>
      
    Tuesday, January 27, 2009 12:30 PM
  • User-1760427068 posted

    I have this vague memory of being told you can't do something within

    public static Bitmap Grayscale(Bitmap bitmap)

    like send information to the front page (like sending info to a Label as mylabel.Text = something or sending info to a div like mydiv.InnerHtml = something) -- the public static will only do its duty to return a Bitmap.

    However, you can copy code within it and paste in the other loop and do whatever you want with it there (which is my plan).

    Stage 1- Get the RGB values into a container (and at this point you have responded and told me your preference for "storage"--I will assume three ArrayLists with names like ArrayListR, ArrayListG, ArrayListB.

    Stage 2- Build a subroutine that goes through each point and calls it "point1" and then loops through all the points and calls them "point2" and checks if they are within a tolerance (you can say the tolerance has to be zero difference if you want). Count a score for each and put it in ArrayListScore.

    Stage 3- Have a storage container that first off, takes the color with the highest score and defines it as color zero.  It then iterates down the list (in order of descreasing score) and does the following:

    1. It takes the current color and compares it to all the colors on this new list.  If it finds a match then it takes the score for the current color and gives that score to color on the new list that is a match.
    2. If it doesn't find a match and it reaches the end then it takes that current color and adds it to new list (and that color keeps its score).  This process assimilates similar colors together and I'm guessing this is a step better than counting very similar colors as separate entities.

    Do you have a deadline on this?

    Tuesday, January 27, 2009 12:46 PM
  • User1260372307 posted

    Thank you so much

    I was able to upload the image and extract the colors like what you did in your Grayscale Code, but i still need to put them into Array and get the most top five, later i am gona save them to a storge( but i prefer to  convert the RGB into HTM  Color and then save  them as HTML,   i have the convertion code  , no prblem with that)  , the deadline on it is this Friday.

    Thanks

    Tuesday, January 27, 2009 1:31 PM
  • User-1760427068 posted

    Good -- That the deadline is Friday.  I live in the time zone including New York City and it is Tuesday here.

    Quick question -- I don't know your background -- if I say the distance between two points in 3D space do you think back to the formula

    distance equals square root of the sum of the squares x2-x1, y2-y1, z2-z1 if our points are x1,y1,z1 and x2,y2,y2?

    If it's been a while since you were in school and the above isn't familiar then I would want to send you to a web site that talks about this and shows it with graphics because we'll need it for what we're doing.

    Oh, and I need to ask: if one color is R=100, G=100, and B=100 and the next is R=100,G=100 and B=99 do you want to say that these are two different colors or do you want tolerance testing that says they are the same.

    The reason I ask, I've worked with Adobe Photoshop for years and when looking at Color Palettes I know often a color will show up several times as very slightly different versions of itself--I'm guessing (and possibly wrong) that you want these to come in as one color.

    Tuesday, January 27, 2009 1:46 PM
  • User1260372307 posted

    Thanks  for your quick reply

    I live in  Eastern Time Zone

    -  I have no problem with Math

    -   R=100,G=100 and B=99  is the nearest color for  R=100, G=100, and B=100

    - Now i need to get the exact color

    Now I need to get the exact colors then  convert them into html,  put them into an Array  get the top five colors , put them into database .

     

    I am gona use tolerance later for getting nearest or complementary  color.

    Have a good afternoon

    Tuesday, January 27, 2009 2:16 PM
  • User-1760427068 posted

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Phase1.aspx.cs" Inherits="Phase1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Phase I</title>
        <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        <h1>Phase I</h1>
    
        
        <p>Please upload your picture: 
        <asp:FileUpload id="PictureUploadControl" Width="400" runat="server" />        
        <asp:Button runat="server" id="AddPictureButton" text="Upload" onclick="AddPictureButton_Click" /><br />
        </p> 
        
        <div id="results" runat="server" />
        
        <div id="topfiveresults" runat="server" />
        
        <div id="allresults" runat="server" />
        
        <asp:Image ID="GrayscaledPhoto" runat="server" /><br />
        
        </div>
        </form>
    </body>
    </html>
     
     

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;  //Bitmap needed this
    using System.Drawing.Imaging; //ImageFormat needed this
    using System.IO; // .delete needed this
    using System.Collections; // ArrayList needed this
    
    public partial class Phase1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void AddPictureButton_Click(object sender, EventArgs e)
        {
            if (PictureUploadControl.HasFile)
            {
                PictureUploadControl.SaveAs(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName);
    
                Bitmap oldBitmap = new Bitmap(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName, false);
    
                List<pixelpoint> originalpixels1 = new List<PIXELPOINT>();
    
                for (int i = 0; i < oldBitmap.Width; i++)
                {
                    for (int x = 0; x < oldBitmap.Height; x++)
                    {
                        //Get the Pixel
                        Color BitmapColor = oldBitmap.GetPixel(i, x);
                        //put the integer values into a pixelpoint class
                        pixelpoint pp = new pixelpoint(BitmapColor.R, BitmapColor.G, BitmapColor.B, 0);
                        //add the pixelpoint to the ArrayList
                        originalpixels1.Add(pp);
                    }
                }
    
    
                List<PIXELPOINT> uniquecolors2 = new List<PIXELPOINT>();
    
                int matchfound = 0;
    
                foreach (pixelpoint p in originalpixels1)
                {
                    pixelpoint pixel2 = p;
                    if (uniquecolors2.Count > 0) 
                    {
                        //a) we first need to check if there is a match (if match then the color is not unique and
                        //it will be assimilated
                        //
                        //b) if we don't get a match then the color is unique and it will be added as a new color
    
                        int count2 = uniquecolors2.Count;
    
                        matchfound = 0; //if no match then the color is unique
                        for (int d2 = 0; d2 < count2; d2++)
                        {
                            pixelpoint uniquecolor2 = (pixelpoint)uniquecolors2[d2];
                            if ((uniquecolor2.R == pixel2.R) && (uniquecolor2.G == pixel2.G) && (uniquecolor2.B == pixel2.B))
                            {
                                ///I don't like the next line of code...
                                uniquecolor2.Score += 1;
                                matchfound = 1;
                            }
                        }
                        if (matchfound == 0)
                        {
                            pixelpoint addpixel = new pixelpoint(pixel2.R, pixel2.G, pixel2.B, 1);
                            uniquecolors2.Add(addpixel);
                        }
                    }
                    else
                    {
                        //this loop is used once, at the very beginning, to add the first color
                        pixelpoint addpixel = new pixelpoint(pixel2.R, pixel2.G, pixel2.B, 1);
                        uniquecolors2.Add(addpixel);
                    }
                  }
    
                int finalcount2 = uniquecolors2.Count;
                results.InnerHtml += "<br /> Number of unique colors is " + finalcount2 + "<br /><br />";
    
    
                allresults.InnerHtml = "<br /><br />All results:<br /><br />";
                //the g loop will show all
                foreach (pixelpoint p in uniquecolors2)
                {
                    pixelpoint thepixel = p;
                    allresults.InnerHtml += " R- " + thepixel.R + " G- " + thepixel.G + " B- " + thepixel.B + " score- " + thepixel.Score + "<br />";
                }
    
                topfiveresults.InnerHtml = "The top five are shown:<br /><br />";
                //the h loop will just show the top five
    
                PixelSort psort = new PixelSort();
                uniquecolors2.Sort(psort);
                uniquecolors2.Reverse();
    
                ////this loop will take colors from the Array with indices of 0,1,2,3,4
                ////something should be built in prior to deals with a scenario where a picture
                ////has less than five unique colors
                for (int h = 0; h < 5; h++)
                {
                    pixelpoint thepixel = (pixelpoint)uniquecolors2[h];
                    topfiveresults.InnerHtml += "h is " + h + " R- " + thepixel.R + " G- " + thepixel.G + " B- " + thepixel.B + " score- " + thepixel.Score + "<br />";
                }
    
                oldBitmap.Dispose();
              }
          }
    }
     
    Wednesday, January 28, 2009 11:18 AM
  • User-1760427068 posted

    The next phase of work is to have the program create a new bitmap that has transparent pixels for each point where it was occupied by a color not present in the top five colors.

    Correct me if I am wrong here on this next step.

    Right now my goal is to get this finished and working.  Certainly we will spend a little time discussing it afterwards (better to discuss with a finished product in hand).

    Wednesday, January 28, 2009 4:35 PM
  • User-1760427068 posted

      

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Phase3.aspx.cs" Inherits="Phase3" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    <h1>Phase III</h1>


    <p>Please upload your picture:
    <asp:FileUpload id="PictureUploadControl" Width="400" runat="server" />
    <asp:Button runat="server" id="AddPictureButton" text="Upload" onclick="AddPictureButton_Click" /><br />
    </p>

    <p><asp:Image ID="Image1" runat="server" /></p>

    <p><asp:Image ID="Image2" runat="server" /></p>

    <div id="results" runat="server" />

    <div id="topfiveresults" runat="server" />

    <div id="allresults" runat="server" />

    </div>
    </form>
    </body>
    </html>
     
    Wednesday, January 28, 2009 11:04 PM
  • User-1760427068 posted

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;  //Bitmap needed this
    using System.Drawing.Imaging; //ImageFormat needed this
    using System.IO; // .delete needed this
    using System.Collections; // ArrayList needed this
    
    public partial class Phase3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        public Bitmap AddWhite(Bitmap bitmap)
        {
            ArrayList colorsarraylist = new ArrayList();
    
            if (Session["colors"] != null)
            {
                colorsarraylist = (ArrayList)Session["colors"];
            }
    
            //Declare myBitmap as a new Bitmap with the same Width & Height
            Bitmap myBitmap = new Bitmap(bitmap.Width, bitmap.Height);
    
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int x = 0; x < bitmap.Height; x++)
                {
                    //Get the Pixel
                    Color BitmapColor = bitmap.GetPixel(i, x);
    
                    Color myColor = Color.FromArgb(0, 255, 255, 255);
                    for (int c = 0; c < 5; c++)
                    {
                        //results.InnerHtml += c.ToString();
                        pixelpoint p = new pixelpoint(0, 0, 0, 0);
                        p = (pixelpoint)colorsarraylist[c];
                        if ((BitmapColor.R == p.R) && (BitmapColor.G == p.G) && (BitmapColor.B == p.B))
                        {
                            myColor = Color.FromArgb(255, BitmapColor.R, BitmapColor.G, BitmapColor.B);
                        }
                    }
                    myBitmap.SetPixel(i, x, myColor);
                    //results.InnerHtml += "i- " + i + " x- " + x + " now R is " + myColor.R + " G is " + myColor.B + " B is " + myColor.B + " <br /&gt;";
                }
            }
            return myBitmap;
        }
    
        protected void AddPictureButton_Click(object sender, EventArgs e)
        {
            if (PictureUploadControl.HasFile)
            {
                PictureUploadControl.SaveAs(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName);
    
                Bitmap oldBitmap = new Bitmap(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName, false);
                Image1.ImageUrl = "~/images/ORIGINAL" + PictureUploadControl.FileName;
    
    
                List<pixelpoint> originalpixels1 = new List<pixelpoint>();
    
    
                for (int i = 0; i < oldBitmap.Width; i++)
                {
                    for (int x = 0; x < oldBitmap.Height; x++)
                    {
                        //Get the Pixel
                        Color BitmapColor = oldBitmap.GetPixel(i, x);
                        //put the integer values into a pixelpoint class
                        pixelpoint pp = new pixelpoint(BitmapColor.R, BitmapColor.G, BitmapColor.B, 0);
                        //add the pixelpoint to the ArrayList
                        originalpixels1.Add(pp);
                    }
                }
    
    
                List<pixelpoint> uniquecolors2 = new List<pixelpoint>();
    
                int matchfound = 0;
    
                foreach (pixelpoint p in originalpixels1)
                {
                    pixelpoint pixel2 = p;
                    if (uniquecolors2.Count > 0) //discardable comment--I hope we can .Count a Generic List
                    {
                        //a) we first need to check if there is a match (if match then the color is not unique and
                        //it will be assimilated
                        //
                        //b) if we don't get a match then the color is unique and it will be added as a new color
    
                        int count2 = uniquecolors2.Count;
    
                        matchfound = 0; //if no match then the color is unique
                        for (int d2 = 0; d2 < count2; d2++)
                        {
                            pixelpoint uniquecolor2 = (pixelpoint)uniquecolors2[d2];
                            if ((uniquecolor2.R == pixel2.R) && (uniquecolor2.G == pixel2.G) && (uniquecolor2.B == pixel2.B))
                            {
                                ///ALERT!!! There is something I don't like about the next line of code
                                uniquecolor2.Score += 1;
                                matchfound = 1;
                            }
                        }
                        if (matchfound == 0)
                        {
                            pixelpoint addpixel = new pixelpoint(pixel2.R, pixel2.G, pixel2.B, 1);
                            uniquecolors2.Add(addpixel);
                        }
                    }
                    else
                    {
                        //this loop is used once, at the very beginning, to add the first color
                        pixelpoint addpixel = new pixelpoint(pixel2.R, pixel2.G, pixel2.B, 1);
                        uniquecolors2.Add(addpixel);
                    }
                }
    
                int finalcount2 = uniquecolors2.Count;
                results.InnerHtml = "&lt;br /> Number of unique colors is " + finalcount2 + "&lt;br /><br />";
    
    
                allresults.InnerHtml = "&lt;br /><br />All results:<br /><br />";
                //the g loop will show all
                foreach (pixelpoint p in uniquecolors2)
                {
                    pixelpoint thepixel = p;
                    allresults.InnerHtml += " R- " + thepixel.R + " G- " + thepixel.G + " B- " + thepixel.B + " score- " + thepixel.Score + "&lt;br />";
                }
    
                topfiveresults.InnerHtml = "The top five are shown:<br /><br />";
                //the h loop will just show the top five
    
                PixelSort psort = new PixelSort();
                uniquecolors2.Sort(psort);
                uniquecolors2.Reverse();
    
                ////this loop will take colors from the Array with indices of 0,1,2,3,4
                ////something should be built in prior to deals with a scenario where a picture
                ////has less than five unique colors
    
                ArrayList topfivecolor = new ArrayList();
                // results.InnerHtml += "count for uniquecolors2 is " + uniquecolors2.Count + "<br />";
                for (int h = 0; h < 5; h++)
                {
                    //results.InnerHtml += "h is " + h + "<br />";
                    pixelpoint thepixel = (pixelpoint)uniquecolors2[h];
                    topfiveresults.InnerHtml += "h is " + h + " R- " + thepixel.R + " G- " + thepixel.G + " B- " + thepixel.B + " score- " + thepixel.Score + "&lt;br />";
                    topfivecolor.Add(thepixel);
                }
                Session["Colors"] = topfivecolor;
    
                Bitmap newBitmap = AddWhite(new Bitmap(oldBitmap));
                string name = "whiteaddedPNG";
                newBitmap.Save(Server.MapPath("~/images/") + name + ".png", ImageFormat.Png);
                Image2.ImageUrl = "~/images/whiteaddedPNG.png";
    
                oldBitmap.Dispose();
            }
        }
    }</pixelpoint></pixelpoint></pixelpoint></pixelpoint>
     
    Wednesday, January 28, 2009 11:06 PM
  • User-1760427068 posted

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;  //Bitmap needed this
    using System.Drawing.Imaging; //ImageFormat needed this
    using System.IO; // .delete needed this
    using System.Collections; // ArrayList needed this
    
    public partial class Phase3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        public Bitmap AddWhite(Bitmap bitmap)
        {
            ArrayList colorsarraylist = new ArrayList();
    
            if (Session["colors"] != null)
            {
                colorsarraylist = (ArrayList)Session["colors"];
            }
    
            //Declare myBitmap as a new Bitmap with the same Width & Height
            Bitmap myBitmap = new Bitmap(bitmap.Width, bitmap.Height);
    
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int x = 0; x < bitmap.Height; x++)
                {
                    //Get the Pixel
                    Color BitmapColor = bitmap.GetPixel(i, x);
    
                    Color myColor = Color.FromArgb(0, 255, 255, 255);
                    for (int c = 0; c < 5; c++)
                    {
                        //results.InnerHtml += c.ToString();
                        pixelpoint p = new pixelpoint(0, 0, 0, 0);
                        p = (pixelpoint)colorsarraylist[c];
                        if ((BitmapColor.R == p.R) && (BitmapColor.G == p.G) && (BitmapColor.B == p.B))
                        {
                            myColor = Color.FromArgb(255, BitmapColor.R, BitmapColor.G, BitmapColor.B);
                        }
                    }
                    myBitmap.SetPixel(i, x, myColor);
                    //results.InnerHtml += "i- " + i + " x- " + x + " now R is " + myColor.R + " G is " + myColor.B + " B is " + myColor.B + " <br />";
                }
            }
            return myBitmap;
        }
    
        protected void AddPictureButton_Click(object sender, EventArgs e)
        {
            if (PictureUploadControl.HasFile)
            {
                PictureUploadControl.SaveAs(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName);
    
                Bitmap oldBitmap = new Bitmap(Server.MapPath("~/images/ORIGINAL") + PictureUploadControl.FileName, false);
                Image1.ImageUrl = "~/images/ORIGINAL" + PictureUploadControl.FileName;
    
    
                List<pixelpoint> originalpixels1 = new List<pixelpoint>();
    
    
                for (int i = 0; i < oldBitmap.Width; i++)
                {
                    for (int x = 0; x < oldBitmap.Height; x++)
                    {
                        //Get the Pixel
                        Color BitmapColor = oldBitmap.GetPixel(i, x);
                        //put the integer values into a pixelpoint class
                        pixelpoint pp = new pixelpoint(BitmapColor.R, BitmapColor.G, BitmapColor.B, 0);
                        //add the pixelpoint to the ArrayList
                        originalpixels1.Add(pp);
                    }
                }
    
    
                List<pixelpoint> uniquecolors2 = new List<pixelpoint>();
    
                int matchfound = 0;
    
                foreach (pixelpoint p in originalpixels1)
                {
                    pixelpoint pixel2 = p;
                    if (uniquecolors2.Count > 0) //discardable comment--I hope we can .Count a Generic List
                    {
                        //a) we first need to check if there is a match (if match then the color is not unique and
                        //it will be assimilated
                        //
                        //b) if we don't get a match then the color is unique and it will be added as a new color
    
                        int count2 = uniquecolors2.Count;
    
                        matchfound = 0; //if no match then the color is unique
                        for (int d2 = 0; d2 < count2; d2++)
                        {
                            pixelpoint uniquecolor2 = (pixelpoint)uniquecolors2[d2];
                            if ((uniquecolor2.R == pixel2.R) && (uniquecolor2.G == pixel2.G) && (uniquecolor2.B == pixel2.B))
                            {
                                ///ALERT!!! There is something I don't like about the next line of code
                                uniquecolor2.Score += 1;
                                matchfound = 1;
                            }
                        }
                        if (matchfound == 0)
                        {
                            pixelpoint addpixel = new pixelpoint(pixel2.R, pixel2.G, pixel2.B, 1);
                            uniquecolors2.Add(addpixel);
                        }
                    }
                    else
                    {
                        //this loop is used once, at the very beginning, to add the first color
                        pixelpoint addpixel = new pixelpoint(pixel2.R, pixel2.G, pixel2.B, 1);
                        uniquecolors2.Add(addpixel);
                    }
                }
    
                int finalcount2 = uniquecolors2.Count;
                results.InnerHtml = "<br /> Number of unique colors is " + finalcount2 + "<br /><br />";
    
    
                allresults.InnerHtml = "<br /><br />All results:<br /><br />";
                //the g loop will show all
                foreach (pixelpoint p in uniquecolors2)
                {
                    pixelpoint thepixel = p;
                    allresults.InnerHtml += " R- " + thepixel.R + " G- " + thepixel.G + " B- " + thepixel.B + " score- " + thepixel.Score + "<br />";
                }
    
                topfiveresults.InnerHtml = "The top five are shown:<br /><br />";
                //the h loop will just show the top five
    
                PixelSort psort = new PixelSort();
                uniquecolors2.Sort(psort);
                uniquecolors2.Reverse();
    
                ////this loop will take colors from the Array with indices of 0,1,2,3,4
                ////something should be built in prior to deals with a scenario where a picture
                ////has less than five unique colors
    
                ArrayList topfivecolor = new ArrayList();
                // results.InnerHtml += "count for uniquecolors2 is " + uniquecolors2.Count + "<br />";
                for (int h = 0; h < 5; h++)
                {
                    //results.InnerHtml += "h is " + h + "<br />";
                    pixelpoint thepixel = (pixelpoint)uniquecolors2[h];
                    topfiveresults.InnerHtml += "h is " + h + " R- " + thepixel.R + " G- " + thepixel.G + " B- " + thepixel.B + " score- " + thepixel.Score + "<br />";
                    topfivecolor.Add(thepixel);
                }
                Session["Colors"] = topfivecolor;
    
                Bitmap newBitmap = AddWhite(new Bitmap(oldBitmap));
                string name = "whiteaddedPNG";
                newBitmap.Save(Server.MapPath("~/images/") + name + ".png", ImageFormat.Png);
                Image2.ImageUrl = "~/images/whiteaddedPNG.png";
    
                oldBitmap.Dispose();
            }
        }
    }</pixelpoint></pixelpoint></pixelpoint></pixelpoint>
     
    Wednesday, January 28, 2009 11:10 PM
  • User-1760427068 posted

     Phase II put white pixels everywhere that a color wasn't one of the top five.  Phase III puts transparency wherever a color is not one of the top five colors.

    Phase III works.  It has one line of code that I can't explain.

    It turns out we were already using argb for color (alpha red green blue) even though we were only putting in three values--apparently it assumes a=255 if you have only three values and it assumes they are RGB).

    For no transparency, alpha is 255.  If we want complete transparency, alpha is 0--we made a simple change from Phase2 to Phase3 of changing the declaration of white from

    255, 255, 255

    to being

    0, 255, 255, 255

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 11:14 PM