hi i have created a code that allows images from 5 picboxes to merge together.
but i have a problem distinguishing them.
after merging, i would want to have different greyscale values for the images merged into one picbox.
my image is made up of black and white only. thus, the image of the black image(which are actually dots) to have different pixel value. BUT, it must still be a shade of black.

this is the code that i am working on:

public Bitmap mergeImages(Bitmap[] images)
        {
            Bitmap mergedImg = new Bitmap(images[0].Width, images[0].Height);

            for (int h = 0; h < mergedImg.Height; h++)
            {

                for (int w = 0; w < mergedImg.Width; w++)
                {

                    Color c = images[0].GetPixel(w, h);

                    if (c.R == 255 && c.G == 255 && c.B == 255)
                    {
                        int index = 1;
                        for (; index < images.Length; index++)
                        {
                            if (images[index].GetPixel(w, h) == c)
                                continue;
                            else
                                break;

                        }
                        if (index == images.Length)
                        {
                            //all the pixel in the same location in all images are all white

                            mergedImg.SetPixel(w, h, c);
                        }
                        else
                        {
                            //if not, set the color of the pixel to be black;
                            Color c1 = Color.FromArgb(0, 0, 0);
                            mergedImg.SetPixel(w, h, c1);
                        }
                    }
                    else
                    {
                        Color c1 = Color.FromArgb(0, 0, 0);
                        mergedImg.SetPixel(w, h, c1);
                    }
                }
            }
            return mergedImg;
        }

pls help.

Dani AI

Generated

A couple of practical fixes and a simple, robust approach that follow from 's description. The current routine collapses every non-white contribution to pure black and also uses nested GetPixel/SetPixel calls, which becomes very slow for anything larger than tiny images. For per-pixel work prefer bulk access (LockBits / BitmapData) rather than calling GetPixel inside nested loops; LockBits gives a direct memory buffer and is far faster. (stackoverflow.com)

A compact, deterministic method: treat the N input bitmaps as binary masks (black = present). For each pixel compute a bitmask where bit i = 1 if image i has a black pixel at that location (use a small threshold instead of exact equality to handle anti-alias/JPEG). Convert that mask to a grayscale value so every different combination maps to a distinguishable shade:

int maxMask = (1<<N) - 1;
int step = 255 / maxMask;
for each pixel (x,y) {
  int mask = 0;
  for (int i=0;i<N;i++) if (isBlack(images[i].GetPixel(x,y), thresh)) mask |= 1<<i;
  int intensity = 255 - mask * step; // darker when more bits set; clamp 0..255
  merged.SetPixel(x,y, Color.FromArgb(intensity, intensity, intensity));
}

The snippet above shows the mapping logic; replace GetPixel/SetPixel with LockBits + Marshal.Copy or unsafe pointers for production. Convert all bitmaps to a known pixel format (e.g. Format32bppArgb) before locking to simplify indexing. If a simpler recolor is acceptable (same shade per whole source image, not per-combination), ImageAttributes/ColorMatrix can tint whole images faster. (learn.microsoft.com)

Notes: pick a sensible threshold (eg. average RGB < 128) rather than exact equality; clamp values; choose step so combinations remain visible. 's MATLAB idea is valid for heavy image analysis but overkill for this mapping; 's code is useful when you want tiled/quadrant layouts rather than per-pixel encoding.

Recommended Answers

All 2 Replies

for those tasks, I recommend to use MATLAB it helps a lot and has a lot of built in algorithms and toolbox you can use, use its funcationality then make your work as dll and call it from C# application

I was just trying to make the same thing. It took me a while but I got it.

Hope this helps

/// <summary>
        /// Merges 4 Images into 1 Image.
        /// </summary>
        /// <param name="image1">The Image you want in the Top-Left Corner.</param>
        /// <param name="image2">The Image you want in the Top-Right Corner.</param>
        /// <param name="image3">The Image you want in the Bottom-Left Corner.</param>
        /// <param name="image4">The Image you want in the Bottom-Right Corner.</param>
        /// <returns>An Image of 4</returns>
        public Image MergeImages(Image image1, Image image2, Image image3, Image image4)
        {
            //Get the Width of All the Images
            Int32 width = image1.Width + image2.Width + image3.Width + image4.Width;
            //Get the Height of All the Images
            Int32 height = image1.Height + image2.Height + image3.Height + image4.Height;
            //Create a new Bitmap with the Width and Height
            Bitmap bitmap = new Bitmap(width, height);

            //Get All of the x Pixels
            for (int w = 0; w < image1.Width; w++)
            {
                //Get All of the Y Pixels
                for (int h = 0; h < image1.Height; h++)
                {
                    //Create a new Bitmap from image1
                    Bitmap image = new Bitmap(image1);
                    //Set the Cooresponding Pixel
                    bitmap.SetPixel(w, h, image.GetPixel(w, h));
                }
            }
            //Get All of the x Pixels
            for (int w = 0; w < image2.Width; w++)
            {
                //Get All of the Y Pixels
                for (int h = 0; h < image2.Height; h++)
                {
                    //Create a new Bitmap from image2
                    Bitmap image = new Bitmap(image2);
                    //Set the Cooresponding Pixel
                    bitmap.SetPixel(image.Width + w, h, image.GetPixel(w, h));
                }
            }
            //Get All of the x Pixels
            for (int w = 0; w < image3.Width; w++)
            {
                //Get All of the Y Pixels
                for (int h = 0; h < image3.Height; h++)
                {
                    //Create a new Bitmap from image3
                    Bitmap image = new Bitmap(image3);
                    //Set the Cooresponding Pixel
                    bitmap.SetPixel(w, image.Height + h, image.GetPixel(w, h));
                }
            }
            //Get All of the x Pixels
            for (int w = 0; w < image4.Width; w++)
            {
                //Get All of the Y Pixels
                for (int h = 0; h < image4.Height; h++)
                {
                    //Create a new Bitmap from image4
                    Bitmap image = new Bitmap(image4);
                    //Set the Cooresponding Pixel
                    bitmap.SetPixel(image.Width + w, image.Height + h, image.GetPixel(w, h));
                }
            }
            
            //Return the new Bitmap
            return bitmap;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.