Click here to Skip to main content
15,893,923 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralSending email via outlook express Pin
saad khan6-Sep-02 6:48
saad khan6-Sep-02 6:48 
GeneralRe: Sending email via outlook express Pin
Stephane Rodriguez.6-Sep-02 6:54
Stephane Rodriguez.6-Sep-02 6:54 
GeneralRe: Sending email via outlook express Pin
Joaquín M López Muñoz6-Sep-02 6:59
Joaquín M López Muñoz6-Sep-02 6:59 
GeneralUpgrade Win 3.1 application Pin
Rico6216-Sep-02 6:39
Rico6216-Sep-02 6:39 
GeneralRe: Upgrade Win 3.1 application Pin
Joaquín M López Muñoz6-Sep-02 7:14
Joaquín M López Muñoz6-Sep-02 7:14 
GeneralRe: Upgrade Win 3.1 application Pin
Rico6216-Sep-02 7:54
Rico6216-Sep-02 7:54 
GeneralRe: Upgrade Win 3.1 application Pin
Garth J Lancaster11-Sep-02 18:23
professionalGarth J Lancaster11-Sep-02 18:23 
GeneralIDirectDrawSurface saving as a bitmap Pin
Todd Jeffreys6-Sep-02 6:34
Todd Jeffreys6-Sep-02 6:34 
I have an IDirectDrawSurface and I would like to save it as a bitmap. Im only interested in 24/32 formats. I have written some code, and it exports the file correctly. It locks the surface to get the bits, and then dumps it to a file. When I view the file with MS paint, it looks fine. However, when I open in Adobe Photoshop, the colors are off (I think the blue and red are switched). It seems odd to work in MSpaint but then not in photoshop. If anybody has experience doing this, please take a look at my code. Thanks
STDMETHODIMP CCaptureWindow::SaveScreenshot(LPTSTR pFile)
{
  if (!g_pCapture || !g_pBaseFilter)
	return E_UNEXPECTED;

  CComPtr<IDDrawExclModeVideo> ddemv;
  HRESULT hr;
  if (FAILED(g_pCapture->FindInterface(&PIN_CATEGORY_PREVIEW,
	&MEDIATYPE_Interleaved, g_pBaseFilter,
	IID_IDDrawExclModeVideo, (void**)&ddemv)))
  {
	if (FAILED(hr = g_pCapture->FindInterface(&PIN_CATEGORY_PREVIEW,
	  &MEDIATYPE_Video, g_pBaseFilter,
	  IID_IDDrawExclModeVideo, (void**)&ddemv)))
	{
	  return hr;
	}
  }
  // ok take the screenshot now
  CComPtr<IDirectDrawSurface> pSurface;
  CComPtr<IDirectDrawSurface7> pSurface7;
  BOOL bExternal;
  if (FAILED(hr=ddemv->GetDDrawSurface(&pSurface,&bExternal)))
	return hr;

  if (FAILED(hr=pSurface->QueryInterface(IID_IDirectDrawSurface7,(void**)&pSurface7)))
	return hr;

  
  DDSURFACEDESC2 desc;
  desc.dwSize = sizeof(desc);
  if (FAILED(hr=pSurface7->Lock(NULL,&desc,DDLOCK_WAIT | DDLOCK_READONLY,0)))
	return hr;

  // now make sure we can save it to a file
  if ((desc.ddpfPixelFormat.dwRGBBitCount < 24) || !(desc.ddpfPixelFormat.dwFlags & DDPF_RGB) || (desc.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXEDTO8))
  {
	pSurface7->Unlock(NULL);
	return E_UNEXPECTED;
  }

  // now create the file
  HANDLE hFile = CreateFile(pFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  if (hFile == INVALID_HANDLE_VALUE)
  {
	pSurface7->Unlock(NULL);
	return GetLastError();
  }

  CFile file(hFile);
  BITMAPFILEHEADER bfh;
  BITMAPINFO bmi;
  DWORD sizeImage;

  // fill in common areas
  bfh.bfType = MAKEWORD('B','M');
  bfh.bfReserved1 = 0;
  bfh.bfReserved2 = 0;
  bfh.bfOffBits = sizeof(bmi.bmiHeader);
  bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biXPelsPerMeter = 0;
  bmi.bmiHeader.biYPelsPerMeter = 0;
  bmi.bmiHeader.biClrUsed = 0;
  bmi.bmiHeader.biClrImportant = 0;
  bmi.bmiHeader.biWidth = desc.dwWidth;
  bmi.bmiHeader.biHeight = desc.dwHeight;

  /* this leaves only
  
	bfh.bfSize = sizeof(bfh) + sizeof(bmi.bmiHeader) + sizeImage;
	bmi.bmiHeader.biBitCount = ;
	bmi.bmiHeader.biSizeImage = sizeImage;

	*/
  if (desc.ddpfPixelFormat.dwRGBBitCount == 32)
  {
	// must do 32 bit case - downsample to 24 bit
	DWORD lPitch = desc.dwWidth * 3;
	// round it to a DWORD boundary
	if (lPitch & 3)
	  lPitch = (lPitch & ~3) + 4;

	// allocate memory for it
	sizeImage = desc.dwHeight * lPitch;
	LPBYTE pBitmapMem = (LPBYTE)CoTaskMemAlloc(sizeImage+4);
	if (!pBitmapMem)
	{
	  pSurface7->Unlock(NULL);
	  return E_OUTOFMEMORY;
	}  	
	
	// now do the conversion here
	LPBYTE pCurrentWriteMem = (LPBYTE)pBitmapMem;
	LPBYTE pCurrentReadMem = (LPBYTE)((LPBYTE)desc.lpSurface+((desc.dwHeight-1)*desc.lPitch));
	for (DWORD i=0;i<desc.dwHeight;++i)
	{
	  // current line pointer
	  LPBYTE curWLine = pCurrentWriteMem;
	  LPBYTE curRLine = pCurrentReadMem;
	  // now copy the entire line over
	  for (DWORD j=0;j<desc.dwWidth;++j)
	  {
		// RGB 
		// BGR
		pCurrentWriteMem[0] = pCurrentReadMem[0];
		pCurrentWriteMem[1] = pCurrentReadMem[1];
		pCurrentWriteMem[2] = pCurrentReadMem[2];
		
		pCurrentWriteMem += 3;
		pCurrentReadMem  += 4;
	  }
	  // update where lines point
	  pCurrentWriteMem = curWLine + lPitch;
	  pCurrentReadMem  = curRLine - desc.lPitch;
	}
	// update headers
	bfh.bfSize = sizeof(bfh) + sizeof(bmi.bmiHeader) + sizeImage;
	bmi.bmiHeader.biBitCount = 24;
	bmi.bmiHeader.biSizeImage = sizeImage;

	// write the stuff now
	file.Write(&bfh,sizeof(bfh));
	file.Write(&bmi,sizeof(bmi.bmiHeader));
	file.Write(pBitmapMem,sizeImage);

	CoTaskMemFree(pBitmapMem);
  }
  else
  {	
	sizeImage = desc.dwHeight * desc.lPitch;
	
	bfh.bfSize = sizeof(bfh) + sizeof(bmi.bmiHeader) + sizeImage;
	bmi.bmiHeader.biBitCount = (WORD)desc.ddpfPixelFormat.dwRGBBitCount;
	bmi.bmiHeader.biSizeImage = sizeImage;
	
	// write the stuff now
	file.Write(&bfh,sizeof(bfh));
	file.Write(&bmi,sizeof(bmi.bmiHeader));
	file.Write(desc.lpSurface,sizeImage);
  }
  
  file.Close();
  pSurface7->Unlock(NULL);
  return S_OK;
}

GeneralRe: IDirectDrawSurface saving as a bitmap Pin
Stephane Rodriguez.6-Sep-02 6:50
Stephane Rodriguez.6-Sep-02 6:50 
GeneralRe: IDirectDrawSurface saving as a bitmap Pin
Todd Jeffreys6-Sep-02 7:19
Todd Jeffreys6-Sep-02 7:19 
GeneralRe: IDirectDrawSurface saving as a bitmap Pin
Mike Nordell6-Sep-02 18:24
Mike Nordell6-Sep-02 18:24 
QuestionStrange resource stuff????? Pin
jimNLX6-Sep-02 5:54
jimNLX6-Sep-02 5:54 
AnswerRe: Strange resource stuff????? Pin
pnpfriend6-Sep-02 9:20
pnpfriend6-Sep-02 9:20 
GeneralRe: Strange resource stuff????? Pin
jimNLX6-Sep-02 9:23
jimNLX6-Sep-02 9:23 
GeneralRe: Strange resource stuff????? Pin
pnpfriend6-Sep-02 10:26
pnpfriend6-Sep-02 10:26 
GeneralRe: Strange resource stuff????? Pin
jimNLX6-Sep-02 10:49
jimNLX6-Sep-02 10:49 
GeneralRe: Strange resource stuff????? Pin
Jawache6-Sep-02 12:48
Jawache6-Sep-02 12:48 
GeneralOwner Drawn Menus Pin
Paul C6-Sep-02 5:42
Paul C6-Sep-02 5:42 
GeneralRe: Owner Drawn Menus Pin
Jawache6-Sep-02 12:51
Jawache6-Sep-02 12:51 
GeneralRe: Owner Drawn Menus Pin
Paul C6-Sep-02 23:47
Paul C6-Sep-02 23:47 
GeneralSet window to be topmost Pin
BlackSmith6-Sep-02 5:20
BlackSmith6-Sep-02 5:20 
GeneralRe: Set window to be topmost Pin
Joaquín M López Muñoz6-Sep-02 6:32
Joaquín M López Muñoz6-Sep-02 6:32 
GeneralVery strange behavior trying to exit. Please help! Pin
ns6-Sep-02 5:04
ns6-Sep-02 5:04 
GeneralRe: Very strange behavior trying to exit. Please help! Pin
[James Pullicino]6-Sep-02 5:10
[James Pullicino]6-Sep-02 5:10 
GeneralRe: Very strange behavior trying to exit. Please help! Pin
Joaquín M López Muñoz6-Sep-02 6:25
Joaquín M López Muñoz6-Sep-02 6:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.