Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

c# - Trying to Understand this Image function

private void ReadImage()
    {
        int i, j;
        GreyImage = new int[Width, Height];  //[Row,Column]
        Bitmap image = Obj;
        BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                                 ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
        unsafe
        {
            byte* imagePointer1 = (byte*)bitmapData1.Scan0;

            for (i = 0; i < bitmapData1.Height; i++)
            {
                for (j = 0; j < bitmapData1.Width; j++)
                {
                    GreyImage[j, i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0);
                    //4 bytes per pixel
                    imagePointer1 += 4;
                }//end for j
                //4 bytes per pixel
                imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
            }//end for i
        }//end unsafe
        image.UnlockBits(bitmapData1);
        return;
    }

the line GreyImage[j,i] = (int)((imagePointer1[0] ..... seems to be reading into the byte* like an array, obviously I can't assign an unsafe bit of code to an array for later processing, so i thought maybe just assign those 4 bytes to the array.

How do you assign those 4 bytes to the array?

i thought by doing:

var imageData = new byte[Width, Height][];
imageData[x,y] = pixelSet //basically byte[];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you are trying to do something like this. Obviously, I haven't tested this code but it will get you in the direction you want.

byte[] save = new byte[4];
Array.Copy(*imagePointer1, save, 4);

OR

byte[] save = new byte[4];
save[0] = bitmapData1.Scan0[0];
save[1] = *(imagePointer1 + 1);
save[2] = *(imagePointer1 + 2);
save[3] = *(imagePointer1 + 3);

A pointer to an array always points to element zero. You can access other elements by adding to the pointer or incrementing the pointer.

 (imagePointer1 + 5)  // pointer to 5th element
*(imagePointer1 + 5)  // value of 5th element
  imagePointer1 += 5; // imagePointer1 now starts at element 5

Plus and minus move the pointer reference by the number of bytes that make up the sizeof the array's data type. If it was an int[], + and - would move the pointer in increments of 4 bytes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...