C
C#7mo ago
smarts1l

Error with getting pixels

I have tried to create some code to get the pixels of an uploaded image but the function doesn't seem to work correctly. It gives extremely strange outputs so it isn't that the r,g,b values are looking at the wrong index in the array. It does work for black and white but no colours. Does anyone know why? This is the part of my code where the error occurs var pixelData = createBytes(width, height); string oldr = "0", oldb = "0", oldg = "0"; string[] compressed = new string[height * width]; int godown = 0; var path = op.FileName; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) {
int index = y * (width*4) + 4 * x; byte blue = pixelData[index]; byte green = pixelData[index + 1]; byte red = pixelData[index + 2]; int arraySlot = x +(y*width);int StandardArraySlot = arraySlot - godown; string r = red.ToString("D3"); string g = green.ToString("D3"); string b = blue.ToString("D3"); MessageBox.Show(r+" "+g+" "+b); public byte[] createBytes(int width,int height) { int stride = width * 4; byte[] pixelData = new byte[height * stride]; image.CopyPixels(new Int32Rect(0, 0, width, height), pixelData, stride, 0); return pixelData;

}
8 Replies
life grinder
life grinder7mo ago
format the code with $code
MODiX
MODiX7mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
life grinder
life grinder7mo ago
also stuff like this int index = y * (width4) + 4 x; is y * (width * 4) + 4 * x)? `
smarts1l
smarts1l7mo ago
yes it is that in my code but I think that just pasting it into dicord has deleted it
int width = image.PixelWidth;
int height = image.PixelHeight;
string[] compressed = new string[height * width];
string oldr = "0"; string oldg = "0"; string oldb = "0";
int godown = 0;
var path = op.FileName;
int stride = width * 4;
byte[] pixelData = new byte[height * stride];
image.CopyPixels( pixelData, stride, 0);

for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int index = y * stride + 4 * x;
byte blue = pixelData[index];
byte green = pixelData[index + 1];
byte red = pixelData[index + 2];

int arraySlot = x + (y * width); int StandardArraySlot = arraySlot - godown;
string r = red.ToString(); string g = green.ToString(); string b = blue.ToString();
MessageBox.Show(r + " " + g + " " + b);
int width = image.PixelWidth;
int height = image.PixelHeight;
string[] compressed = new string[height * width];
string oldr = "0"; string oldg = "0"; string oldb = "0";
int godown = 0;
var path = op.FileName;
int stride = width * 4;
byte[] pixelData = new byte[height * stride];
image.CopyPixels( pixelData, stride, 0);

for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int index = y * stride + 4 * x;
byte blue = pixelData[index];
byte green = pixelData[index + 1];
byte red = pixelData[index + 2];

int arraySlot = x + (y * width); int StandardArraySlot = arraySlot - godown;
string r = red.ToString(); string g = green.ToString(); string b = blue.ToString();
MessageBox.Show(r + " " + g + " " + b);
the method gets the wrong pixels but i cant see why. like for a pixel of 85,160,34 it gave a value of 0,18,0 and for some pixels it says that they are 0,0,0 (black when they clearly arent)
life grinder
life grinder7mo ago
symbols like ~ * ` _ are used for formatting in discord and many other services have you tried to check pixel by pixel where's the first difference? it could give you a hint also, making sure the format is rgba or argb or what else
smarts1l
smarts1l7mo ago
all pixels are wrong, none of them share any value woith the supposed result if it was argb or rgba I would at least see a value in common so it cant be that exactly a pixel of 53,255,107 gives a value of 0,18,0 and afterwards the same pixel of 53,255,107 givesd a value of 0,0,0
life grinder
life grinder7mo ago
is there a conversion happening, maybe? how did you create image? is Format what you expect it to be? you could also try drawing it to look if you can understand some common artifacts/issues
smarts1l
smarts1l7mo ago
thank you so much for your help but I have fixed the problem by changing the function to another one and this works fine. I don't know why the other function didn't work though