#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[]) {
FILE *fp = fopen("1.bmp", "rb+"); // 以读写模式打开文件
if (!fp) {
perror("无法打开文件");
return 1;
}
fseek(fp, 18, SEEK_SET);
double w = 0, h = 0;
fread(&w, sizeof(int), 1, fp);
fread(&h, sizeof(int), 1, fp);
double a = (1 * h) / 3;
double b = (2 * h) / 3;
fseek(fp, 54, SEEK_SET);
unsigned char arr[3] = {0, 0, 0};
unsigned char rrr[3] = {255, 0, 0};
unsigned char crr[3] = {255, 255, 0};
for (double i = 0; i < w; i++)
{
for (double j = 0; j < h; j++)
{
if (j < a)
{
fwrite(crr, sizeof(unsigned char), 3, fp); // 黄色
} else if (j < b)
{
fwrite(rrr, sizeof(unsigned char), 3, fp); // 红色
} else
{
fwrite(arr, sizeof(unsigned char), 3, fp); // 黑色
}
}
}
fclose(fp);
return 0;
}
使用fwrite、fread将一张随意的bmp图片,修改成德国的国旗
于 2024-03-03 14:48:26 首次发布