BitBlt绘制位图倒置 [英] BitBlt drawing bitmap upside down

查看:119
本文介绍了BitBlt绘制位图倒置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MFC控件,向该控件传递了位图(HBITMAP)的句柄.在控件的OnPaint方法中,我使用BitBlt渲染位图.但是位图是颠倒的.

I have an MFC control to which I pass a handle to a bitmap (HBITMAP). In the controls OnPaint method I am using BitBlt to render the bitmap. But the bitmap is being rendered upside down.

作为测试,我从该句柄创建了一个CBitmap对象,并将其写到文件中,并创建了一个朝上的位图.那我对BitBlt的呼叫做错了吗?

As a test I created a CBitmap object from this handle and write that out to a file and it created a bitmap that was right side up. So am I doing something wrong with my call to BitBlt?

我已经从下面的OnPaint发布了我的代码.我确实尝试将设备上下文的映射模式更改为MM_LOENGLISH,并且能够获取位图以使其右侧向上渲染,但是它非常粗糙.当我将映射模式保留为MM_TEXT时,图像质量是完美的,但正如我所说的那样,它是颠倒的.我在位图,blitting等方面工作不多...所以我可能会缺少一些简单的东西.任何其他建议,将不胜感激.对于某些背景,我从摄像机驱动程序中获取BYTE *,并创建HBITMAP来呈现视频. 我怎样才能使它正确渲染?非常感谢

I've posted my code from OnPaint below. I did try to change the mapping mode of my device context to MM_LOENGLISH and was able to get the bitmap to render right side up but it was very grainy. When I leave the mapping mode at MM_TEXT the quality of the image is perfect but as I said it is upside down. I haven't worked much with bitmaps, blitting etc...so I could be missing something easy. Any other suggestions would be appreciated. For some background I'm grabbing a BYTE* from a video camera driver and creating the HBITMAP to render the video. How can I get this to render properly?? Many thanks

void BitmapControl::OnPaint()
{
EnterCriticalSection (&CriticalSection);

if (_handleBMP)
{

    CPaintDC dc(this);
    //dc.SetMapMode(MM_LOENGLISH);
    CDC dcMem;
    dcMem.CreateCompatibleDC(&dc);

    CRect rect;
    GetClientRect(&rect);
    dc.DPtoLP(&rect);


    CBitmap* pBmpOld = dcMem.SelectObject(CBitmap::FromHandle(_handleBMP));
    BitBlt(dc,rect.left,rect.top,rect.Width(),rect.Height(),dcMem,rect.left,rect.top,SRCCOPY); //works with MM_TEXT but upsidedown
    //BitBlt(dc,0,rect.bottom,rect.Width(),-rect.Height(),dcMem,0,0,SRCCOPY); //works with MM_LOENGLISH
    dcMem.SelectObject(pBmpOld);
    DeleteDC(dc);
    DeleteDC(dcMem);
    DeleteObject(_handleBMP);
    DeleteObject(pBmpOld);
    _handleBMP = NULL;

}
LeaveCriticalSection (&CriticalSection);
}

编辑* 我以为是因为我可以按正确的方向将位图保存到磁盘,所以问题出在bitblt上.这是我用来生成HBITMAP的代码.

edit* I was assuming because I could save the bitmap to disk in the correct orientation that the problem was with bitblt. Here is the code I use to generate the HBITMAP.

 HBITMAP BitmapWriter::CreateBitmapFromFrame(BYTE* frame)
{
BITMAPFILEHEADER* bmfh;
bmfh = (BITMAPFILEHEADER*)frame;

BITMAPINFOHEADER* bmih = &_bmi;

BITMAPINFO* bmpInfo = (BITMAPINFO*)bmih;

HBITMAP hbmp = CreateDIBSection(_hdc,bmpInfo,DIB_RGB_COLORS,NULL,NULL,0);
SetBitmapBits(hbmp,_bmi.biSizeImage,frame);


return hbmp;
}

哦,我使用了关键部分,因为我将hbitmap传递给属性中的控件,然后在OnPaint中对其进行访问.如果这是一个潜在的问题,我将不得不重新考虑.谢谢

Oh, and I used the critical section because I pass the hbitmap to the control in a property and then access it in the OnPaint. If that's a potential problem I will have to rethink that. Thanks

推荐答案

Windows位图首先存储在最底行.世界上大多数其他国家/地区都首先使用顶线,因此我想这就是您从相机中获得的东西.

Windows bitmaps are stored with the bottom line first. Most of the rest of the world works top line first, so I presume that's what you're getting from your camera.

您可以在BITMAPINFOHEADER结构中使用负高度来反转正常顺序.

You can use a negative height in the BITMAPINFOHEADER structure to reverse the normal order.

这篇关于BitBlt绘制位图倒置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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