 We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

## 4 replies

Last post Feb 03, 2009 08:27 AM by TATWORTH

• ### byte array and average value

i'm working on something to get the average color from an image and the code i have puts all the rgb values into a byte array, so the values are being stored as (r,g,b,r,g,b,r,g,b,r,g,b,r,g,b,etc). how would i loop through each r value to add them to another array to then be able to calculate the average (and so forth for g and b as well). Can anyone provide an example?

• ### Re: byte array and average value

for(int i=0, int iNewArr=0;i<arr.Length;)

{

RedArr[iNewArr] = arr[i++];

GreenArr[iNewArr] = arr[i++];

BlueArr[iNewArr] = arr[i++];

iNewArr++;

}

Is that what u wanted?

Mark as answer if you are agree with the solution. This helps the community.

Sandeep Bhutani
• ### Re: byte array and average value

Make a for loop on the array. Next you need to sum things up with a if condition which checks the value for counter
int j = 0;

for(int i=0;i<arr.length;i++)
{
if(j==0)
{
//Do work for red
}
if(j==1)
{
//Do work for red
}
if(j==2)
{
//Do work for red
}
j++;
if(j==3)
{
j=0;
}

}

Vikram
www.vikramlakhotia.com

• ### Re: byte array and average value

Can anyone provide me with a vb example?

• ### Re: byte array and average value

Try

<div class="code-view"> <div class="container"> Dim j As Integer = 0
For i As Integer = 0 To arr.length - 1
If j = 0 Then
'Do work for red
End If
If j = 1 Then
'Do work for red
End If
If j = 2 Then
'Do work for red
End If
j += 1
If j = 3 Then
j = 0
End If
Next </div> </div>

Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239