我如何得到比位图宽度小的Stride?

时间:2012-09-26 14:22:13

标签: c# image-processing bitmapdata

我从一个1bpp位图复制到一个较小的1bpp位图。我只想剪掉一个区域,这样我就可以算出黑色像素的数量了。

我使用以下内容制作副本:

    private Bitmap Copy(Bitmap srcBitmap, Rectangle section)
    {
        BitmapData SourceLockedData;
        BitmapData DestLockedData;
        Rectangle DestRect;
        byte[] SrcImageData;
        byte[] DestImageData;
        int ByteCount;
        int WidthCount = 0;
        int CurrentLine = 0;
        int DestStride;
        int SrcStride = 0;

        // Create the new bitmap and associated graphics object
        Bitmap Newbmp = new Bitmap(section.Width, section.Height, PixelFormat.Format1bppIndexed);
        Newbmp.SetResolution(srcBitmap.HorizontalResolution, srcBitmap.VerticalResolution);

        //Lock the bits
        SourceLockedData = srcBitmap.LockBits(section, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);

        SrcStride = SourceLockedData.Stride;

        //Get a count of the number of bytes to copy. Remember, bytes are not pixels.
        ByteCount = SourceLockedData.Stride * SourceLockedData.Height;

        //Initialize the source byte array
        SrcImageData = new byte[ByteCount];

        //Copy the data to the source byte array
        Marshal.Copy(SourceLockedData.Scan0, SrcImageData, 0, ByteCount);

        //Unlock the bits
        srcBitmap.UnlockBits(SourceLockedData);

        //Set a rectangle to the size of the New bitmap
        DestRect = new Rectangle(new Point(0, 0), Newbmp.Size);

        //Lock the bits
        DestLockedData = Newbmp.LockBits(DestRect, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);

        DestStride = DestLockedData.Stride;

        //Get a count of the number of bytes to copy. Remember, bytes are not pixels.
        ByteCount = DestLockedData.Stride * DestLockedData.Height;

        //Initialize the source byte array
        DestImageData = new byte[ByteCount];

        //Copy the data to the destination byte array
        Marshal.Copy(DestLockedData.Scan0, DestImageData, 0, ByteCount);

        //Unlock for now
        Newbmp.UnlockBits(DestLockedData);

        for (int ArrayIndex = 0; ArrayIndex < ByteCount; ArrayIndex++)
        {
            if (WidthCount == Newbmp.Width)
            {
                //increment the line and push the index by the stride
                ArrayIndex = (++CurrentLine) * DestStride;
                continue;
            }

            DestImageData[ArrayIndex] = SrcImageData[ArrayIndex];
            WidthCount++;
        }

        //Lock the bits again
        DestLockedData = Newbmp.LockBits(DestRect, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);

        //Copy data from byte array to IntPtr
        Marshal.Copy(DestImageData, 0, DestLockedData.Scan0, ByteCount);

        //Unlock bits
        Newbmp.UnlockBits(DestLockedData);

        // Return the bitmap
        return Newbmp;
    }

我遇到的最大问题是SourceLockedData.Stride和DestLockedData.Stride都小于相应图像的宽度。怎么可能?从我所知道的所有步骤来看,它是从一条数据扫描线到下一条扫描数据线的位数。这在数学上如何可能小于宽度?

我使用LockBits或BitmapData是错误的吗? BitmapData不可信吗?我应该用手计算步幅吗?

Tom P。

1 个答案:

答案 0 :(得分:0)

我发现如果处理RLE位图,步幅可能会小于宽度。由于我加载的位图是TIFF,因此它们是RLE8编码的。