哪位高手给指点指点:VC中获得一图片像素之后,又由Bitmap::FromBITMAPINFO()函数重新生成位图后,图像发生移动,图像左边一部分跑到位图右侧。

Ni259251 2014-04-17 10:25:47
具体代码如下:
Bitmap mybitmap(L"myimage2.jpg");
m_nBmpWidth =mybitmap.GetWidth();
m_nBmpHeight=mybitmap.GetHeight();
m_nBuf_Len_TwoDim = m_nBmpHeight*m_nBmpWidth;
p_image_Color_buf=new BYTE[3*m_nBuf_Len_TwoDim];

memset(m_image_buf,0,m_nBuf_Len_TwoDim);
memset(p_image_Color_buf,0,3*m_nBuf_Len_TwoDim);
Color mycolor;
int R, G, B;
for(int i = 0; i < m_nBmpHeight;i++)
{
for(int j = 0; j < m_nBmpWidth; j++)//x--width
{
mybitmap.GetPixel(j,i,&mycolor);//获得像素值
R=mycolor.GetR();
G=mycolor.GetG();
B=mycolor.GetB();
*(p_image_Color_buf+(599-i)*m_nBmpWidth*3+j*3)=B;
*(p_image_Color_buf+(599-i)*m_nBmpWidth*3+j*3+1)=G;
*(p_image_Color_buf+(599-i)*m_nBmpWidth*3+j*3+2)=R;
}//for(j)
m_pBmpInfo =GetBItmapInfo_Color(m_nBmpWidth, m_nBmpHeight); //生成位图文件头信息
m_Two_Image = Bitmap::FromBITMAPINFO(m_pBmpInfo,p_image_Color_buf);//由位图文件头和已读取的图片像素重新生成位图
原始图片:
后生成的位图:
哪位高手懂这个呀。给指点指点吧!小妹不胜感激!
...全文
260 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2014-04-20
  • 打赏
  • 举报
回复
不知道你的目的,下面是上下颠倒的代码:void CMyImageDlg::GetNewBitmap() { Bitmap myBitmap(L"Myimage2.jpg"); // 57,858 字节 m_nBmpWidth = myBitmap.GetWidth();// 650 if(m_nBmpWidth==0) return; // m_nBmpHeight=myBitmap.GetHeight();// 600 if(m_nBmpHeight==0) return; // check format PixelFormat pf=myBitmap.GetPixelFormat(); if(pf != PixelFormat24bppRGB) return; // make new Bitmap *pBitmap=myBitmap.Clone(0,0,m_nBmpWidth,m_nBmpHeight,pf); // upside down ! Color mycolor; for(int i = 0; i < m_nBmpHeight; i++) { for(int j = 0; j < m_nBmpWidth; j++)//x--width { myBitmap.GetPixel(j,i,&mycolor);//获得像素值 pBitmap->SetPixel(j,m_nBmpHeight-1-i,mycolor); } } // for paint Color backColor; // Argb 0xff000000 pBitmap->GetHBITMAP(backColor, &m_hBitmap); if(m_hBitmap ==0 ) return; } ////////////////////////////////// void CMyImageDlg::OnPaint() ...... else { CPaintDC dc(this); // device context for painting if(m_hBitmap) { CDC memDC; memDC.CreateCompatibleDC(&dc); HBITMAP hold=(HBITMAP)memDC.SelectObject(m_hBitmap); CRect rc; GetClientRect(&rc); dc.SetStretchBltMode(HALFTONE); dc.StretchBlt(rc.left,rc.top,rc.Width(),rc.Height(),&memDC, 0,0,m_nBmpWidth,m_nBmpHeight,SRCCOPY); // free memDC.SelectObject(hold); }
schlafenhamster 2014-04-20
  • 打赏
  • 举报
回复
void CMyImageDlg::GetNewBitmap() { Bitmap myBitmap(L"Myimage2.jpg"); // 57,858 字节 m_nBmpWidth = myBitmap.GetWidth();// 650 if(m_nBmpWidth==0) return; // m_nBmpHeight=myBitmap.GetHeight();// 600 if(m_nBmpHeight==0) return; // Color backColor;// Argb 0xff000000 myBitmap.GetHBITMAP(backColor, &m_hBitmap); if(m_hBitmap ==0 ) return; // PixelFormat pf=myBitmap.GetPixelFormat(); if(pf == PixelFormat24bppRGB) { afxDump << "The pixel format of is 24bppRGB.\n"; } ... 格式 PixelFormat24bppRGB 应该没有调色板.
schlafenhamster 2014-04-17
  • 打赏
  • 举报
回复
256 * sizeof(RGBQUAD) 能确定 调色板是 256 项 吗 ?
Ni259251 2014-04-17
  • 打赏
  • 举报
回复
我的位图文件头信息设置如下: BITMAPINFO* pInfo = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));//分配空间 BITMAPINFOHEADER Info_Header; Info_Header.biSize = sizeof(BITMAPINFOHEADER); Info_Header.biWidth = nBmpWidth; Info_Header.biHeight = nBmpHeight; Info_Header.biPlanes = 1; Info_Header.biBitCount = 24; Info_Header.biCompression = BI_RGB; Info_Header.biSizeImage = nBmpHeight * nBmpWidth*3; Info_Header.biXPelsPerMeter = 0; Info_Header.biYPelsPerMeter = 0; Info_Header.biClrUsed = 0; Info_Header.biClrImportant = 0; for(int i=0; i<256; i++) { pInfo->bmiColors[i].rgbBlue = (BYTE)i; pInfo->bmiColors[i].rgbGreen = (BYTE)i; pInfo->bmiColors[i].rgbRed = (BYTE)i; pInfo->bmiColors[i].rgbReserved = 0; } memcpy(pInfo, &Info_Header, sizeof(BITMAPINFOHEADER)); return pInfo;
Ni259251 2014-04-17
  • 打赏
  • 举报
回复
谢谢指点,不过还是不懂,那具体通过修改哪些参数可以改善呢?是文件头信息结构体重的参数?还是m_nBmpWidth =mybitmap.GetWidth();??
schlafenhamster 2014-04-17
  • 打赏
  • 举报
回复
宽度 不对 !引起 图形 位移, 一定要 4的 倍数。

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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