Opengl 实现截屏功

使用FreeImage提供的库,实现函数如下demo链接:https://github.com/liujiapeng550/jp_opengl

bool SaveImage(const char* imgpath)
{
    //创建一块内存
    unsigned char* mpixels = new unsigned char[800 * 600 * 4];
    //glReadBuffer函数指明要从哪个颜色缓存中读取数据
    glReadBuffer(GL_FRONT);
    //glReadPixels 做了实际的读取工作
    glReadPixels(0, 0, 800, 600, GL_BGRA, GL_UNSIGNED_BYTE, mpixels);
    glReadBuffer(GL_NONE);
    //for (int i = 0; i < (int)800 * 600 * 4; i += 4)
    //{
    //    mpixels[i] ^= mpixels[i + 2] ^= mpixels[i] ^= mpixels[i + 2];
    //}
    FIBITMAP* bitmap = FreeImage_Allocate(800, 600, 32, 8, 8, 8);

    for (int y = 0; y < FreeImage_GetHeight(bitmap); y++)
    {
        //bits返回指向位图数据位中给定扫描行开头的指针
        BYTE* bits = FreeImage_GetScanLine(bitmap, y);
        for (int x = 0; x < FreeImage_GetWidth(bitmap); x++)
        {
            bits[0] = mpixels[(y * 800 + x) * 4 + 0];
            bits[1] = mpixels[(y * 800 + x) * 4 + 1];
            bits[2] = mpixels[(y * 800 + x) * 4 + 2];
            bits[3] = 255;
            bits += 4;

        }

    }
    //将先前加载的FIBITMAP保存到文件中
    bool bSuccess = FreeImage_Save(FIF_PNG, bitmap, imgpath, PNG_DEFAULT);
    //释放位图
    FreeImage_Unload(bitmap);

    return bSuccess;

}

本文由博客一文多发平台 OpenWrite 发布!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值