Solved

Writing StretchBlt to a file

Posted on 2006-06-19
5
391 Views
Last Modified: 2008-02-01
I am currently working on a program that will take a screen capture and save it to a file, now I got that to work out just fine, but I am now attempting to to simeltaneuously create a thumbnails of the captured image by calling StretchBlt() immediatley following the screen capture. This works, and works well, but unfortuntely I cannot figure out how to write the contents of the buffer (the thumbnailed image turns up in the corner of my monitor) to a file say 1_t.bmp. Does anyone know of a way to do this? I have been racking my brains on how to do this but have yet to get it to work.  Thanks.

0
Comment
Question by:atrc
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 250 total points
ID: 16939383
0
 
LVL 22

Assisted Solution

by:mahesh1402
mahesh1402 earned 250 total points
ID: 16958690
You may use CreateDIBSection for this purpose.. Refer following functions
Have a look at Draw, Resize() and Save() functions, m_hBitmap is your Handle to bitmap(HBITMAP) and m_dibsec is DIBSECTION object

void MyBitmap::Draw(HDC hDC, int x, int y, int width, int height) const
{
 BITMAP bmp;
 GetObject(m_hBitmap, sizeof(bmp), & bmp);
 HDC hMemDC = CreateCompatibleDC(NULL);
 HGDIOBJ hOld = SelectObject(hMemDC, m_hBitmap);
 StretchBlt(hDC, x, y, width, height, hMemDC, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
 SelectObject(hMemDC, hOld);
 DeleteDC(hMemDC);
}

void MyBitmap::Resize(const Bitmap & bmp, int newWidth, int newHeight)
{
 Release();
 m_dibsec = bmp.m_dibsec;
 m_dibsec.dsBmih.biWidth  = newWidth;
 m_dibsec.dsBmih.biHeight = newHeight;
 void * pBits = NULL;
 m_hBitmap = CreateDIBSection(NULL, (const BITMAPINFO *) & m_dibsec.dsBmih,
 DIB_RGB_COLORS, & pBits, NULL, NULL);
 HDC hMemDC = CreateCompatibleDC(NULL);
 HGDIOBJ hOld = SelectObject(hMemDC, m_hBitmap);
 bmp.Draw(hMemDC, 0, 0, newWidth, newHeight);
 SelectObject(hMemDC, hOld);
 DeleteDC(hMemDC);
 Set();
}

int MyBitmap::GetPixelSize(void) const
{
 return ( m_dibsec.dsBmih.biWidth * m_dibsec.dsBmih.biBitCount + 31 ) / 32 * 4 * m_dibsec.dsBmih.biHeight;
}

BOOL MyBitmap::Save(const TCHAR * szFileName)
{
 HANDLE handle = CreateFile(szFileName, GENERIC_WRITE, FILE_SHARE_READ,  NULL, CREATE_NEW | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 if ( handle == INVALID_HANDLE_VALUE )
  return FALSE;

 BITMAPFILEHEADER bmFH;

 int nHeadSize = sizeof(BITMAPINFOHEADER);

 bmFH.bfType      = 0x4D42;
 bmFH.bfSize      = nHeadSize + GetPixelSize();
 bmFH.bfReserved1 = 0;
 bmFH.bfReserved2 = 0;
 bmFH.bfOffBits   = nHeadSize + sizeof(BITMAPFILEHEADER);

 DWORD dwRead = 0;
 WriteFile(handle, & bmFH, sizeof(bmFH), & dwRead, NULL);

 WriteFile(handle, & m_dibsec.dsBmih,      nHeadSize,   & dwRead, NULL);
 WriteFile(handle, m_dibsec.dsBm.bmBits, GetPixelSize(), & dwRead, NULL);

 CloseHandle(handle);

 return TRUE;
}

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16958714
In above Draw() function StretchBlt is used to stretch bitmap and in Resize() function CreateDIBSection() and Draw() used to construct object for stretched bitmap and then in Save() BMP file is created from stretched bitmap object ....which meets your requirement.

-MAHESH
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question