Solved

Writing StretchBlt to a file

Posted on 2006-06-19
5
382 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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