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
489 views
in Technique[技术] by (71.8m points)

c# - any can explain the function of stride in bitmapdata?

        Bitmap bit1 = new Bitmap( bmpimg , width , height );
        Bitmap bit2 = new Bitmap( bmp , width , height );

        Bitmap bmpresult = new Bitmap( width , height );

        BitmapData data1 = bit1.LockBits( new Rectangle( 0 , 0 , bit1.Width , bit1.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
        BitmapData data2 = bit2.LockBits( new Rectangle( 0 , 0 , bit2.Width , bit2.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
        BitmapData data3 = bmpresult.LockBits( new Rectangle( 0 , 0 , bmpresult.Width , bmpresult.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );

unsafe
        {
            int remain1 = data1.Stride - data1.Width * 3;
            int remain2 = data2.Stride - data2.Width * 3;
            int remain3 = data3.Stride - data3.Width * 3;


            byte* ptr1 = ( byte* )data1.Scan0;
            byte* ptr2 = ( byte* )data2.Scan0;
            byte* ptr3 = ( byte* )data3.Scan0;

            for( int i = 0 ; i < height ; i ++ )
            {
                for( int j = 0 ; j < width * 3 ; j ++ )
                {
                    ptr3[ 0 ] = ( byte ) ( XOR_Operator( ptr1[ 0 ] , ptr2[ 0 ] ) );
                    ptr1 ++;
                    ptr2 ++;
                    ptr3 ++;
                }

                ptr1 += remain1;
                ptr2 += remain2;
                ptr3 += remain3;
            }


        }

        bit1.UnlockBits( data1 );
        bit2.UnlockBits( data2 );
        bmpresult.UnlockBits( data3 );

        return bmpresult;
    }

is it necessary to get remain for data objects

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Stride is there due to hardware requirements that have unfortunately leaked through to the API layer.

It is important because Windows drivers sometimes require that scanlines (rows in the image) be memory aligned. That is why they are sometimes larger than they would strictly need to be.

See for example this MSDN article DIBs and Their Use.

Every scanline is DWORD-aligned. The scanline is buffered to alignment; the buffering is not necessarily 0.

Your handling seems to be sufficient.


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

...