 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ben Guest
|
Posted: Wed Oct 04, 2006 5:34 pm Post subject: HELP~ |
|
|
How can I save the content of the given HDC to a ".bmp" file?? (Only use the
Win API) |
|
| Back to top |
|
 |
Lord Crc Guest
|
Posted: Wed Oct 04, 2006 9:59 pm Post subject: Re: HELP~ |
|
|
On Wed, 4 Oct 2006 20:34:49 +0800, "ben" <wtu_ben (AT) 163 (DOT) com> wrote:
| Quote: | How can I save the content of the given HDC to a ".bmp" file?? (Only use the
Win API)
|
Create a DIB, then use BitBlt to copy the contents from the HDC to the
DIB, then write the DIB data to a file with the appropriate headers.
If you can lift your restriciton a bit, you can do it with about 4
lines of code using TBitmap.
- Asbjørn |
|
| Back to top |
|
 |
Peter Below (TeamB) Guest
|
Posted: Wed Oct 04, 2006 11:07 pm Post subject: Re: HELP~ |
|
|
ben wrote:
| Quote: | How can I save the content of the given HDC to a ".bmp" file?? (Only
use the Win API)
|
By writing a lot of code that may turn out not to yield what you expect
it to yield. There are device contexts that are write-only, for example
(for printers). And if you have a HDC for a window on screen that is
covered partially or completely by other windows you will find that
your bitmap shows the screen area as you see it, with the overlapping
windows, not the hidden content of the window you have the HDC for.
Look at GetDeviceCaps, that allows you to figure out the dimensions of
the device and whether it belongs to a printer. Look at
CreateCompatibleBitmap and CreateCompatibleDC, that is what you need to
create a bitmap and an associated HDC you can the use with BitBlt to
copy pixels from the source DC to the bitmap. Then you have the
uneviable task to convert that bitmap to a device-independent one to
write it to file. Study the code for TBitmap.SaveToStream in the
Graphics unit to get a feeling for what is involved. The good old
Win32.hlp file that came with D7 and below also contains example code
(in C) for storing a bitmap. Look for a topic named "Storing an Image".
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be |
|
| Back to top |
|
 |
