none
convert image to color map using color matrix? RRS feed

  • Question

  • does anybody know how to convert a image to a color map in which its rgb components are indexes for a palette?

    I would like to use a colormatrix.

    for example color 0 would be `{255,0,0,0}` and color 1 would be `{255,1,1,1}` in argb format

            public void Load(string filename, int x, int y)
            {
                if (!File.Exists(filename)) return;
    
                Color c;
                //ColorMap[]      colorMap    = new ColorMap[Palette.Length];
                ImageAttributes attributes = new ImageAttributes();
    
                //create the grayscale ColorMatrix
                ColorMatrix colorMatrix = new ColorMatrix
                (
                      new float[][]
                      {
                         new float[] {.03f, .03f, .03f, 0, 0},
                         new float[] {.59f, .59f, .59f, 0, 0},
                         new float[] {.11f, .11f, .11f, 0, 0},
                         new float[] {   0,    0,    0, 1, 0},
                         new float[] {   0,    0,    0, 0, 1}
                      }
                );
    
                /*
                for (var i = 0; i < colorMap.Length; i++)
                {
                    c = Palette[i];
                    colorMap[i].OldColor = new Color(i, i, i, 255);
                    colorMap[i].NewColor = new Color(c.R, c.G, c.B, c.A);
                };
                */
    
                attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                //attributes.SetRemapTable(colorMap);
    
                using (Image i = new Bitmap(filename))
                {                
                    using (Graphics g = Graphics.FromImage(Data))
                    {
    
                        g.DrawImage(i, new Rectangle(x, y, i.Width, i.Height), 0, 0, i.Width, i.Height, GraphicsUnit.Pixel, attributes);
                    }
                }
            }



    Wednesday, July 17, 2019 2:26 AM

All replies

  • Hi Shadowblitz16,

    Do you mean you want to get all pixels in the image? If so, you can define a "Color" List to store them.

    Here is the simple demo you can refer to.

        List<Color> colors = new List<Color>();
        Bitmap bitmap = new Bitmap(@"D:\test.png");
    
        private void btGet_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color color = bitmap.GetPixel(i, j);
                    colors.Add(color);
                }
            }
            foreach (var i in colors)
            {
                Console.WriteLine(i.A.ToString() + ", " + i.R.ToString() + ", " + i.G.ToString() + ", " + i.B.ToString());
            }
        }

    Besides, could you explain the following sentence in detail?

    >> convert a image to a color map

    If there is any misunderstanding, please feel free to contact me.

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 5:47 AM
    Moderator
  • Hi Kyle

    what I mean is take a image and prepare it for palette swapping using a colormap class

    for example I first need to convert the image to a grayscale value between ARGB(255,0,0,0) to ARGB(255,maxColors,maxColors,maxColors) with maxColors being a value ranging from 0 to 255

    then I need to switch the grayscale colors around in the image by swapping the colors in the colormap using the index of the lookup color array.

    lastly I can either return the bitmap as a remapped grayscale image or colored bitmap using the color array

    the problem I am having is step 1 where I convert the image to a grayscale image with rgb values ranging from 0 to maxcolors which in this case would be 16.

    Thursday, July 18, 2019 1:06 AM
  • Hi Shadowblitz16,

    To convert a image to grayscale image? Here I found an article maybe you can refer to.

    C# Tutorial - Convert a Color Image to Grayscale.

    The test result,

    Regards,

    Kyle

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 18, 2019 9:55 AM
    Moderator
  • Hi Kyle

    please reread what I posted

    I want to have greyscale but I also want to have the rgb components to be the same with r being used to access the color in the colormap with the rgb components ranging from 0-paletteLength 

    here is a example of what I mean..

    the right image is not just black but just looks like it because it contains the following colors..

    ARGB(255,0,0,0),
    ARGB(255,1,1,1),
    ARGB(255,2,2,2),
    ARGB(255,3,3,3),
    ARGB(255,4,4,4),
    ARGB(255,5,5,5),
    ARGB(255,6,6,6),
    ARGB(255,7,7,7),
    ARGB(255,8,8,8),
    ARGB(255,9,9,9),
    ARGB(255,A,A,A),
    ARGB(255,B,B,B),
    ARGB(255,C,C,C),
    ARGB(255,D,D,D),
    ARGB(255,E,E,E),
    ARGB(255,F,F,F),

    is there a way to do this and if so can you provide some code?

    Thursday, July 18, 2019 10:43 PM
  • Hi Shadowblitz16,

    Sorry for my misunderstanding. Does it mean set the the "ARGB" like "ARGB(255, Rvalue,Rvalue,Rvalue)"? First, "ARGB" always uses the decimal int type as a parameter, instead of hexadecimal. If you want to reset the pixel color, you can use the method "Bitmap.SetPixel(Int32, Int32, Color)".

    Here is a simple demo. And hope this can help you.

        Bitmap bitmap = new Bitmap(@"D:\pic\test.png");
    
        private void btGet_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color color = bitmap.GetPixel(i, j);
                    Color rcolor = Color.FromArgb(255, color.R, color.R, color.R);
                    bitmap.SetPixel(i, j, rcolor);
                }
            }
            bitmap.Save(@"D:\pic\newpic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 19, 2019 6:12 AM
    Moderator
  • Hi Shadowblitz16,

    Sorry for my misunderstanding. Does it mean set the the "ARGB" like "ARGB(255, Rvalue,Rvalue,Rvalue)"? First, "ARGB" always uses the decimal int type as a parameter, instead of hexadecimal. If you want to reset the pixel color, you can use the method "Bitmap.SetPixel(Int32, Int32, Color)".

    Here is a simple demo. And hope this can help you.

        Bitmap bitmap = new Bitmap(@"D:\pic\test.png");
    
        private void btGet_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color color = bitmap.GetPixel(i, j);
                    Color rcolor = Color.FromArgb(255, color.R, color.R, color.R);
                    bitmap.SetPixel(i, j, rcolor);
                }
            }
            bitmap.Save(@"D:\pic\newpic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    this just converts it to grayscale again.

    I don't know how to explain it any better, I even posted a picture of what I wanted to do.

    Friday, August 9, 2019 4:30 AM