在16位高彩色下用api实现屏幕截图的问题(100分)

W

wlmx009

Unregistered / Unconfirmed
GUEST, unregistred user!
本人在写一个程序,由于体积要求,要用api实现屏幕截图并保存,但是在16位高彩下截图却出现问题,出来的图严重失真,感觉好像是256色或者16色的效果,并且有的地方颜色明显不对,不知是什么问题,我的代码如下,请指点
procedure jituuser(xl,xr,yl,yr:integer);
var
DC,MemDC:HDC;
BitPerPixel,Width ,Height:integer;
memBitmap,hOldBitmap:HBITMAP;
bitmapbits,bitmapbits16to24:pchar;
bitmapinfo:pbitmapinfo;
bih:BITMAPINFOHEADER;
bfh:BITMAPFILEHEADER;
SysTime: TsystemTime;
userbmp:string;

begin
DC:=GetDC(0);
BitPerPixel:=GetDeviceCaps(dc,BITSPIXEL);
Width:=xr-xl;
Height:=yr-yl;
memdc:= CreateCompatibleDC(DC);
memBitmap:=CreateCompatibleBitmap(DC,Width,Height);
hOldBitmap:=SelectObject(memdc,memBitmap);
BitBlt(MemDC,0,0,Width,Height,DC,xl,yl,SRCCOPY);
getmem(bitmapinfo,sizeof(BITMAPINFOHEADER)+BitPerPixel*sizeof(trgbquad));
bitmapinfo^.bmiHeader.biWidth:=Width;
bitmapinfo^.bmiHeader.biHeight:=Height;

bitmapinfo^.bmiHeader.biPlanes:=1;
bitmapinfo^.bmiHeader.biBitCount:=0;

bitmapinfo^.bmiHeader.biCompression:=BI_RGB;
bitmapinfo^.bmiHeader.biSizeImage:=0;
bitmapinfo^.bmiHeader.biXPelsPerMeter:=0;

bitmapinfo^.bmiHeader.biYPelsPerMeter:=0;

bitmapinfo^.bmiHeader.biClrUsed:=0;

bitmapinfo^.bmiHeader.biClrImportant:=0;
bitmapinfo^.bmiHeader.biSize:=sizeof(tbitmapinfoheader);
GetDIBits(memdc,memBitmap,0,Height,bitmapbits,bitmapinfo^,1);

bih.biBitCount:= bitmapinfo^.bmiHeader.biBitCount;
if bih.biBitCount=16 then
begin
x:=2;

end
else if bih.biBitCount=24 then
x:=3
else if bih.biBitCount=32 then
x:=4;
bih.biCompression:= BI_RGB;

bih.biHeight:= bitmapinfo^.bmiHeader.biHeight;

bih.biWidth:=bitmapinfo^.bmiHeader.biWidth;
bih.biXPelsPerMeter:=bitmapinfo^.bmiHeader.biXPelsPerMeter;
bih.biYPelsPerMeter:=bitmapinfo^.bmiHeader.biYPelsPerMeter;

bih.biPlanes:=1;

bih.biSize:= sizeof(BITMAPINFOHEADER);

bih.biSizeImage:=bitmapinfo^.bmiHeader.biSizeImage;//芞砉杅擂湮苤
bih.biCompression:=BI_RGB;
bih.biClrUsed:=0;
bih.biClrImportant:=0;

bfh.bfType:=0;
bfh.bfSize:=0;
bfh.bfReserved1:=0;
bfh.bfReserved2:=0;
bfh.bfOffBits:=0;
bfh.bfOffBits:= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//善弇芞杅擂腔?痄講
bfh.bfSize:= bfh.bfOffBits + bih.biSizeImage;//恅璃軞腔湮苤

GetSystemTime(SysTime);
userbmp:=inttostr(SysTime.wYear)+inttostr(SysTime.wMonth)+inttostr(SysTime.wDay)+inttostr(SysTime.wHour)+inttostr(SysTime.wMinute)+inttostr(SysTime.wSecond)+inttostr(SysTime.wMilliseconds)+'u.bmp';
//userbmp为生成的bmp文件的文件名

deletefile(pchar(windir+userbmp));
bfh.bfType := $4D42;

writebmp(bfh,sizeof(BITMAPFILEHEADER),windir+userbmp);
//writebmp为我自己的函数,功能为写入文件

writebmp(bih,sizeof(BITMAPINFOHEADER),windir+userbmp);

getmem(bitmapbits,bih.biSizeImage);
GetDIBits(memDC, HBITMAP(memBitmap), 0, Height, bitmapbits,
bitmapinfo^, DIB_RGB_COLORS);//鳳?弇芞杅擂
if bitmapinfo^.bmiHeader.biBitCount=16 then
begin
getmem(bitmapbits16to24,(bih.biSizeImage)div 2 * 3);
end;

writebmp(bitmapbits^,bih.biSizeImage,windir+userbmp);





end;
这代码在24位和32位下都没问题,16位就不行
 
试一下截图后把它绘出来,如正常则是写入文件出问题
 
顶部