ben Guest
|
Posted: Thu Oct 05, 2006 8:11 am Post subject: Re: HELP~ |
|
|
The following is my code,but I find it not working properly(cannot save to
the "bmp" file!)
What's wrong? thx~
unit u_SaveClientToBmp;
interface
uses
Windows,
Messages,
Graphics,
SysUtils;
function SaveClientToFile(WinHD: HWND; Width, Height: Integer; szFName:
PChar): Integer;
implementation
function SaveClientToFile(WinHD: HWND; Width, Height: Integer; szFName:
PChar): Integer;
var
WinDC, hMemDC: HDC;
hBits, hFile: THandle;
winBitmap, hTmpBmp: HBITMAP;
hPal: HPALETTE;
lpBits: Pointer;
aRGBQuad: RGBQUAD;
ImgSize, plSize, dwWritten: DWORD;
i, CRes: Integer;
bmFH: BITMAPFILEHEADER;
pBmInfo, pBmInfoMem: PBitmapInfo;
lp: PLogPalette;
testBMP: TBitmap;
begin
hFile := CreateFile(szFName, GENERIC_WRITE, 0, nil, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0);
if hFile = 0 then Result := 1;
//createfile
WinHD := GetForegroundWindow;
WinDC := GetDC(WinHD);
winBitmap := CreateCompatibleBitmap(WinDC, Width, Height);
// hTmpBmp := CreateCompatibleBitmap(WinDC, Width, Height);
pBmInfoMem := PBitmapInfo(GlobalAlloc(GHND, sizeof(BITMAPINFO) + 256 *
sizeof(RGBQUAD)));
pBmInfo := PBitmapInfo(GlobalLock(GlobalAlloc(GHND, sizeof(BITMAPINFO) +
256 * sizeof(RGBQUAD))));
//pBmInfo := GlobalLock(pBmInfoMem);
hTmpBmp:=SelectObject(hMemDC, winBitmap);
BitBlt(hMemDC, 0, 0, Width, Height, WinDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hTmpBmp);
testBMP := TBitmap.Create;
testBMP.Width := Width;
testBMP.Height := Height;
Windows.BitBlt(testBMP.Canvas.Handle, 0, 0, hMemDC, Height, WinDC, 0, 0,
SRCCOPY);
testBMP.SaveToFile('D:\test.bmp');
testBMP.Free;
ZeroMemory(pBmInfo, sizeof(BITMAPINFO));
with pBmInfo^.bmiHeader do
begin
biSize := DWORD(sizeof(BITMAPINFOHEADER));
biWidth := Width;
biHeight := Height;
biPlanes := 1;
biBitCount := WORD(GetDeviceCaps(WinDC, BITSPIXEL));
biCompression := BI_RGB;
GetDIBits(WinDC, winBitmap, 0, Height, nil, pBmInfo^, DIB_RGB_COLORS);
if biSizeImage = 0 then
biSizeImage := Round((((biWidth * biBitCount) + 31) / 32)) * 4 *
biHeight;
//biSizeImage = ((((biWidth * biBitCount) + 31) and ~31) / *
biHeight;
CRes := GetDeviceCaps(WinDC, SIZEPALETTE);
plSize := CRes * sizeof(RGBQUAD);
ImgSize := biSizeImage;
end;
bmFH.bfType := $4D42; //"BM"
bmFH.bfOffBits := plSize + sizeof(BITMAPINFOHEADER) +
sizeof(BITMAPFILEHEADER);
bmFH.bfSize := ImgSize + bmFH.bfOffBits;
bmFH.bfReserved1 := 0;
bmFH.bfReserved2 := 0;
WriteFile(hFile, bmFH, sizeof(BITMAPFILEHEADER), dwWritten, nil);
WriteFile(hFile, pBmInfo^.bmiHeader, sizeof(BITMAPINFOHEADER), dwWritten,
nil);
if (CRes <> 0) then
begin
hPal := HPALETTE(GlobalAlloc(GHND, sizeof(LOGPALETTE) + (CRes *
sizeof(PALETTEENTRY))));
lp := GlobalLock(hPal);
lp^.palNumEntries := WORD(CRes);
lp^.palVersion := $0300;
GetSystemPaletteEntries(WinDC, 0, CRes, lp^.palPalEntry);
aRGBQuad.rgbReserved := 0;
for i := 0 to CRes do
begin
aRGBQuad.rgbRed := lp^.palPalEntry[i].peRed;
aRGBQuad.rgbGreen := lp^.palPalEntry[i].peGreen;
aRGBQuad.rgbBlue := lp^.palPalEntry[i].peBlue;
WriteFile(hFile, aRGBQuad, sizeof(RGBQUAD), dwWritten, nil);
end;
GlobalUnlock(hPal);
GlobalFree(hPal);
end;
hBits := GlobalAlloc(GHND, pBmInfo^.bmiHeader.biSizeImage);
lpBits := GlobalLock(hBits);
GetDIBits(WinDC, winBitmap, 0, Height, lpBits, pBmInfo^, DIB_RGB_COLORS);
WriteFile(hFile, lpBits, ImgSize, dwWritten, nil);
GlobalUnlock(hBits);
GlobalFree(hBits);
GlobalUnlock(Integer(pBmInfo));
GlobalFree(Integer(pBmInfoMem));
DeleteObject(hTmpBmp);
DeleteObject(winBITMAP);
DeleteDC(hMemDC);
ReleaseDC(WinHD, WinDC);
CloseHandle(hFile);
//SetCursor(LoadCursor(NULL, IDC_ARROW));
Result := 0;
end;
end. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|