将图片的大小另存成自定义的大小!急!!!谢谢 ( 积分: 100 )

  • 主题发起人 主题发起人 zglwxb
  • 开始时间 开始时间
Z

zglwxb

Unregistered / Unconfirmed
GUEST, unregistred user!
(1)不管Image1 载入的图片的大小是多少,都将压缩或拉伸成 宽 310 ,高 234 的大小并保存成 JPG 格式的文件。
(2)能不能不管Image1载入的是什么格式的图片,只要能显示,都能做到以上的要求。
谢谢!
 
可不可以先把Image1固定成310*234,属性strech设置成true?没做过.不知道可行否?
 
现在已经能显示出来了,就是保存不下来,保存下来的文件并不是显示的大小。

Image1.Picture.LoadFromFile(extractfilepath(application.ExeName)+'temp/tempbmw.jpg');

Image1.Stretch := not Image1.Stretch;
if Image1.Stretch then
Image1.Align := alClient
else
begin
Image1.Align := alNone;
Image1.Height := Image1.Picture.Height;
Image1.Width := Image1.Picture.Width;
end;
 
保存的代码也帖上来,我也试试.
 
保存的代码也帖上来,我也试试.
 
刚才吃东西去了,保存的代码在下面
其实已经实现的我的要求,但是就是 保存的图片 颜色很难看 失真要紧。

var
jpg :TJpegImage;
bmp,tmpbmp :TBitmap;
begin
jpg :=TJpegImage.Create;
jpg.LoadFromFile(extractfilepath(application.ExeName)+'temp/tempbmw.jpg');
bmp :=TBitmap.Create;
bmp.Assign(jpg);
tmpbmp :=TBitmap.Create;
tmpbmp.Width :=bmp.Width div 2;
tmpbmp.Height :=bmp.Height div 2;
StretchBlt(tmpbmp.Canvas.Handle,0,0,tmpbmp.Width,tmpbmp.Height,
bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,SRCCOPY);
jpg.Assign(tmpbmp);
jpg.CompressionQuality := 100;//已经调到100了还是不行,郁闷
jpg.Compress;
jpg.SaveToFile(extractfilepath(application.ExeName)+'temp/bmw.jpg');
jpg.Free;
bmp.Free;
tmpbmp.Free;
end;
 
没人知道吗???
 
偶刚才也出去了.呵,我也来玩玩.
 
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Image1.Picture.LoadFromFile(extractfilepath(application.ExeName)+'tempbmw.bmp');
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
Abitmap :TBitmap;
begin
Abitmap := TBitmap.Create;
try
Abitmap.Height:= Panel1.Height;
Abitmap.Width:= Panel1.Width;
Panel1.PaintTo(Abitmap.Canvas,0,0);
Abitmap.SaveToFile(extractfilepath(application.ExeName)+'tempbmw1.bmp');
finally
Abitmap.Free;
end;
end;


偶这样做的,可以的了.
 
这个我知道
我现在要的是全部转成 JPG 格式
现在是可以转了
但是就是 失真 图象颜色很难看
 
加下面的内容试试看
if Jpg.PixelFormat = jf24bit then
bmp.PixelFormat := pf24bit //指定格式
else
bmp.PixelFormat := pf8bit;
 
使用Canvas.stretchdraw或StretchBlt函数,图像压缩后总是一锯齿,图像不好看,需要经过柔化处理。以下代码实现柔化压缩:

procedure RHStretchDraw(DestRect : TRect;Canvas : TCanvas; SourceBmp : TBitmap);
var
i,j, k: Integer;
SourceBuf,DestBufffer : pointer;
W, W3, H, WS3:DWord;
Y,X1,X2,X3,X4,X5,X6,X7,X8,X9 : PByte;
C, B : DWord;
Zx, Zy : Single;
Bmp : TBitmap;
ARect : TRect;
BmpInfo11 : BITMAPINFO;
begin

ARect := DestRect;
W := ARect.Right - ARect.Left;
H := ARect.Bottom - ARect.Top;
if (W = 0) or (H = 0 ) then exit;

