像素数据到CBitmap [英] Pixel data to CBitmap

查看:94
本文介绍了像素数据到CBitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时遇到运行时错误。任何人都可以帮忙。

A runtime error is encountered while running this code. Can anyone please help.

int* pix = new int[50 * 50 * sizeof(int)];
for(int i = 0; i < 50; i++)
{
    for(int j = 0; j < 50; j++)
    {
        *pix = j;
        pix++;
    }
}
CBitmap* img = 0;
img->CreateBitmap(50, 50, 1, 8, pix);

推荐答案

您正在定义指向 CBitmap 的指针,并使用 NULL 初始化此指针。然后你使用CBitmap指针:

You are defining a pointer to CBitmap and initialize this pointer with NULL. Then you are using the CBitmap pointer:
CBitmap* img = 0;
// This is a NULL pointer access resulting in a runtime error:
// Think of: NULL->CreateBitmap()
img->CreateBitmap(50, 50, 1, 8, pix);



你必须为 CBitmap 使用 new 对象或在堆栈上创建对象:


You must allocate memory for the CBitmap object using new or create the object on the stack:

// Allocate object
CBitmap* pImg = new CBitmap;
pImg->CreateBitmap(50, 50, 1, 8, pix);
// Using pImg here
delete pImg;

// Or create object on the stack
CBitmap Img;
Img.CreateBitmap(50, 50, 1, 8, pix);


这篇关于像素数据到CBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