
应用场景:
必须显示图片以及图片的二进制数据. 例如,图像需要转换为二进制数据并以字符串形式发送,并且同时需要显示图像.
主要思想:

以二进制格式读取图像文件,然后将二进制数据加载到QImage中以实现显示效果. 之后qt读取图片并显示,根据需要,读取的二进制数据将由其自身进行处理.
主要代码:
//begin std::ifstream fileInput("F:\\VSprojects\\DChat\\1.jpg", std::ios_base::binary); //获取文件大小 fileInput.seekg(0, std::ios_base::end); const size_t maxSize = fileInput.tellg(); //重置文件指针 fileInput.seekg(0, std::ios_base::end); //读取图片文件 char *picBin = new char[maxSize]; fileInput.read(picBin, maxSize); //载入二进制数据初始化Qimage QImage img; img.loadFromData((uchar*)picBin, maxSize); //显示图片QImage QDialog *newDialog = new QDialog(); QLabel *picLabel = new QLabel(newDialog); picLabel->setPixmap(QPixmap::fromImage(img)); newDialog->show(); newDialog->setVisible(true); //复制图片 std::ofstream fileOutput("F:/VSprojects/DChat/2.jpg", std::ios_base::binary); fileOutput.write(picBin, maxSize); fileInput.close(); fileOutput.close(); //end

其他说明:
std :: ifstream :: seekg(streampos pos); //设置指针位置.
std :: ifstream :: seekg(strrampoff off,std :: ios_base :: seekdir方式); //设置指针的起始位置和偏移量qt读取图片并显示,off是偏移量.

参考:
结果截图:



最后:
如果您有任何错误或遗漏,请给我们您的建议.
Ifstream读取图片,Qt加载并显示
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-171973-1.html
因为他不配
现如今美帝弄死了老萨