本文介绍了LockBits()步幅不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最重要的是,我在使用LockBits gdi +函数时遇到了麻烦,似乎无法正常工作.

这是我非常简单的测试代码:

Hi to all, i''m in trouble with LockBits gdi+ function, it dosen''t seems work correctly.

This is my very simple test code:

BitmapData dataHigh;
BitmapData dataImg;
Bitmap *image = new Bitmap(480,520,PixelFormat24bppRGB);
Bitmap *imgSx = new Bitmap(480,480,PixelFormat24bppRGB);

imgSx->LockBits(Rect(10,10,10,10),ImageLockModeRead | ImageLockModeWrite,PixelFormat24bppRGB,&dataHigh);

image->LockBits(Rect(10,10,10,10),ImageLockModeRead | ImageLockModeWrite,PixelFormat24bppRGB,&dataImg);

memset(dataHigh.Scan0,0xff,320);
memcpy(dataImg.Scan0,dataHigh.Scan0,320);

imgSx->UnlockBits(&dataHigh);
image->UnlockBits(&dataImg);



我只将图像的选择部分填充为空白,如果一行不是正方形,则填充结果,似乎从Scan0指向的缓冲区包含alla图像数据,而不仅仅是用Rect选择的部分.

我强制使用了memset和memcpy中的字节数,因为两个BitmapData的Stride值始终像整个图像一样都是1440.

可以是LockBits函数的错误吗?

提前谢谢!

Marco



I would fill with blank only the selection portion of image, but the result if a line not a square, Seem that the buffer pointed from Scan0 contains alla image data and not only portion selected with Rect.

I forced the number of byte in memset and memcpy because the Stride value of both BitmapData are always 1440 like entire image.

Can be a bug of LockBits function?

Thank you in advance!

Marco

推荐答案

BYTE* pbyFirstLine = (BYTE*) dataHigh.Scan0;

    int nX_i = 10;
    int nY_i = 10;
    int nW_i = 10; // Width of Bitmap
    int nH_i = 10; // Height of bitmap

    for(int nY = nY_i; nY < nY_i + nH_i; nY++)
    {
        // Seek in X and Y direction.
        BYTE* pbySeek = pbyFirstLine + ( nX_i * 3 ) + nY * dataHigh.Stride;
        memset(pbySeek, 0xFF, 3 * nW_i );
    }



Scan0始终返回位图的第一个像素,这可能是Gdi的错误:)
根据msdn文档,



Scan0 always return first pixel of Bitmap, This may be a bug of Gdi :)
According to msdn documentation,

Scan0: Gets or sets the address of the first pixel data in the bitmap. This can also be thought of as the first scan line in the bitmap.


这篇关于LockBits()步幅不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:40