Zx := SourceBmp.Width / W; //x方向的压缩比例
Zy := SourceBmp.Height / H; //y方向的压缩比例
if (Zx = 1) and (Zy = 1) then
begin
Canvas.Draw(DestRect.Left,DestRect.Top, SourceBmp) ;
exit;
end;
if (Zx > 1) and (Zy > 1 ) then //图像缩小
begin
{ if (Zx < 1.5) and (Zy < 1.5 ) then //图像缩小
begin
Canvas.StretchDraw(Rect(0,0,W,H),SourceBmp); ;
exit;
end; }
//W := (W + 3) div 4 * 4;
W3 := (W * 3+3) div 4 * 4;
WS3 := (SourceBmp.Width* 3+3) div 4 * 4;
SourceBmp.PixelFormat := pf24bit;
try
Getmem(SourceBuf,(WS3+12) * SourceBmp.Height );
except
exit;
end;

GetBitmapBits(SourceBmp.Handle, WS3 * SourceBmp.Height , SourceBuf);
C := W3 * H ;
try
Getmem(DestBufffer, C+1000);
except
exit;
end;
//Copymemory(DestBufffer, SourceBuf, C);
try
for j:=0 to H - 1 do
begin
for i:=0 to W - 1 do
begin
for k := 0 to 2 do //Blue, Green, Red
begin
Y := PByte(DWORD(DestBufffer)+ (j) * W3 + i * 3 + K); //H-j-1
//四个相邻点
B := DWORD(SourceBuf)+ trunc(j * Zy) * WS3+ trunc(i * Zx) * 3 + K;
if (j = 0) or (i = 0) then // (Zx < 2.5)or
begin
X1 := PByte(B);
X2 := PByte(B+WS3);
X3 := PByte(B+3);
X4 := PByte(B+WS3+3);
Y^ := (X1^ + X2^ + X3^ + X4^ ) div 4;
end else
begin
//9个相邻点取平均值
X1 := PByte(B - WS3 - 3);
X2 := PByte(B - WS3);
X3 := PByte(B - WS3 + 3);
X4 := PByte(B - 3);
X5 := PByte(B);
X6 := PByte(B + 3);
X7 := PByte(B + WS3 - 3);
X8 := PByte(B + WS3);
X9 := PByte(B + WS3 + 3);
Y^ := (X1^ + X2^ + X3^ + X4^ +X5^ * 5 + X6^ + X7^ + X8^ + X9^) div 13;
//Y^ := ( X1^ + X3^ + X5^*2 + X7^ + X9^ ) div 6;
//Y^ := X5^ ;
end;
end;
end;
end;
BmpInfo11.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
BmpInfo11.bmiHeader.biCompression := BI_RGB;
BmpInfo11.bmiHeader.biPlanes := 1;
BmpInfo11.bmiHeader.biBitCount := 24;
BmpInfo11.bmiHeader.biWidth := W;
BmpInfo11.bmiHeader.biHeight := H;
BmpInfo11.bmiHeader.biSizeImage := BmpInfo11.bmiHeader.biWidth*BmpInfo11.bmiHeader.biHeight*BmpInfo11.bmiHeader.biPlanes*(BmpInfo11.bmiHeader.biBitCount div 8);
BmpInfo11.bmiHeader.biXPelsPerMeter := 0;
BmpInfo11.bmiHeader.biYPelsPerMeter := 0;
BmpInfo11.bmiHeader.biClrUsed := 0;
BmpInfo11.bmiHeader.biClrImportant := 0;

StretchDIBits(Canvas.Handle, 0, 0, W, H, 0, 0, W, H,
DestBufffer, BmpInfo11 , DIB_RGB_COLORS, SRCCOPY);
finally
freemem(SourceBuf);
freemem(DestBufffer);
end;
end else //图像扩大
begin
W := (W + 3) div 4 * 4;
Bmp := TBitmap.Create;
bmp.PixelFormat := pf24bit;
bmp.Width:=W;
bmp.Height:=H;
bmp.Canvas.StretchDraw(Rect(0,0,W,H),SourceBmp);

