由于内存不足错误,Setalphabitmap 无法正常工作
Setalphabitmap not working due to out of memory error
我正在尝试在直接显示应用程序中对实时视频流划清界限。我成功画了一条线,但如果我想通过调用下面的函数连续创建一条线,我从 SetAlphaBitmap()
收到错误 E_OUTOFMEMORY
。我一个接一个地调用drawlineOverlay()
函数,调用8次没问题,但是第9次和后面的就不行了。 SetAlphaBitmap
returns S_OK
前 8 个调用,然后是第 9 个 returns 8007000E
。但视频流正在和平地进行,只有覆盖图被挂起。如果我在有时视频流停止并挂起后继续调用该函数。
为什么会出现这个错误?
void drawlineOverlay(HWND m_hwndApp)
{
int cx, cy;
HRESULT hr;
HBITMAP hbm;
RECT rcClient;
GetResolution(&cx,&cy);
GetClientRect(m_hwndApp,&rcClient);
HDC hdc = GetDC(m_hwndApp);
if (hdc == NULL)
{
return E_FAIL;
}
HDC hdcBmp = CreateCompatibleDC(hdc);
if (hdcBmp == NULL)
{
return E_FAIL;
}
hbm = CreateCompatibleBitmap(hdc,cx,cy);
BITMAP bm;
if (0 == GetObject(hbm, sizeof(bm), &bm))
{
DeleteDC(hdcBmp);
return E_FAIL;
}
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);
if (hbmOld == 0)
{
DeleteDC(hdcBmp);
return E_FAIL;
}
//To draw line
drawLine1(xx1, yy1, xx2, yy2,hdcBmp,2);
VMR9AlphaBitmap bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
bmpInfo.dwFlags = VMRBITMAP_HDC | VMRBITMAP_SRCCOLORKEY;
bmpInfo.hdc = hdcBmp;
SetRect(&bmpInfo.rSrc, 0, 0, bm.bmWidth, bm.bmHeight);
bmpInfo.rDest.left = 0.f;
bmpInfo.rDest.top = 0.f;
bmpInfo.rDest.right = 1.0f;
bmpInfo.rDest.bottom = 1.0f;
// Set the transparency value (1.0 is opaque, 0.0 is transparent).
bmpInfo.fAlpha = 0.5f;
bmpInfo.clrSrcKey = RGB(0,0,0);
if(m_pVideoRender != NULL)
{
IVMRMixerBitmap9* pBmp;
hr = m_pVideoRender->QueryInterface(IID_IVMRMixerBitmap9, (LPVOID *)&pBmp);
if (SUCCEEDED(hr))
{
hr = pBmp->SetAlphaBitmap(&bmpInfo);
if(FAILED(hr))
{
PrintMessage(L"pBmp->SetAlphaBitmap hr = 0x%x GetLastError() = %d\r\n",hr,GetLastError());
}
pBmp->Release();
//SAFE_RELEASE(pBmp);
}
}
// Clean up.
ReleaseDC(m_hwndApp, hdc);
DeleteBitmap(hbm);
DeleteObject(SelectObject(hdcBmp, hbmOld));
DeleteDC(hdcBmp);
}
void drawLine1(int xx1, int yy1, int xx2, int yy2,HDC hdcBmp)
{
RECT clntRc;
int temp,s1,s2,swap;
double dx,dy,p,x,y;
x = xx1;
y = yy1;
dx = abs(xx2 - xx1);
dy = abs(yy2 - yy1);
s1 = sign(xx2 - xx1);
s2 = sign(yy2 - yy1);
swap = 0;
if (dy > dx)
{
temp = dx;
dx = dy;
dy = temp;
swap = 1;
}
p = 2*dy - dx;
for (int i = 0; i < dx; i++)
{
clntRc.left =x;
clntRc.top = y;
clntRc.right = x+g_Thickness;
clntRc.bottom =y+g_Thickness;
FillRect(hdcBmp,&clntRc,CreateSolidBrush(RGB(0,255,0)));
while (p >= 0)
{
p = p - 2*dx;
if (swap)
x += s1;
else
y += s2;
}
p = p + 2*dy;
if (swap)
y += s2;
else
x += s1;
}
}
CreateSolidBrush(RGB(0,255,0)) 正在泄漏资源。
您需要删除 CreateSolidBrush(RGB(0,255,0)) 返回的句柄:https://msdn.microsoft.com/en-us/library/windows/desktop/dd183523(v=vs.85).aspx
我正在尝试在直接显示应用程序中对实时视频流划清界限。我成功画了一条线,但如果我想通过调用下面的函数连续创建一条线,我从 SetAlphaBitmap()
收到错误 E_OUTOFMEMORY
。我一个接一个地调用drawlineOverlay()
函数,调用8次没问题,但是第9次和后面的就不行了。 SetAlphaBitmap
returns S_OK
前 8 个调用,然后是第 9 个 returns 8007000E
。但视频流正在和平地进行,只有覆盖图被挂起。如果我在有时视频流停止并挂起后继续调用该函数。
为什么会出现这个错误?
void drawlineOverlay(HWND m_hwndApp)
{
int cx, cy;
HRESULT hr;
HBITMAP hbm;
RECT rcClient;
GetResolution(&cx,&cy);
GetClientRect(m_hwndApp,&rcClient);
HDC hdc = GetDC(m_hwndApp);
if (hdc == NULL)
{
return E_FAIL;
}
HDC hdcBmp = CreateCompatibleDC(hdc);
if (hdcBmp == NULL)
{
return E_FAIL;
}
hbm = CreateCompatibleBitmap(hdc,cx,cy);
BITMAP bm;
if (0 == GetObject(hbm, sizeof(bm), &bm))
{
DeleteDC(hdcBmp);
return E_FAIL;
}
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);
if (hbmOld == 0)
{
DeleteDC(hdcBmp);
return E_FAIL;
}
//To draw line
drawLine1(xx1, yy1, xx2, yy2,hdcBmp,2);
VMR9AlphaBitmap bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
bmpInfo.dwFlags = VMRBITMAP_HDC | VMRBITMAP_SRCCOLORKEY;
bmpInfo.hdc = hdcBmp;
SetRect(&bmpInfo.rSrc, 0, 0, bm.bmWidth, bm.bmHeight);
bmpInfo.rDest.left = 0.f;
bmpInfo.rDest.top = 0.f;
bmpInfo.rDest.right = 1.0f;
bmpInfo.rDest.bottom = 1.0f;
// Set the transparency value (1.0 is opaque, 0.0 is transparent).
bmpInfo.fAlpha = 0.5f;
bmpInfo.clrSrcKey = RGB(0,0,0);
if(m_pVideoRender != NULL)
{
IVMRMixerBitmap9* pBmp;
hr = m_pVideoRender->QueryInterface(IID_IVMRMixerBitmap9, (LPVOID *)&pBmp);
if (SUCCEEDED(hr))
{
hr = pBmp->SetAlphaBitmap(&bmpInfo);
if(FAILED(hr))
{
PrintMessage(L"pBmp->SetAlphaBitmap hr = 0x%x GetLastError() = %d\r\n",hr,GetLastError());
}
pBmp->Release();
//SAFE_RELEASE(pBmp);
}
}
// Clean up.
ReleaseDC(m_hwndApp, hdc);
DeleteBitmap(hbm);
DeleteObject(SelectObject(hdcBmp, hbmOld));
DeleteDC(hdcBmp);
}
void drawLine1(int xx1, int yy1, int xx2, int yy2,HDC hdcBmp)
{
RECT clntRc;
int temp,s1,s2,swap;
double dx,dy,p,x,y;
x = xx1;
y = yy1;
dx = abs(xx2 - xx1);
dy = abs(yy2 - yy1);
s1 = sign(xx2 - xx1);
s2 = sign(yy2 - yy1);
swap = 0;
if (dy > dx)
{
temp = dx;
dx = dy;
dy = temp;
swap = 1;
}
p = 2*dy - dx;
for (int i = 0; i < dx; i++)
{
clntRc.left =x;
clntRc.top = y;
clntRc.right = x+g_Thickness;
clntRc.bottom =y+g_Thickness;
FillRect(hdcBmp,&clntRc,CreateSolidBrush(RGB(0,255,0)));
while (p >= 0)
{
p = p - 2*dx;
if (swap)
x += s1;
else
y += s2;
}
p = p + 2*dy;
if (swap)
y += s2;
else
x += s1;
}
}
CreateSolidBrush(RGB(0,255,0)) 正在泄漏资源。 您需要删除 CreateSolidBrush(RGB(0,255,0)) 返回的句柄:https://msdn.microsoft.com/en-us/library/windows/desktop/dd183523(v=vs.85).aspx