Bmp图形用StretchBlt缩小,图形质量严重损失? (在线等候)(50分)

  • 主题发起人 主题发起人 ebeggar
  • 开始时间 开始时间
E

ebeggar

Unregistered / Unconfirmed
GUEST, unregistred user!
我用StretchBlt(image2.Canvas.Handle, 0, 0, image2.Width, image2.Height, image1.Canvas.Handle, 0, 0,
image1.Picture.Width, image1.Picture.Height, SRCCOPY);
将一bmp图形缩小(如300*420---->120*168),图形变得不可辨认或是血肉模糊,
怎么样才能避免这样呢?(PhotoShop中就不会)
 
大侠多关注啊
 
这段代码可以帮你但是不是最理想的
unit stretch;

interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, ExtCtrls, StdCtrls;

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 := ((bmp.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: PRGB;
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;

end.
 
呵呵,好长!
先谢谢花无泪,再看代码
 
呵呵,搞定了!
方法如下:
每次缩放前加一句
SetStretchBltMode(image2.Canvas.Handle,STRETCH_DELETESCANS);

也还谢谢花无泪!
 
接受答案了.
 
后退
顶部