来自数据流的QImage

时间:2010-02-23 02:17:58

标签: c++ qt qt4 qimage

我正在使用Qt库,创建QImages。

我可以使用this constructor

QImage image("example.jpg");

但是我遇到了this static function

的问题
char buffer[sizeOfFile];
ifstream inFile("example.jpg");
inFile.read(buffer, sizeOfFile);
QImage image = QImage::fromData(buffer); // error here
// but there's nothing wrong with the buffer
ofstream outFile("bufferOut.jpg");
outFile.write(buffer, sizeOfFile);

Qt向控制台吐出的地方:

Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image

以上不是完全我所拥有的,但它是唯一重要的区别。 (我需要能够从缓冲区中读取,因为我正在打开zip存档中的图像。)

1 个答案:

答案 0 :(得分:3)

来自irc.freenode.net上的#qt的tnx:

解决方案是明确包含缓冲区长度。忽略一些unsigned charchar类型转换和其他细节,我应该使用的是类似于:

QImage image = QImage::fromData(buffer, sizeOfFile);