Cximage 库使用,直接读取图像数据到内存。

编译Cximage生成对应的Debug与Release版本,网上教程很多,不明白请留言。

这里需要GetBits()函数获得每一行图像数据的首像素的指针,不得不再次提醒的是,图像数据是从下到上,从左到右的方式扫描的。然后用GetEffWidth()函数获得图像的每行的stride长度,用来申请内存空间,注意:对于32位RGBA图像的储存方式,这个函数只是获得的RGB内存块的宽度,下面会详细介绍。同样的除了GetBits()这个函数可以获得图像数据指针之外,还有GetDIB()函数来直接获得图像数据指针。注意:图像数据指针指向左下角像素位置,所以图像数据是上下颠倒的。

首先

首先创建CxImage类对象读取图像,判断为真进入循环。

CxImage image;

image.Load(strFilePath,0);

if (image.IsValid())

{

int width = image.GetWidth();

int height = image.GetHeight();//获得宽高基本尺寸

int ColorType = image.GetColorType();//1 = indexed, 2 = RGB, 4 = RGBA, 获得颜色模型

int BytePerLine = image.GetEffWidth();

  //(bitCount*width + 3)/ 4* 4;//4字节对齐

//int BytePerLine =((((image.GetBpp() * width) + 31) / 32) * 4);

/*两个宽度,一个是图像的宽度,例如201。另一个宽度是你的数据矩阵的宽 度,BytePerLine。这个值根据你的图像而定,如果你的图像是24位真彩, 201*3=603,这个数据不能被4整除,必须强制其等于604,这样你的数据矩阵 就是604*height。显示时的宽度还是你的201不变*/

BYTE* pDib = image.GetBits();

BYTE* buffer = new BYTE[height*BytePerLine];

for (int j = 0; j < height; j++)

{

for (int i = 0; i < width; i++)

{

buffer[j*BytePerLine+i*3] = *(pDib+(height-1- j)

*BytePerLine + i*3);

buffer[j*BytePerLine+i*3+1] = *(pDib+(height-1-j)

*BytePerLine + i*3+1);

buffer[j*BytePerLine+i*3+2] = *(pDib+(height-1-j)

*BytePerLine + i*3+2);

}

}

delete[] buffer; 

}

或者直接用GetDIB()函数。

CxImage image;

image.Load(strFilePath,0);

if (image.IsValid())

{

int width = image.GetWidth();

int height = image.GetHeight();

int ColorType = image.GetColorType();

int BytePerLine = image.GetEffWidth();

BYTE* pDib = image.GetDIB();

BYTE* buffer = new BYTE[height*BytePerLine];

memset(buffer,0,height*BytePerLine);

memcpy(buffer,pdib,height*BytePerLine);

//特别注意:这里图像是倒像咬

}

delete[] buffer; 

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值