|
|
|
date: Thu, 12 Jun 2008 17:07:59 -0700 (PDT),
group: microsoft.public.win32.programmer.gdi
back
Show a bitmap on windows screen
My plan was to capture a screen-shot to a bitmap array and then do
some changes on some of the pixels and then put the new bitmap on
screen- everything is in full-screen.
So, I used the GetDIBits and was able to capture the screen-shot and I
was able to save it in a bitmap file to make sure that this part is
working fine.
I decided not to make any changes in the captured bitmap so I don't
mess up anything for right now.
Now that I'm trying to show the captured bitmap on screen using
SetDIBitsToDevice,SetDIBits or StretchDIBits, it doesn't do anything
and I don't even get any error.
I think I'm doing something wrong since I wasn't able to find any good
reference for these functions.
I would really appreciate your comment about my code.
** The code captures a screen-shot, then by writing two lines changes
the screen, so I assume if I display the screen shot it should show
the screen without those two lines since it was captured before
writing those two lines but it still shows those lines!!
Here is the complete code:
#include "iostream"
#include "windows.h"
#include "time.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
using namespace std;
int main()
{
char filename[] = "testScreen01.bmp";
// This function only works for 24-bit (truecolor) display mode
// prepare DCs, bitmaps,..
HDC hScreenDC = GetWindowDC(0);
HDC hmemDC = CreateCompatibleDC(hScreenDC);
int ScreenWidth = GetDeviceCaps(hScreenDC, HORZRES);
int ScreenHeight = GetDeviceCaps(hScreenDC, VERTRES);
HBITMAP hmemBM = CreateCompatibleBitmap(hScreenDC, ScreenWidth,
ScreenHeight);
SelectObject(hmemDC, hmemBM);
// copy screen to memory DC
BitBlt(hmemDC, 0, 0, ScreenWidth, ScreenHeight, hScreenDC, 0, 0,
SRCCOPY);
// allocate and lock memory for the bitmap data
HGLOBAL hpxldata = GlobalAlloc(GMEM_FIXED, ScreenWidth * ScreenHeight
* 3);
void FAR* lpvpxldata = GlobalLock(hpxldata);
// fill .bmp - structures
BITMAPINFO bmInfo;
bmInfo.bmiHeader.biSize = 40;
bmInfo.bmiHeader.biWidth = ScreenWidth;
bmInfo.bmiHeader.biHeight = ScreenHeight;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = 24;
bmInfo.bmiHeader.biCompression = 0;
bmInfo.bmiHeader.biSizeImage = 0;
bmInfo.bmiHeader.biXPelsPerMeter = 0;
bmInfo.bmiHeader.biYPelsPerMeter = 0;
bmInfo.bmiHeader.biClrUsed = 0;
bmInfo.bmiHeader.biClrImportant = 0;
BITMAPFILEHEADER bmFileHeader;
bmFileHeader.bfType = 19778;
bmFileHeader.bfSize = (ScreenWidth * ScreenHeight * 3) + 40 + 14;
bmFileHeader.bfReserved1 = 0;
bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfOffBits = 54;
// copy bitmap data into global memory
GetDIBits(hmemDC, hmemBM, 0, ScreenHeight, lpvpxldata, &bmInfo,
DIB_RGB_COLORS);
cout<<"aaaaaaaaaaaaaaaaaaa"<<endl<<"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"<<endl;
// SetDIBits(hmemDC, hmemBM, 0, ScreenHeight, lpvpxldata, &bmInfo,
DIB_RGB_COLORS);
SetDIBitsToDevice(hmemDC, 0, 0, ScreenWidth, ScreenHeight, 0, 0, 0,
ScreenHeight, lpvpxldata, &bmInfo, DIB_RGB_COLORS);
// StretchDIBits(hmemDC, 0, 0, ScreenWidth, ScreenHeight, 0, 0,
ScreenWidth, ScreenHeight, lpvpxldata, &bmInfo, DIB_RGB_COLORS,
SRCCOPY);
// open file and write data
int bmfile = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
S_IWRITE);
write(bmfile, &bmFileHeader, 14);
write(bmfile, &bmInfo, 40);
write(bmfile, lpvpxldata, ScreenWidth * ScreenHeight * 3);
// clean up
close(bmfile);
GlobalUnlock(hpxldata);
GlobalFree(hpxldata);
DeleteObject(hmemBM);
DeleteDC(hmemDC);
ReleaseDC(0, hScreenDC);
return 0;
}
date: Thu, 12 Jun 2008 17:07:59 -0700 (PDT)
author: navid_ad
Re: Show a bitmap on windows screen
Here is the complete program to capture a screenshot, save it to a
file, make some changes(draw a red line), and show it on screen.
#include "iostream"
#include "windows.h"
#include "time.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
using namespace std;
int main() {
HDC hScreenDC = GetDC(NULL);//GetWindowDC(0);
HDC hmemDC = CreateCompatibleDC(hScreenDC);
int ScreenWidth = GetDeviceCaps(hScreenDC, HORZRES);
int ScreenHeight = GetDeviceCaps(hScreenDC, VERTRES);
BITMAPINFO bmInfo;
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = ScreenWidth;
bmInfo.bmiHeader.biHeight = -ScreenHeight;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = 24;
bmInfo.bmiHeader.biCompression = BI_RGB;
bmInfo.bmiHeader.biSizeImage = 0;
bmInfo.bmiHeader.biXPelsPerMeter = 0;
bmInfo.bmiHeader.biYPelsPerMeter = 0;
bmInfo.bmiHeader.biClrUsed = 0;
bmInfo.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, ScreenWidth,
ScreenHeight);//CreateDIBSection(hScreenDC, &bmInfo, DIB_RGB_COLORS,
0, 0, 0);
BYTE *pbyteBuff = new BYTE[ScreenWidth * ScreenHeight * 3];
HBITMAP oldbits = (HBITMAP)SelectObject(hmemDC, hBitmap);
cout<<"aaaaaaaaa"<<endl;
// copy screen to memory DC
BitBlt(hmemDC, 0, 0, ScreenWidth, ScreenHeight, hScreenDC, 0, 0,
SRCCOPY);
// copy bitmap data into global memory
GetDIBits(hScreenDC, hBitmap, 0, ScreenHeight, pbyteBuff, &bmInfo,
DIB_RGB_COLORS);
for(int i = 0; i < ScreenWidth; i++)
for(int k = 0; k < 20; k++) {
pbyteBuff[((200 + k) * ScreenWidth + i) * 3 + 0] = 0;
pbyteBuff[((200 + k) * ScreenWidth + i) * 3 + 1] = 0;
pbyteBuff[((200 + k) * ScreenWidth + i) * 3 + 2] = 255;
}
cout<<"bbbbbbbbb"<<endl;
char a;
cin>>a;
SetDIBitsToDevice(hScreenDC, 0, 0, ScreenWidth, ScreenHeight, 0, 0,
0, ScreenHeight, pbyteBuff, &bmInfo, DIB_RGB_COLORS);
cin>>a;
char filename[] = "testScreen01.bmp";
BITMAPFILEHEADER bmFileHeader;
bmFileHeader.bfType = 19778;
bmFileHeader.bfSize = (ScreenWidth * ScreenHeight * 3) +
sizeof(BITMAPINFOHEADER) + 14;
bmFileHeader.bfReserved1 = 0;
bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfOffBits = 54;
// open file and write data
int bmfile = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
S_IWRITE);
write(bmfile, &bmFileHeader, 14);
write(bmfile, &bmInfo, 40);
write(bmfile, pbyteBuff, ScreenWidth * ScreenHeight * 3);
// clean up
delete []pbyteBuff;
close(bmfile);
DeleteObject(hBitmap);
DeleteDC(hmemDC);
ReleaseDC(0, hScreenDC);
return 0;
}
date: Sun, 15 Jun 2008 14:55:23 -0700 (PDT)
author: navid_ad
|
|