微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C中使用bmp文件拍摄屏幕截图?

如何解决在C中使用bmp文件拍摄屏幕截图?

我正在尝试使用c创建3D游戏。

添加标志“ --save”时 游戏需要截图并保存。

为此(从游戏中截取屏幕截图)。 我必须使用 BMP文件,所以必须使用c来制作。

代码

#include "../includes/cub3d.h"

t_screenshot        *ft_init_shot(t_mlx *mlx)
{
    t_screenshot    *takeshot;

    takeshot = malloc(1 * sizeof(t_screenshot));
    takeshot->width = w;
    takeshot->height = h;
    takeshot->bitcount = 24;
    takeshot->width_in_bytes = ((takeshot->width * takeshot->bitcount + 31) / 32) * 4;
    takeshot->imagesize = takeshot->width_in_bytes * takeshot->height;
    takeshot->filesize = 54 + takeshot->imagesize;
    return (takeshot);
}

unsigned char       *ft_bitheader(t_mlx *mlx)
{
    unsigned char    *header;
    uint32_t bisize;
    uint32_t bfoffbits;
    uint16_t biplanes;
    
    header =(unsigned char *)malloc(54 * sizeof(char));
    bisize = 40;
    bfoffbits = 54;
    biplanes = 1;
    memcpy(header,"BM",2);
    memcpy(header + 2,&mlx->shot->filesize,4);
    memcpy(header + 10,&bfoffbits,4);
    memcpy(header + 14,&bisize,4);
    memcpy(header + 18,&mlx->shot->width,4);
    memcpy(header + 22,&mlx->shot->height,4);
    memcpy(header + 26,&biplanes,2);
    memcpy(header + 28,&mlx->shot->bitcount,2);
    memcpy(header + 34,&mlx->shot->imagesize,4);
    return (header);
}

void                screen_shot(t_mlx *mlx)
{
    ft_move(mlx);
    ft_update(mlx,YES);
    mlx->shot = ft_init_shot(mlx);
    mlx->shot->header = ft_bitheader(mlx);
    screno(mlx);
}
 
void                screno(t_mlx *mlx)
{
    int x ;
    int y ;
    int row;
    int col;

    x = 0;
    y = 0;
    row = mlx->shot->height - 1;
    col = 0;
    unsigned char* buf = malloc(mlx->shot->imagesize);
    while (row >= 0)
    {
        y = 0;
        col = 0;
        while (col < mlx->shot->width)
        {
            int red = (mlx->tex.img_data[x * w + y] >> 16) & 0xFF;
            int green = (mlx->tex.img_data[x * w + y] >> 8) & 0xFF;
            int blue = mlx->tex.img_data[x * w + y] & 0xFF;
            buf[row * mlx->shot->width_in_bytes + col * 3 + 0] = blue;
            buf[row * mlx->shot->width_in_bytes + col * 3 + 1] = green;
            buf[row * mlx->shot->width_in_bytes + col * 3 + 2] = red;
            coL++;
            y++;
        }
        row--;
        x++;
    }
    ft_printf("Taking ScreenShoot....\n");
    FILE *image = fopen("screenshot.bmp","wb");
    ft_printf("ScreenShot Has been saved under The name 'screenshot.bmp']\n");
    fwrite(mlx->shot->header,1,54,image);
    fwrite((char*)buf,mlx->shot->imagesize,image);
    fclose(image);
    free(buf);
}

问题是当我编译此代码时,图像出现了一段时间,并且有一段时间给我这个错误

screenshot to see the error I got

谢谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。