请问如何用C++builder通过像素点矩阵存储图片

wenchangwc 2011-09-20 12:24:26
我有一个32*32的的矩阵W[32][32],怎么样用这个矩阵构建一张32*32的256色位图并保存呢?
谢谢了啊~
我用了一个Bitmap类去实现,可总是会出现Scan line 溢出错误
try
{
byte *ptr;
for(int y = 0; y<pBitmap-> Height;y++)
{
ptr=(byte *)pBitmap-> ScanLine[y];
for (int x=0;x<pBitmap-> Width;x++)
ptr[x]=BitMapArray[y][x];
}
}
catch (...)
{
ShowMessage( "Could not load or alter bitmap ");
return;
}
...全文
129 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cczlp 2011-09-20
  • 打赏
  • 举报
回复
上面示例for里面少个i++, 发上来才看到.
cczlp 2011-09-20
  • 打赏
  • 举报
回复
void SetGrayToBitmap(BYTE *pBuf, int w, int h, Graphics::TBitmap *dstbmp)
{
typedef struct
{
WORD palVersion;
WORD palNumEntries;
PALETTEENTRY dummy[256];
} LOGPAL;
LOGPAL GrayPal;

GrayPal.palVersion=0x300;
GrayPal.palNumEntries=256;
for (int i=0;i<256;i++)
{
GrayPal.dummy[i].peRed=i;
GrayPal.dummy[i].peGreen=i;
GrayPal.dummy[i].peBlue=i;
GrayPal.dummy[i].peFlags=0;
}

Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Width = w;
bmp->Height = h;
bmp->PixelFormat=pf8bit;
bmp->Palette= CreatePalette((const tagLOGPALETTE *)&GrayPal);

for (int y = 0; y < h; y++)
{
memcpy(bmp->ScanLine[y], pBuf, w);
pBuf += w;
}
if (!dstbmp->Empty)
{
dstbmp->FreeImage();
}
dstbmp->Assign(bmp);
delete bmp;
}
//用法:
Graphics::TBitmap *bmp = new Graphics::TBitmap;
BYTE GrayBuf[32*32];
for (int i = 0; i < 32*32) GrayBuf[i] = i%256;//灰度图像数据
SetGrayToBitmap(GrayBuf, 32, 32, bmp);
bmp->SaveToFile("c:\\test.bmp");
delete bmp;
zzbinfo 2011-09-20
  • 打赏
  • 举报
回复
代码杯具了,自己到连接那看吧
zzbinfo 2011-09-20
  • 打赏
  • 举报
回复
给你抄了一段,看能不能行。http://www.cnblogs.com/xidiandaily/archive/2011/03/24/1993336.html
#include "EasyBMP.h"int array[3][3]={{0,1,0,},{1,0,1,},{0,1,0,},};//数组转图像void array2bmp(){    int i,j;    BMP bmp;    RGBApixel pix_black={0};//R=0 G=0 B=0为黑色    RGBApixel pix_white={255,255,255,0};//白色    bmp.SetSize(3,3);    bmp.SetBitDepth(1);    for(i=0;i<3;i++)    {        for(j=0;j<3;j++)        {            if(array[i][j]==1)            {                bmp.SetPixel( i,  j,pix_black);            }            else            {                bmp.SetPixel( i,  j,pix_white);            }        }    }    bmp.WriteToFile("examp_array2bmp.bmp");    printf("array2bmp suc...\n");}//图像转数组void bmp2array(){    int i,j;    BMP bmp;    int *pdata=NULL;    int *phead=NULL;    int *buf=NULL;    int width;    int height;    bmp.ReadFromFile("examp_bmp2array.bmp");    width = bmp.TellWidth();    height= bmp.TellHeight();    pdata=(int*)malloc(width*height*sizeof(int));    phead = pdata;    for(i=0;i<height;i++)    {        for(j=0;j<width;j++)        {//打印模拟图,空白为'.',黑色为'M'            *pdata=bmp(j,i)->Red;//位深1,读Red分量即可            pdata++;        }    }    //save    pdata=phead;    for(i=0;i<height;i++)    {        for(j=0;j<width;j++)        {//打印至终端            printf("%d,",*pdata);            pdata++;        }        printf("\n");    }    printf("bmp2array suc...\n");    getchar();}int main(){    array2bmp();    bmp2array();    return 1;}

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