Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...