Solved

Writing StretchBlt to a file

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now