C := (W * 3+3) div 4 * 4* H;
try
Getmem(SourceBuf, C * 2);
except
exit;
end;
try
GetBitmapBits(Bmp.Handle, C, SourceBuf);

DestBufffer :=Pointer( DWORD(SourceBuf)+C);

Copymemory(DestBufffer, SourceBuf, C);


W3 := (W * 3+3)div 4*4;
for j:=1 to H - 2 do
begin
for i:=1 to W - 2 do
begin
for k := 0 to 2 do //Blue, Green, Red
begin
Y := PByte(DWORD(DestBufffer)+ j * W3 + i * 3 + K);
B := DWORD(SourceBuf)+ j * W3 + i * 3 + K;
X1 := PByte(B - W3 - 3);
X2 := PByte(B - W3);
X3 := PByte(B - W3 + 3);
X4 := PByte(B - 3);
X5 := PByte(B);
X6 := PByte(B + 3);
X7 := PByte(B + W3 - 3);
X8 := PByte(B + W3);
X9 := PByte(B + W3 + 3);
Y^ := (X1^ + X2^ + X3^ + X4^ + X5^ * 2 + X6^ + X7^ + X8^ + X9^) div 10;
end;
end;
end;
SetBitmapBits(Bmp.Handle, C, DestBufffer);
Canvas.Draw(0,0,Bmp);
Bmp.Free;
finally
freemem(SourceBuf);
end;
end;

end;

procedure ChangeImageSize(SourceImage:TImage;NewWidth,NewHeight:integer;DestBmp:TBitmap );
var SourceBmp:TBitmap ;
begin
SourceBmp:=TBitmap.Create;
if DestBmp=nil then
DestBmp:=TBitmap.Create;
try
SourceBmp.Width := SourceImage.Picture.Width;
SourceBmp.height := SourceImage.Picture.height;
DestBmp.Width := NewWidth;
DestBmp.Height := NewHeight;
SourceBmp.Canvas.Draw(0,0,SourceImage.Picture.Graphic);
RHStretchDraw(SourceImage.ClientRect,DestBmp.Canvas, SourceBmp);
finally
SourceBmp.Free;
end;
end;

procedure TForm1.Button1Click(Sender:TObject);
var jpg:TJpegImage;
DestBmp:TBitmap;
begin
DestBmp := TBitmap.Create;
jpg:=TJpegImage.Create;
try
ChangeImageSize(Image1,310,234,DestBmp);
jpg.Assign(DestBmp);
jpg.SaveToFile('a.jpg');
finally
jpg.free;
DestBmp.Free;
end;
end;
 
^_^ 需要包含JPEG单元哦。 uses jpeg;
 
其实抗锯齿挺简单的,调一个API函数就搞定了,无需做的那个复杂.

int SetStretchBltMode(

HDC hdc, // handle of device context
int iStretchMode // bitmap stretching mode
);
 
to liaj:
我测试了你的方法,图片缩小如果使用 STRETCH_HALFTONE 参数效果还可以,但放大却没有效果。以下是测试代码:

在form上放3个image控件:Image1,Image2,Image3。Image1尺寸比较大,Image2中等,Image3最小。在Image2里预先加载一个位图。
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
SetStretchBltMode(Image1.Canvas.Handle, STRETCH_HALFTONE );
StretchBlt(Image1.Canvas.Handle,0,0,Image1.Width,Image1.Height,Image2.Canvas.Handle,0,0,Image2.Width,Image2.Height,SRCCOPY);
SetStretchBltMode(Image3.Canvas.Handle, STRETCH_HALFTONE );
StretchBlt(Image3.Canvas.Handle,0,0,Image3.Width,Image3.Height,Image2.Canvas.Handle,0,0,Image2.Width,Image2.Height,SRCCOPY);
end;
 

Similar threads

D
回复
0
查看
791
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
826
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部