Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have 3 code!!they have no error but i dont know a problem!!time to run code1 and code2 is slower than code3 and the result image is not true but i think code1 and code2 are better than code 3!!Can you explain me for this problem??

Code 1:
public static void chinhdosang(Bitmap bm, int dosang)
        {
            Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
            BitmapData bmdata = bm.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmdata.Scan0;
            int stride = bmdata.Stride;
            int offset = stride - bm.Width * 3;
            int n = stride * bm.Height;
            byte[] rgbvalue = new byte[n];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalue, 0, n);
            int temp,x,y;
            for (y = 0; y < bm.Height; y++)
            {
                for (x = 0; x < bm.Width; x++)
                {
                    int i;
                    i = y * stride + x;
                    temp = (int)rgbvalue[i] + dosang;
                    if (temp < 0) temp = 0;
                    if (temp > 255) temp = 255;
                    rgbvalue[i] = (byte)temp;

                }
            }
            System.Runtime.InteropServices.Marshal.Copy(rgbvalue, 0, ptr, n);
            bm.UnlockBits(bmdata);
            

 
        }

Code 2:
public static void chinhdosang(Bitmap bm, double dosang)
        {
            Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
            BitmapData bmdata = bm.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int stride = bmdata.Stride;
            IntPtr ptr = bmdata.Scan0;
            int offset = stride - bm.Width * 3;
            unsafe
            {
                byte* p = (byte*)ptr;
                int x, y;
                double value;
                for (y =0;y<bm.Height;y++)
                {
                    for (x = 0; x < bm.Width; x++)
                    {
                        value = (double)(p[0]) + dosang;
                        if (value < 0) value = 0;
                        if (value > 255) value = 255;
                        p[0] = (byte) value;
                        ++p;


                    }
                    p += offset;
                }

            }
            bm.UnlockBits(bmdata);
}


Code 3:
public static void chinhdosang(Bitmap bm, int dosang)
        {
            Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
            BitmapData bmdata = bm.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmdata.Scan0;
            int stride = bmdata.Stride;
            int offset = stride - bm.Width * 3;
            int n = bm.Width * bm.Height * 3;
            byte[] rgbvalue = new byte[n];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalue, 0, n);
            int temp,x,y;
            
                for ( int i = 0; i < n; i++)
                {
                    
                    //i = y * stride + x;
                    temp = (int)rgbvalue[i] + dosang;
                    if (temp < 0) temp = 0;
                    if (temp > 255) temp = 255;
                    rgbvalue[i] = (byte)temp;

                }
            
            System.Runtime.InteropServices.Marshal.Copy(rgbvalue, 0, ptr, n);
            bm.UnlockBits(bmdata);
            

 
        }
Posted

You need to keep in mind that you have an image with a pixel format of 24bit. This means that each pixel is using 3 bytes of color information horizontally. This is not the case for vertically,

First of all, your calculation is not correct:
int n = bm.Width * bm.Height * 3;

When for example take a width of 10 and height of 100 the calculation would be 10*100*3 and is 3000. This isn't correct and should be height * (width * 30) which would be 100 * (10 * 3) and is 300.

Second, the loop needs to process each color information byte of each pixel, so horizontally you need to multiply by 3 (not vertically).

C#
for (y =0;y<bm.Height;y++)
{
   for (x = 0; x < bm.Width * 3; x++)
...


Good luck!
 
Share this answer
 
Comments
E.F. Nijboer 29-Sep-10 18:13pm    
Very nice my answer could help you out :-) Could you also mark it as solved?
thanks!i knew what mistake is!haha!thanks again!
 
Share this answer
 

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