二进制打开和复制图像文件 c++
Binary open and copy image file c++
我想将一个图像文件复制到另一个新文件。这是我执行此操作的方法:
std::ofstream myOutpue;
std::ifstream mySource;
//int i = 0;
mySource.open(ofn.lpstrFile, std::ios::binary);
myOutpue.open("im4.jpg", std::ios::binary);
char buffer;
char bufferToSave[100];
if (mySource.is_open())
{
//client->sendFilePacket(FileStates::START_SAVE, buffer, false,i);
i++;
while (!mySource.eof())
{
mySource >> std::noskipws >> buffer;
myOutpue << buffer;
//client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false,i);
i++;
}
}
i++;
//client->sendFilePacket(FileStates::END_SAVE, buffer, true,i);
mySource.close();
//myOutpue.close();
此方法工作正常,但我的问题是我想复制 char/bit 并将其发送到另一个客户端。当我通过每个字符执行此操作时,无法正常工作,因此我想制作一个更大的buffor(例如char t[512])或类似的东西并将它们复制到新文件中。
我尝试这样做:
std::ofstream myOutpue;
std::ifstream mySource;
mySource.open(ofn.lpstrFile, std::ios::binary);
myOutpue.open("im4.jpg", std::ios::binary);
char buffer;
char bufferToSave[100];
if (mySource.is_open())
{
//client->sendFilePacket(FileStates::START_SAVE, buffer, false,i);
i++;
while (!mySource.eof())
{
if (i == 100)
{
for (int i = 0; i < 100; i++)myOutpue << bufferToSave[i];
i = 0;
}
mySource >> std::noskipws >> buffer;
bufferToSave[i] = buffer;
//myOutpue << buffer;
//client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false,i);
i++;
}
}
i++;
//client->sendFilePacket(FileStates::END_SAVE, buffer, true,i);
mySource.close();
myOutpue.close();
但是我得到的图像我无法打开。
所以我的问题是如何读取文件以从中获取更多位,并为我创建与原始图像相同的图像。
原始文件复制算法中存在错误,永远不应使用 eof()
作为结束标志进行循环。
请参阅:为什么循环条件中的 iostream::eof 被认为是错误的?
复制文件可以很简单,如下所示:
std::ofstream("output.jpg", std::ios::binary) << std::ifstream("input.jpg", std::ios::binary).rdbuf();
它在传递std::istream
缓冲区时使用输出运算符的特殊重载(使用 rdbuf()
)。它复制整个流。
读取整个缓冲区时,应使用std::istream::read
:
std::ifstream ifs("input.jpg", std::ios::binary)
char buffer[1025]; // create a buffer
// keep going as long as the reading succeeds
while(ifs.read(buffer, sizeof(buffer)))
{
// ifs.gcount() is the number of chars read successfully
client->sendFilePacket(buffer, ifs.gcount()); // send all bytes
}
已经很长时间了,但是阅读这些主题我找到了解决方案:
std::ifstream ifs(ofn.lpstrFile, std::ios::binary);
std::ofstream myOutpue;
char buffer[1024]; // create a buffer
myOutpue.open("output.jpg", std::ios::binary);
//client->sendFilePacket(FileStates::START_SAVE, buffer, false, i);
while (ifs.read(buffer, sizeof(buffer)))
{
myOutpue.write(buffer, ifs.gcount());
}
//
myOutpue.write(buffer, ifs.gcount());
myOutpue.close();
注意:我的答案类似于@dawcza94,但为了避免黑屏,循环后您必须保存其余的读数,因为在循环中您只保存适合缓冲区的内容,其余部分您忽略。有时,其余部分可能只有几个字符长,看起来图像的大小相同,但事实并非如此。
注意2:我在这里发帖是为了帮助那些仍然像我一样陷入困境的人!!
C++ FAQ:
您可能希望使用 iostream 的 read() 和 write() 方法,而不是它的>> 和 <<运算符。 read() 和 write() 更适合二进制模式;>> 和 <<更适合文本模式。
您可以指定要read
多少。通过gcount
您可以询问成功读取了多少字符。write
也是如此。
我尝试使用以下代码:
std::ifstream ifs(ofn.lpstrFile, std::ios::binary);
std::ofstream myOutpue;
char buffer[1024]; // create a buffer
myOutpue.open("output.jpg", std::ios::binary);
//client->sendFilePacket(FileStates::START_SAVE, buffer, false, i);
while (ifs.read(buffer, sizeof(buffer)))
{
//client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false, ifs.gcount());
myOutpue.write(buffer, ifs.gcount());
}
//client->sendFilePacket(FileStates::END_SAVE, buffer, true, i);
myOutpue.close();
但是当我这样做时,在我的图像副本中,我只得到了原始图像的一半和黑屏的一半(kb的数量与原始文件中相同),所以我不知道这有什么问题?
不要使用"手动"复制,而是尝试使用 ifstream::read 方法
- 在qt窗口中具有图像,而无需将图像文件本身作为源
- 如何从所选目录(位于listWidget中)中筛选图像文件
- 如何将图像文件夹添加到CMake项目以便在c ++可执行文件中使用这些图像?
- 解码并保存 base64 C++中的图像文件
- BASE64图像文件用C 编码
- 如何在使用 c++/COM 发送时在电子邮件中插入/嵌入图像文件(.png)
- 如何使用Imebra库将压缩像素(用我自己的编码器压缩)回到DICOM图像文件中
- 使用 Imread 打开具有 Unicode 名称的图像文件
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- C 读取/写作PPM图像文件灰色图像
- 如何在C 中删除或删除.jpg和.dpx图像文件
- C++图像处理 - 将图像文件读入 2D 数组
- 将图像文件(.BMP)复制到其他位置
- 从位图图像文件读取像素数据值
- Boost:Asio.从服务器下载图像文件
- 打开不同的图像文件,并使用GDI 库在主应用程序窗口的背景上绘制它们
- 使用 QPixmap 和 QLabel 在 Qt Creator 中以编程方式添加图像文件 (.PNG)
- C++:将整数的文本文件转换为BMP格式的位图图像文件
- 读取/写入PPM图像文件C++
- 将C++图像文件转换为数组