如何使用C代码生成图像
下面的函数在调用时仅提供 html,但是
void generateHTML (int socket) {
char* message;
// Sends HTTP response header
message = "HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
message = "<HTML><BODY><P>Hello World.</P></BODY></HTML>\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
}
我在 Web 浏览器上的输出将是一条简单的 Hello World 消息。但是,我想更改它,以便它显示位图图像。让我们使用 1x1 红色像素作为 bmp。
到目前为止,我已经修改了这个函数:
void generateHTML (int socket) {
char* message;
// Sends HTTP response header
message = "HTTP/1.0 200 OK\r\n"
"Content-Type: image/bmp\r\n"
"Content-Length: ???WTF???\r\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
message = "BMF8\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
message = " "; //bmp file data goes here.
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
}
引用 Dan 的答案,十六进制数据看起来像:
0000000: 424d 3a00 0000 0000 0000 3600 0000 2800 BM:.......6...(.
0000010: 0000 0100 0000 0100 0000 0100 1800 0000 ................
0000020: 0000 0400 0000 130b 0000 130b 0000 0000 ................
0000030: 0000 0000 0000 0000 0000 ..........
但是,我根本无法将其放在引号内。我该怎么做?
The function below simply serves html when it is called, however
void generateHTML (int socket) {
char* message;
// Sends HTTP response header
message = "HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
message = "<HTML><BODY><P>Hello World.</P></BODY></HTML>\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
}
My output on the web browser would by a simple Hello World message. However, I want to change it so that it will display a bitmap image instead. Let's use a 1x1 red pixel as our bmp.
I've modified this function so far by:
void generateHTML (int socket) {
char* message;
// Sends HTTP response header
message = "HTTP/1.0 200 OK\r\n"
"Content-Type: image/bmp\r\n"
"Content-Length: ???WTF???\r\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
message = "BMF8\n";
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
message = " "; //bmp file data goes here.
printf ("about to send=> %s\n", message);
write (socket, message, strlen (message));
}
Quoting from Dan's answer, the data in hex looks like:
0000000: 424d 3a00 0000 0000 0000 3600 0000 2800 BM:.......6...(.
0000010: 0000 0100 0000 0100 0000 0100 1800 0000 ................
0000020: 0000 0400 0000 130b 0000 130b 0000 0000 ................
0000030: 0000 0000 0000 0000 0000 ..........
However, I simply cannot place that inside the quotation marks. How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 1x1 黑色 Windows BMP 图像的 xxd 转储:
this is an xxd dump of a 1x1 black windows bmp image:
扩展 @Dan D. 所说的内容,您可以在 Linux(也可能是 Windows)机器上使用 ImageMagick 的“转换”命令将原始数据转换为图像。我用 .png 进行了测试,您必须自己在 Windows 上测试输出:
expanding on what @Dan D. said, you can use ImageMagick's "convert" command on a Linux (or possibly Windows also) box to convert raw data to an image. I tested with .png, you'll have to test the output on Windows yourself:
要打印它,您需要创建一个数组
例如 unsigned char bmp[] ={
并用上面的十六进制填充此处的这部分。然而,由于它是十六进制的,因此您需要在每对数字的前面添加 0x。
例如
0x42、0x4d
ETC。
可以将其包含在 {} 括号中。
例如
无符号 char bmp[] ={
0x42、0x4d、0x……
}
然后使用 write 函数通过套接字发送它。
写(套接字,bmp,sizeof(bmp));
To print it out you need to create an array
e.g. unsigned char bmp[] ={
and fill this part here with hexadecimal above. However because it is in hexadecimal you need to add 0x to the front of each pair of numbers.
E.g.
0x42, 0x4d
Etc.
This can just be included in the {} brackets.
E.g.
unsigned char bmp[] ={
0x42, 0x4d, 0x....
}
then just send this through the socket using the write function.
write (socket, bmp, sizeof(bmp));