楼上的代码只是Resize, 根本做不到不失真。
下面的SmoothResize应该就是你要的。试验过320*200的图像缩小到8*8还能看出原来的样子。
代码改自FastLib, 增加了透明背景的处理(透明色为clNone表示不透明)
type
TRGB = packed record
b, g, r: Byte;
end;
PRGB = ^TRGB;
TDIBBmp = record
hBmp: HBITMAP;
w, h: Integer;
gap, Lbytes: Integer;
Bits: PRGB;
end;
procedure SmoothResize(DestDC: HDC; dx, dy, dw, dh: Integer;
Bmp24: TBitmap; sx, sy, sw, sh: Integer;
TransColor: Cardinal); overload;
procedure SmoothResize(Dest: TDIBBmp; Bmp24: TBitmap; sx, sy, sw, sh: Integer;
TransColor: Cardinal); overload;
procedure TransBlt(destdc: HDC; dx, dy, dw, dh: Integer;
srcdc: HDC; sx, sy, sw, sh: Integer; c: Cardinal);
function NewDIBBmp(var bmp: TDIBBmp): HBITMAP;
implementation
function NewDIBBmp(var bmp: TDIBBmp): HBITMAP;
var
bmInfo: TBitmapInfo;
begin
bmInfo.bmiHeader.biSize:=SizeOf(TBitmapInfoHeader);
bmInfo.bmiHeader.biPlanes:=1;
bmInfo.bmiHeader.biBitCount:=24;
bmInfo.bmiHeader.biCompression:=BI_RGB;
bmInfo.bmiHeader.biWidth:=bmp.w;
bmInfo.bmiHeader.biHeight:=bmp.h;
bmp.hbmp := createDIBSection(0, bminfo, DIB_RGB_COLORS, pointer(bmp.bits), 0, 0);
bmp.lbytes := ((W*24+31)shr 5)shl 2;
bmp.gap := bmp.w mod 4;
result := bmp.hbmp;
end;
procedure SmoothResize(DestDC: HDC; dx, dy, dw, dh: Integer;
Bmp24: TBitmap; sx, sy, sw, sh: Integer;
TransColor: Cardinal); overload;
var
tmpdc: HDC;
tmp: TDIBBmp;
begin
if ((sw=dw) and (sh=dh)) or (dw<2) or (dh<2) or (sw<1) or (sh<1) then
transblt(destdc, dx, dy, dw, dh, bmp24.canvas.handle, sx, sy, sw, sh, transcolor)
else begin
if bmp24.PixelFormat <> pf24Bit then
bmp24.PixelFormat := pf24Bit;
tmp.w := dw;
tmp.h := dh;
newdibbmp(tmp);
tmpdc := createcompatibledc(0);
deleteobject(selectobject(tmpdc, tmp.hbmp));
if integer(TransColor)<>clNone then
bitblt(tmpdc, 0, 0, dw, dh, destdc, dx, dy, SRCCOPY);
smoothResize(tmp, bmp24, sx, sy, sw, sh, transcolor);
bitblt(destdc, dx, dy, dw, dh, tmpdc, 0, 0, SRCCOPY);
deletedc(tmpdc);
deleteobject(tmp.hBmp);
end;
end;
procedure SmoothResize(Dest: TDIBBmp; Bmp24: TBitmap; sx, sy, sw, sh: Integer;
TransColor: Cardinal); overload;
var
srcw: Integer;
x,y,xP,yP,
yP2,xP2: Integer;
Read,Read2: PRGB;
t,z,z2,iz2: Integer;
pc
RGB;
w1,w2,w3,w4: Integer;
Col1,Col2,
Col3, Col4: PRGB;
begin
srcw := ((bmp24.width*24+31)shr 5)shl 2;
xP2:=((sw-1)shl 15)div dest.w;
yP2:=((sh-1)shl 15)div dest.h;
yP:=0;
pc := dest.bits;
for y:=0 to dest.h-1 do
begin
xP:=0;
Read:=pointer(bmp24.scanline[Bmp24.Height-1-yp shr 15 - sy]);
if yP shr 16<sh-1 then
Read2:=pointer(integer(read)+srcw)
else
Read2:=read;
z2:=yP and $7FFF;
iz2:=$8000-z2;
for x:=0 to dest.w-1 do
begin
t:=(xP shr 15)+sx;
Col1:=pointer(integer(read)+t*3);
if xp shr 15 < sw-1 then
col3 := pointer(integer(col1) +3)
else
col3 := col1;
if (integer(transcolor) <> clNone) and
(col1^.r=PRGB(@TransColor)^.b) and
(col1^.g=PRGB(@transcolor)^.g) and
(col1^.b=PRGB(@transcolor)^.r) then
col1 := pc;
if (integer(transcolor) <> clNone) and
(col3^.r=PRGB(@transcolor)^.b) and
(col3^.g=PRGB(@transcolor)^.g) and
(col3^.b=PRGB(@transcolor)^.r) then
col3 := pc;
Col2:=pointer(integer(read2)+t*3);
col4 := pointer(integer(col2)+3);
if (integer(transcolor) <> clNone) and
(col2^.r=PRGB(@transcolor)^.b) and
(col2^.g=PRGB(@transcolor)^.g) and
(col2^.b=PRGB(@transcolor)^.r) then
col2 := pc;
if (integer(transcolor) <> clNone) and
(col4^.r=PRGB(@transcolor)^.b) and
(col4^.g=PRGB(@transcolor)^.g) and
(col4^.b=PRGB(@transcolor)^.r) then
col4 := pc;
z:=xP and $7FFF;
w2:=(z*iz2)shr 15;
w1:=iz2-w2;
w4:=(z*z2)shr 15;
w3:=z2-w4;
pc.b:=
(Col1^.b*w1+col3^.b*w2+
Col2^.b*w3+col4^.b*w4)shr 15;
pc.g:=
(Col1^.g*w1+col3^.g*w2+
Col2^.g*w3+col4^.g*w4)shr 15;
pc.r:=
(Col1^.r*w1+col3^.r*w2+
Col2^.r*w3+col4^.r*w4)shr 15;
Inc(pc);
Inc(xP,xP2);
end;
Inc(yP,yP2);
pc := pointer(integer(pc)+dest.gap);
end;
end;
function IsW2K: Boolean;
begin
result := (win32platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion >=5);
end;
function IsW98: Boolean;
begin
result := (win32platform = VER_PLATFORM_WIN32_WINDOWS) and (
(Win32MajorVersion > 4) or (
(Win32MajorVersion=4) and (Win32MinorVersion>0)
));
end;
procedure TransBlt(destdc: HDC; dx, dy, dw, dh: Integer;
srcdc: HDC; sx, sy, sw, sh: Integer; c: Cardinal);
var
monodc: HDC;
monobmp: HBITMAP;
old: THandle;
begin
if isw2k or isw98 then
transparentblt(destdc, dx, dy, dw, dh, srcdc, sx, sy, sw, sh, c)
else begin
monodc := createcompatibledc(0);
monobmp := createbitmap(sw, sh, 1, 1, nil);
old := selectobject(monodc, monobmp);
setbkcolor(srcdc, c);
bitblt(monodc, 0, 0, sw, sh, srcdc, sx, sy, SRCCOPY);
transparentstretchblt(destdc, dx, dy, dw, dh, srcdc, sx, sy, sw, sh, monodc, 0, 0);
selectobject(monodc, old);
deleteobject(monobmp);
deletedc(monodc);
end;
end;
2分少了点