象素的编辑?如何在BMP文件中加入文字信息?需要哪些资料呀? 假设我在窗口的显示是1024*768如果图像小于窗口,按原始图像显示,如果图像大于窗口按照窗口显示图象.怎样转换比如16X16的字体,得到RGB,然后写入窗口的相对位置.不要用截屏的方式. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 创建内存兼容DC,将图像选入内存DC,然后在内存DC中写上你需要写的文字。最后将内存DC中的位图取出,保存成位图文件就可以了。不要考虑用像素的操作。 参考代码:void CCreateRandomBMPDlg::OnBtnTest() { // TODO: Add your control notification handler code here HBITMAP hBmp; CFileDialog dlg(TRUE,"bmp",NULL,0,"位图文件 (*.bmp)|*.bmp||",this); if(dlg.DoModal() != IDOK) { return; } hBmp = (HBITMAP)LoadImage(NULL,dlg.GetPathName(),IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); if(hBmp == NULL) { return; } BITMAP bm; PBITMAPINFO bmpInf; if(GetObject(hBmp,sizeof(bm),&bm)==0) return ; int nPaletteSize=0; if(bm.bmBitsPixel<16) nPaletteSize=(int)pow(2,bm.bmBitsPixel); bmpInf=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD)*nPaletteSize); //----------------------------------------------- bmpInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInf->bmiHeader.biWidth = bm.bmWidth; bmpInf->bmiHeader.biHeight = bm.bmHeight; bmpInf->bmiHeader.biPlanes = bm.bmPlanes; bmpInf->bmiHeader.biBitCount = bm.bmBitsPixel; bmpInf->bmiHeader.biCompression = BI_RGB; bmpInf->bmiHeader.biSizeImage = (bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel; bmpInf->bmiHeader.biXPelsPerMeter = 0; bmpInf->bmiHeader.biYPelsPerMeter = 0; bmpInf->bmiHeader.biClrUsed = 0; bmpInf->bmiHeader.biClrImportant = 0; //----------------------------------------------- HDC hDC = ::GetWindowDC(NULL); if(!::GetDIBits(hDC,hBmp,0,(WORD)bm.bmHeight,NULL,bmpInf,DIB_RGB_COLORS)) { LocalFree(bmpInf); ::ReleaseDC(NULL,hDC); return ; } void* buf=(void*) new char[bmpInf->bmiHeader.biSizeImage]; if(buf==NULL) { ::ReleaseDC(NULL,hDC); LocalFree(bmpInf); return ; } if(!::GetDIBits(hDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS)) { ::ReleaseDC(NULL,hDC); delete []buf; LocalFree(bmpInf); return ; } ::ReleaseDC(NULL,hDC); CString sMsg; sMsg.Format("BitsPixel:%d,width:%d,height:%d", bm.bmBitsPixel,bm.bmWidth,bm.bmHeight ); AfxMessageBox(sMsg); CClientDC dc(this); if(bm.bmBitsPixel == 8) { BYTE *pData = (BYTE*)buf; int nWidth = bm.bmWidth; while(nWidth %4 != 0) {//Bmp每行数据都是4个字节的整数倍。 nWidth++; } for(int i=0; i<bm.bmHeight; i++) { for(int j=0; j<bm.bmWidth; j++) { RGBQUAD rgbQ; rgbQ = bmpInf->bmiColors[pData[i*nWidth+j]]; dc.SetPixel(j,bm.bmHeight-i,RGB(rgbQ.rgbRed,rgbQ.rgbGreen,rgbQ.rgbBlue)); } } } else if(bm.bmBitsPixel == 16) { BYTE *pData = (BYTE*)buf; int nWidth = bm.bmWidth*2; while(nWidth %4 != 0) { nWidth++; } BYTE red,green,blue; for(int i=0; i<bm.bmHeight; i++) { for(int j=0; j<bm.bmWidth; j++) { blue = pData[i*nWidth+j*2]&0x1F; green = pData[i*nWidth+j*2]>>5; green |= (pData[i*nWidth+j*2+1]&0x03)<<3; red = (pData[i*nWidth+j*2+1]>>2)&0x1F; WORD wRed = red*8; WORD wBlue = blue*8; WORD wGreen = green*8; red = min(255,wRed); blue = min(255,wBlue); green = min(255,wGreen); dc.SetPixel(j,bm.bmHeight-i,RGB(red,green,blue)); } } } else if(bm.bmBitsPixel == 24) { BYTE *pData = (BYTE*)buf; int nWidth = bm.bmWidth*3; while(nWidth %4 != 0) { nWidth++; } for(int i=0; i<bm.bmHeight; i++) { for(int j=0; j<bm.bmWidth; j++) { dc.SetPixel(j,bm.bmHeight-i, RGB(pData[i*nWidth+j*3+2], pData[i*nWidth+j*3+1], pData[i*nWidth+j*3])); } } } else if(bm.bmBitsPixel == 32) { BYTE *pData = (BYTE*)buf; int nWidth = bm.bmWidth*4; for(int i=0; i<bm.bmHeight; i++) { for(int j=0; j< bm.bmWidth; j++) { dc.SetPixel(j,bm.bmHeight-i, RGB(pData[i*nWidth+j*4+2], pData[i*nWidth+j*4+1], pData[i*nWidth+j*4])); } } } delete []buf; DeleteObject(hBmp); LocalFree(bmpInf); } 每个点对应的RGB值可以这么修改://修改RGB值 int nWidth = btm.bmWidth * 4; for(int i=0; i<btm.bmHeight; i++) { for(int j=0; j< btm.bmWidth; j++) { lpData[i*nWidth+j*4+2] = GetRandomRGBValue(); //R lpData[i*nWidth+j*4+1] = GetRandomRGBValue(); //G lpData[i*nWidth+j*4] = GetRandomRGBValue(); //B TRACE("\nR = %d; G = %d; B = %d\n",lpData[i*nWidth+j*4+2],lpData[i*nWidth+j*4+1],lpData[i*nWidth+j*4]); } } 如何能响应Tab控件的子对话框里面的 MFC如何更改状态栏文本 请教:字符类型转换成十进制 动态创建对话框的问题 SQLSERVER 如何实现复制粘贴word中的对象如图片、公式等到richedit控件中,来者有分 ACCESS数据库损坏问题(急!!!!!) 用Alt+小键盘数字输入后得到汉字,例如按住Alt,在小键盘输入179179,然后放开Alt,得到汉字"浑",请问。。。 BCG界面布局的问题!!!界面开发的大牛请进~~~ 请教如何读取WAV文件显示成波形? 问一下做过CWnd的高手,为什么CWnd只能接收部分的WM_KEYDOWN消息 ? 关于char str[];
不要考虑用像素的操作。
void CCreateRandomBMPDlg::OnBtnTest()
{
// TODO: Add your control notification handler code here
HBITMAP hBmp;
CFileDialog dlg(TRUE,"bmp",NULL,0,"位图文件 (*.bmp)|*.bmp||",this);
if(dlg.DoModal() != IDOK)
{
return;
}
hBmp = (HBITMAP)LoadImage(NULL,dlg.GetPathName(),IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if(hBmp == NULL)
{
return;
}
BITMAP bm;
PBITMAPINFO bmpInf;
if(GetObject(hBmp,sizeof(bm),&bm)==0)
return ;
int nPaletteSize=0;
if(bm.bmBitsPixel<16)
nPaletteSize=(int)pow(2,bm.bmBitsPixel);
bmpInf=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+
sizeof(RGBQUAD)*nPaletteSize);
//-----------------------------------------------
bmpInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInf->bmiHeader.biWidth = bm.bmWidth;
bmpInf->bmiHeader.biHeight = bm.bmHeight;
bmpInf->bmiHeader.biPlanes = bm.bmPlanes;
bmpInf->bmiHeader.biBitCount = bm.bmBitsPixel;
bmpInf->bmiHeader.biCompression = BI_RGB;
bmpInf->bmiHeader.biSizeImage = (bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel;
bmpInf->bmiHeader.biXPelsPerMeter = 0;
bmpInf->bmiHeader.biYPelsPerMeter = 0;
bmpInf->bmiHeader.biClrUsed = 0;
bmpInf->bmiHeader.biClrImportant = 0;
//-----------------------------------------------
HDC hDC = ::GetWindowDC(NULL);
if(!::GetDIBits(hDC,hBmp,0,(WORD)bm.bmHeight,NULL,bmpInf,DIB_RGB_COLORS))
{
LocalFree(bmpInf);
::ReleaseDC(NULL,hDC);
return ;
}
void* buf=(void*) new char[bmpInf->bmiHeader.biSizeImage];
if(buf==NULL)
{
::ReleaseDC(NULL,hDC);
LocalFree(bmpInf);
return ;
}
if(!::GetDIBits(hDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS))
{
::ReleaseDC(NULL,hDC);
delete []buf;
LocalFree(bmpInf);
return ;
}
::ReleaseDC(NULL,hDC);
CString sMsg;
sMsg.Format("BitsPixel:%d,width:%d,height:%d",
bm.bmBitsPixel,bm.bmWidth,bm.bmHeight
);
AfxMessageBox(sMsg);
CClientDC dc(this);
if(bm.bmBitsPixel == 8)
{
BYTE *pData = (BYTE*)buf;
int nWidth = bm.bmWidth;
while(nWidth %4 != 0)
{//Bmp每行数据都是4个字节的整数倍。
nWidth++;
}
for(int i=0; i<bm.bmHeight; i++)
{
for(int j=0; j<bm.bmWidth; j++)
{
RGBQUAD rgbQ;
rgbQ = bmpInf->bmiColors[pData[i*nWidth+j]];
dc.SetPixel(j,bm.bmHeight-i,RGB(rgbQ.rgbRed,rgbQ.rgbGreen,rgbQ.rgbBlue));
}
}
}
else if(bm.bmBitsPixel == 16)
{
BYTE *pData = (BYTE*)buf;
int nWidth = bm.bmWidth*2;
while(nWidth %4 != 0)
{
nWidth++;
}
BYTE red,green,blue;
for(int i=0; i<bm.bmHeight; i++)
{
for(int j=0; j<bm.bmWidth; j++)
{
blue = pData[i*nWidth+j*2]&0x1F;
green = pData[i*nWidth+j*2]>>5;
green |= (pData[i*nWidth+j*2+1]&0x03)<<3;
red = (pData[i*nWidth+j*2+1]>>2)&0x1F;
WORD wRed = red*8;
WORD wBlue = blue*8;
WORD wGreen = green*8;
red = min(255,wRed);
blue = min(255,wBlue);
green = min(255,wGreen);
dc.SetPixel(j,bm.bmHeight-i,RGB(red,green,blue));
}
}
}
else if(bm.bmBitsPixel == 24)
{
BYTE *pData = (BYTE*)buf;
int nWidth = bm.bmWidth*3;
while(nWidth %4 != 0)
{
nWidth++;
}
for(int i=0; i<bm.bmHeight; i++)
{
for(int j=0; j<bm.bmWidth; j++)
{
dc.SetPixel(j,bm.bmHeight-i,
RGB(pData[i*nWidth+j*3+2],
pData[i*nWidth+j*3+1],
pData[i*nWidth+j*3]));
}
}
}
else if(bm.bmBitsPixel == 32)
{
BYTE *pData = (BYTE*)buf;
int nWidth = bm.bmWidth*4;
for(int i=0; i<bm.bmHeight; i++)
{
for(int j=0; j< bm.bmWidth; j++)
{
dc.SetPixel(j,bm.bmHeight-i,
RGB(pData[i*nWidth+j*4+2],
pData[i*nWidth+j*4+1],
pData[i*nWidth+j*4]));
}
}
}
delete []buf;
DeleteObject(hBmp);
LocalFree(bmpInf);
}
//修改RGB值
int nWidth = btm.bmWidth * 4;
for(int i=0; i<btm.bmHeight; i++)
{
for(int j=0; j< btm.bmWidth; j++)
{
lpData[i*nWidth+j*4+2] = GetRandomRGBValue(); //R
lpData[i*nWidth+j*4+1] = GetRandomRGBValue(); //G
lpData[i*nWidth+j*4] = GetRandomRGBValue(); //B
TRACE("\nR = %d; G = %d; B = %d\n",lpData[i*nWidth+j*4+2],lpData[i*nWidth+j*4+1],lpData[i*nWidth+j*4]);
}
}