MFC 24位图转换成1位位图代码实现
时间: 2023-05-29 10:04:50 浏览: 50
以下是MFC 24位图转换成1位位图的代码实现:
1. 首先,加载24位位图并创建一张新的1位位图。
CImage imgSrc;
imgSrc.Load(_T("24bit.bmp"));
CImage imgDst;
imgDst.Create(imgSrc.GetWidth(), imgSrc.GetHeight(), 1);
2. 然后,遍历24位位图的每个像素,将其转换成1位像素并写入新的1位位图中。
for (int y = 0; y < imgSrc.GetHeight(); y++)
{
for (int x = 0; x < imgSrc.GetWidth(); x++)
{
COLORREF color = imgSrc.GetPixel(x, y);
BYTE r = GetRValue(color);
BYTE g = GetGValue(color);
BYTE b = GetBValue(color);
BYTE gray = (r * 30 + g * 59 + b * 11) / 100;
imgDst.SetPixel(x, y, gray > 128 ? RGB(255, 255, 255) : RGB(0, 0, 0));
}
}
3. 最后,保存新的1位位图。
imgDst.Save(_T("1bit.bmp"));
以上就是MFC 24位图转换成1位位图的代码实现。
相关问题
MFC 24位图转换成8位位图代码
以下是将MFC 24位图转换为8位位图的代码示例:
CBitmap bmp24; // 24位位图
CBitmap bmp8; // 8位位图
bmp24.LoadBitmap(IDB_MY24BITMAP); // 加载24位位图
BITMAP bmpInfo24;
bmp24.GetBitmap(&bmpInfo24);
int nWidth = bmpInfo24.bmWidth;
int nHeight = bmpInfo24.bmHeight;
// 创建8位位图
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = nWidth;
bmi.bmiHeader.biHeight = nHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 8;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = nWidth * nHeight;
BYTE* pBits24 = new BYTE[nWidth * nHeight * 3];
BYTE* pBits8 = new BYTE[nWidth * nHeight];
bmp24.GetBitmapBits(nWidth * nHeight * 3, pBits24);
for (int i = 0; i < nWidth * nHeight; i++)
{
pBits8[i] = (BYTE)((pBits24[i * 3] + pBits24[i * 3 + 1] + pBits24[i * 3 + 2]) / 3);
}
bmp8.CreateBitmap(nWidth, nHeight, 1, 8, pBits8); // 创建8位位图
delete[] pBits24;
delete[] pBits8;
// 显示8位位图
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATIC);
pStatic->SetBitmap(bmp8);
注意,此代码仅适用于将24位位图转换为8位灰度位图。如果需要将24位位图转换为8位彩色位图,需要使用调色板。
24位图转换成1位位图代码实现MFC
以下是一个简单的MFC程序,将24位图转换为1位位图:
1. 首先,在资源编辑器中添加一个24位位图(例如:IDB_MYBITMAP)。
2. 在CMainFrame类中添加以下成员变量:
```
CBitmap m_bitmap24; // 24位位图
CBitmap m_bitmap1; // 1位位图
```
3. 在CMainFrame类的OnCreate函数中添加以下代码:
```
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// 载入24位位图
m_bitmap24.LoadBitmap(IDB_MYBITMAP);
// 创建1位位图
m_bitmap1.CreateBitmap(320, 240, 1, 1, NULL);
// 转换位图
ConvertBitmap();
return 0;
}
```
4. 编写ConvertBitmap函数:
```
void CMainFrame::ConvertBitmap()
{
CDC dc;
dc.CreateCompatibleDC(NULL);
// 选择24位位图
CBitmap* pOldBitmap24 = dc.SelectObject(&m_bitmap24);
// 选择1位位图
CBitmap* pOldBitmap1 = dc.SelectObject(&m_bitmap1);
// 设置位图信息头
BITMAP bm24, bm1;
m_bitmap24.GetBitmap(&bm24);
m_bitmap1.GetBitmap(&bm1);
bm1.bmBitsPixel = 1;
bm1.bmPlanes = 1;
bm1.bmWidthBytes = ((bm24.bmWidth + 31) / 32) * 4;
// 创建缓冲区
BYTE* pBuffer24 = new BYTE[bm24.bmWidthBytes * bm24.bmHeight];
BYTE* pBuffer1 = new BYTE[bm1.bmWidthBytes * bm1.bmHeight];
// 读取24位位图数据
dc.GetBitmapBits(&m_bitmap24, bm24.bmWidthBytes * bm24.bmHeight, pBuffer24);
// 将24位位图转换为1位位图
for (int y = 0; y < bm24.bmHeight; y++)
{
for (int x = 0; x < bm24.bmWidth; x++)
{
BYTE* pPixel24 = pBuffer24 + (y * bm24.bmWidthBytes) + (x * 3);
BYTE* pPixel1 = pBuffer1 + (y * bm1.bmWidthBytes) + (x / 8);
BYTE bGray = (BYTE)((pPixel24[0] * 0.299) + (pPixel24[1] * 0.587) + (pPixel24[2] * 0.114));
if (bGray < 128)
{
pPixel1[0] |= (0x80 >> (x % 8));
}
}
}
// 写入1位位图数据
dc.SetBitmapBits(&m_bitmap1, bm1.bmWidthBytes * bm1.bmHeight, pBuffer1);
// 释放缓冲区
delete[] pBuffer24;
delete[] pBuffer1;
// 恢复DC状态
dc.SelectObject(pOldBitmap24);
dc.SelectObject(pOldBitmap1);
}
```
5. 在CMainFrame类的OnPaint函数中绘制1位位图:
```
void CMainFrame::OnPaint()
{
CPaintDC dc(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
// 选择1位位图
CBitmap* pOldBitmap = dcMem.SelectObject(&m_bitmap1);
// 绘制1位位图
dc.BitBlt(0, 0, 320, 240, &dcMem, 0, 0, SRCCOPY);
// 恢复DC状态
dcMem.SelectObject(pOldBitmap);
}
```
6. 运行程序,即可看到转换后的1位位图。