图片转二进制文件

30 篇文章 0 订阅

将图片转换为二进制文件的步骤如下:

  1. 打开图片文件:使用适当的编程语言和文件操作函数,如c++中的std::ifstream或python中的open函数,打开要转换的图片文件

  2. 读取图片数据:使用文件操作函数从打开的图片文件中读取数据。可以使用适当的读取函数,如c++中的rdbuf函数或python中的read函数,读取图片的字节数据

  3. 将图片数据写入二进制文件:创建一个新的二进制文件,并将从图片文件中读取的数据写入该文件。使用适当的文件操作函数,如c++中的std::ofstream或python中的open函数,以二进制模式打开文件,并使用适当的写入函数,如c++中的write函数或python中的文件对象的write方法,将图片数据写入文件

c++实现

#include <iostream>
#include <fstream>

int main() {
    std::ifstream imageFile("image.jpg", std::ios::binary); // 打开图片文件
    std::ofstream binaryFile("image.bin", std::ios::binary); // 创建二进制文件

    if (imageFile && binaryFile) {
        // 读取图片数据并写入二进制文件
        binaryFile << imageFile.rdbuf();
        std::cout << "图片已成功转换为二进制文件。" << std::endl;
    } else {
        std::cout << "无法打开文件。" << std::endl;
    }

    // 关闭文件
    imageFile.close();
    binaryFile.close();

    return 0;
}

python实现

with open('image.jpg', 'rb') as image_file:  # 打开图片文件
    with open('image.bin', 'wb') as binary_file:  # 创建二进制文件
        binary_file.write(image_file.read())  # 读取图片数据并写入二进制文件
    print("图片已成功转换为二进制文件。")

两种语言转换的二进制文件对比

c++

#include <iostream>
#include <fstream>

bool compareBinaryFiles(const std::string& file1, const std::string& file2) {
    std::ifstream binaryFile1(file1, std::ios::binary); // 打开第一个二进制文件
    std::ifstream binaryFile2(file2, std::ios::binary); // 打开第二个二进制文件

    if (!binaryFile1 || !binaryFile2) {
        std::cout << "无法打开文件。" << std::endl;
        return false;
    }
    
    // 判断两个文件的所有字节是否一致
    char byte1, byte2;
    bool filesAreEqual = true;
    while (binaryFile1.get(byte1) && binaryFile2.get(byte2)) {
        if (byte1 != byte2) {
            filesAreEqual = false;
            break;
        }
    }

    // 检查两个文件是否同时到达文件末尾
    if ((binaryFile1.eof() && !binaryFile2.eof()) || (!binaryFile1.eof() && binaryFile2.eof())) {
        filesAreEqual = false;
    }

    // 关闭文件
    binaryFile1.close();
    binaryFile2.close();

    return filesAreEqual;
}

int main() {
    std::string file1 = "image1.bin";
    std::string file2 = "image2.bin";

    if (compareBinaryFiles(file1, file2)) {
        std::cout << "两个二进制文件内容一致" << std::endl;
    } else {
        std::cout << "两个二进制文件内容不一致" << std::endl;
    }

    return 0;
}

python

def compare_binary_files(file1, file2):
    with open(file1, 'rb') as binary_file1, open(file2, 'rb') as binary_file2:
        byte1 = binary_file1.read(1)
        byte2 = binary_file2.read(1)
        while byte1 and byte2:
            if byte1 != byte2:
                return False
            byte1 = binary_file1.read(1)
            byte2 = binary_file2.read(1)
        return True

file1 = 'image1.bin'
file2 = 'image2.bin'

if compare_binary_files(file1, file2):
    print("两个二进制文件内容一致")
else:
    print("两个二进制文件内容不一致")
  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
图片的常见存储与读取凡是有以下几种: 存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[]. 1.参数是图片路径:返回Byte[]类型: public byte[] GetPictureData(string imagepath) { /**/////根据图片文件的路径使用文件流打开,并保存为byte[] FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法 byte[] byData = new byte[fs.Length]; fs.Read(byData, 0, byData.Length); fs.Close(); return byData; }2.参数类型是Image对象,返回Byte[]类型: public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { //将Image换成流数据,并保存为byte[] MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; }好了,这样通过上面的方法就可以把图片换成Byte[]对象,然后就把这个对象保存到数据库中去就实现了把图片二进制格式保存到数据库中去了。下面我就谈谈如何把数据库中的图片读取出来,实际上这是一个相反的过程。 读取图片:把相应的字段换成Byte[]即:Byte[] bt=(Byte[])XXXX 1.参数是Byte[]类型,返回值是Image对象: public System.Drawing.Image ReturnPhoto(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; }2.参数是Byte[] 类型,没有返回值,这是针对asp.net中把图片从输出到网页上(Response.BinaryWrite) public void WritePhoto(byte[] streamByte) { // Response.ContentType 的默认值为默认值为“text/html” Response.ContentType = "image/GIF"; //图片输出的类型有: image/GIF image/JPEG Response.BinaryWrite(streamByte); }补充: 针对Response.ContentType的值,除了针对图片的类型外,还有其他的类型: Response.ContentType = "application/msword"; Response.ContentType = "application/x-shockwave-flash"; Response.ContentType = "application/vnd.ms-excel";另外可以针对不同的格式,用不同的输出类型以适合不同的类型: switch (dataread("document_type")) { case "doc": Response.ContentType = "application/msword"; case "swf": Response.ContentType = "application/x-shockwave-flash"; case "xls": Response.ContentType = "application/vnd.ms-excel"; case "gif": Response.ContentType = "image/gif"; case "Jpg": Response.ContentType = "image/jpeg"; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chen_znn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值