Solved

Writing StretchBlt to a file

Posted on 2006-06-19
5
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 91
Least Squares Curve Fitting 4 133
find Speed using GPS latitude & longitude 4 156
visual C++ 1 39
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 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