0

我正在从一个 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 不能被信任吗?我应该手动计算步幅吗?

汤姆·P。

4

1 回答 1

0

我发现如果您正在处理 RLE 位图,步幅可以小于宽度。因为我加载的位图是 TIFF,所以它们是 RLE8 编码的。

于 2012-09-26T15:07:32.967 回答