I need to extract R,G,B values from the image and store in 3 different arrays with it's equivalent co-ordinates values.
For example: In 500*500 Image, R must store only red color information of each pixel and it's equivalent co-ordinate values i.e., [0,0] to its maximum co-ordinate [499,499]. After extracting all the R values into array, I need to retrieve the R value of particular pixel using the co-ordinate as a reference by mouse click in the Image, And need to follow same for G,B values.
Can anyone help me doing this using C#? In WPF application.
Asked
Active
Viewed 957 times
-1

Clemens
- 123,504
- 12
- 155
- 268

Vinay Srinivas
- 7
- 1
- 3
-
@orbit Thanks for suggestion, but i need to achieve this using C#. – Vinay Srinivas Dec 02 '17 at 05:16
-
1Possible duplicate of [Getting RGB array from image in C#](https://stackoverflow.com/questions/4747428/getting-rgb-array-from-image-in-c-sharp) – Rufus L Dec 02 '17 at 05:21
-
@RufusL My question is not duplicate. That answer is not storing R,G,B values in separate array. And I need to get the pixel values when i click in the image. – Vinay Srinivas Dec 02 '17 at 05:46
-
It would be awesome if you had a [mcve] – mjwills Dec 02 '17 at 06:12
-
Call [`BitmapSource.CopyPixels`](https://msdn.microsoft.com/en-us/library/ms616043(v=vs.110).aspx), then extract the color channels from the pixel array. The exact way how to do that depends on the [`Format`](https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.format(v=vs.110).aspx) of the BitmapSource. See e.g. here: https://stackoverflow.com/a/14878596 – Clemens Dec 02 '17 at 08:10
-
That said, ignore any possible solution that involves the WinForms Image and Bitmap classes (like the answer here and in the suggested duplicate). You don't need anything else than the WPF BitmapSource class. – Clemens Dec 02 '17 at 08:17
-
After reading the question again, it's also unclear to me what exactly you are trying to do. When you say "I need to retrieve the R value of particular pixel using the co-ordinate as a reference by mouse click in the Image" it seems you just want to get the RGB value of a specific pixel. Why store these values in separate arrays at all? It would be much simpler to just get an RGB value when you click the image. – Clemens Dec 02 '17 at 08:24
-
@Clemens What I'm trying to do is, I want to store RGB info of a image into an array or external database with its co-ordinate values before loading into the WPF application. – Vinay Srinivas Dec 02 '17 at 11:30
-
For loading the image, Image.Source should be the array or database file. After the image is loaded, when I click on edit button I should get a separate window (Something like [this](https://in.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/14142/versions/3/screenshot.jpg)). Here I should be able to edit the values for RGB and it must update the current image as well as the array or external database. – Vinay Srinivas Dec 02 '17 at 11:33
-
That does not require to store the color channels separately. Just load a WriteableBitmap (i.e. another BitmapSource subclass) from your pixel buffer and call its WritePixels method as necessary. – Clemens Dec 02 '17 at 11:35
1 Answers
0
if your image is a System.Drawing.Bitmap:
BitmapData data = image.LockBits(new Rectangle(startX, startY, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat);
try
{
byte[] pixelData = new Byte[data.Stride];
for (int scanline = 0; scanline < data.Height; scanline++)
{
Marshal.Copy(data.Scan0 + (scanline * data.Stride), pixelData, 0, data.Stride);
for (int pixeloffset = 0; pixeloffset < data.Width; pixeloffset++)
{
// PixelFormat.Format32bppRgb means the data is stored
// in memory as BGR. We want RGB, so we must do some
// bit-shuffling.
rgbArray[offset + (scanline * scansize) + pixeloffset] =
(pixelData[pixeloffset * PixelWidth + 2] << 16) + // R
(pixelData[pixeloffset * PixelWidth + 1] << 8) + // G
pixelData[pixeloffset * PixelWidth]; // B
}
}
}
finally
{
image.UnlockBits(data);
}

Hadi Mirzaei
- 222
- 2
- 16