Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Re paste a complete code:
C#
private void Adjust(Bitmap pBitmap, params int[] pValues)
        {
Prepare(pValues);
            BitmapData pBitmapData = pBitmap.LockBits(
                new Rectangle(0, 0, pBitmap.Width, pBitmap.Height),
                ImageLockMode.ReadWrite,
                PixelFormat.Format24bppRgb);

            byte[] pData = new byte[pBitmapData.Stride * pBitmapData.Height];
            Marshal.Copy(pBitmapData.Scan0, pData, 0, pData.Length);

            int iOffset = pBitmapData.Stride - pBitmapData.Width * 3;
            int iIndex = 0;

            for (int i = 0; i < pBitmapData.Height; i++)
            {
                for (int j = 0; j < pBitmapData.Width; j++)
                {
                    for (int k = iIndex; k < iIndex + 3; k++)
                    {
                        pData[k] = Adjust(pData[k], k);
                    }
                    iIndex += 3;
                }
                iIndex += iOffset;
            }

            Marshal.Copy(pData, 0, pBitmapData.Scan0, pData.Length);
}

protected void Prepare(int[] pValues)
        {
            m_iRed = pValues[0];
            m_iGreen = pValues[1];
            m_iBlue = pValues[2];
        }

 protected byte Adjust(byte iValue, int iIndex)
        {
            int nColour = 0;

            switch (iIndex % 3)
            {
                case 0:
                    nColour = (int)iValue + m_iBlue;
                    break;
                case 1:
                    nColour = (int)iValue + m_iGreen;
                    break;
                case 2:
                    nColour = (int)iValue + m_iRed;
                    break;
            }

          
            return Fix(nColour);
        }
  protected byte Fix(int iValue)
        {
            if (iValue < 0) iValue = 0;
            if (iValue > 255) iValue = 255;
            return (byte)iValue;
        }


What I have tried:

i have a picture is divided into four parts.i want change color by RGB.but only changed half.
1.change failed when width of the picture less than 256.
2.my small picture maximum not more than 256.
3.Every picture of 256*256 is normal.
4.Problem parameter:r=0,g=0,b=52.


Sorry, I don't know how to reply downstairs.

about Ralf Meier's solution
On change pixel loop, I debug, the color of the picture and did not change the picture, they are the same value.

about Jochen Arndt's solution
At that time, the code did not paste completely, for the calculation of the value I have been repaired by Fix.
Changed to absolute value, still no effect.
At the moment, I still haven't found the problem.

the image does not change color when set r=0,g=0,b=52:https://i.stack.imgur.com/IIrgd.jpg
Posted
Updated 15-Feb-17 16:39pm
v11
Comments
Ralf Meier 15-Feb-17 1:08am    
You should check your k-Loop.
I would say that you try to Access with k+1, k+2 and k+3 an index outside your Image if i or j comes to their end ...
Jochen Arndt 16-Feb-17 3:26am    
By editing your questions multiple times answers might get out of context. When editing you should indicate the new section and explain what has been changed.

When you want to answer to solutions and comments use the corresponding options ('Have a question or comment'). Doing so sends notification mails. This time I saw that you edited your question adressing me. But you can't rely on that.

"Changed to absolute value, still no effect."
What is with the wrong index passed to the Adjust() function?
While that is not fixed the result is always wrong and makes it more difficult to find other errors.

1 solution

Note that the BitmapData.Stride Property (System.Drawing.Imaging)[^] will be negative with bottom-up bitmaps. So you have to use the absolute value:
C#
int iStride = Math.Abs(pBitmapData.Stride);
byte[] pData = new byte[iStride * pBitmapData.Height];
int iOffset = iStride - pBitmapData.Width * 3;


Another problem is using the byte index in your Adjust function to determine if the byte is a R, G, or B value. While this will work for the first line this does not apply to others because of the stride (the start index of a line is always a multiple of four which may not give a zero modulo). So you should not pass the index but k - iIndex (or rewrite your code).

I don't know if the above are the source for the seen problem (changed only half of the colours) but they will result in wrong conversions.

[EDIT]
There might be also a third problem:
You are adjusting colours by adding a value without checking for overflow. With your m_iBlue value of 52, each blue value greater than 203 will become smaller. If this is not intended you should check for overflows and set the result to 255:
C#
protected byte Adjust(byte iValue, int iIndex)
{
    // ...
    return nColour > 255 ? 255 : nColour;
}

[/EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900