缩列图 ( 积分: 100 )

  • 主题发起人 主题发起人 framecai
  • 开始时间 开始时间
F

framecai

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾:
有没用delphi编写的图像的缩列图代码。
 
满街都是
 
unit JpegConv;

interface

uses Windows, Graphics, SysUtils, Classes;

procedure CreateThumbnail(InStream, OutStream: TStream;
Width, Height: Integer; FillColor: TColor=clWhite); overload;
procedure CreateThumbnail(const InFileName, OutFileName: string;
Width, Height: Integer; FillColor: TColor=clWhite); overload;

implementation

uses Jpeg;

procedure CreateThumbnail(InStream, OutStream: TStream;
Width, Height: Integer; FillColor: TColor=clWhite);
var
JpegImage: TJpegImage;
Bitmap: TBitmap;
Ratio: Double;
ARect: TRect;
AHeight, AHeightOffset: Integer;
AWidth, AWidthOffset: Integer;
begin
// Check for invalid parameters
if Width<1 then
raise Exception.Create('Invalid Width');
if Height<1 then
raise Exception.Create('Invalid Height');
JpegImage:=TJpegImage.Create;
try
// Load the image
JpegImage.LoadFromStream(InStream);
// Create bitmap, and calculate parameters
Bitmap:=TBitmap.Create;
try
Ratio:=JpegImage.Width/JpegImage.Height;
if Ratio>1 then
begin
AHeight:=Round(Width/Ratio);
AHeightOffset:=(Height-AHeight) div 2;
AWidth:=Width;
AWidthOffset:=0;
end
else
begin
AWidth:=Round(Height*Ratio);
AWidthOffset:=(Width-AWidth) div 2;
AHeight:=Height;
AHeightOffset:=0;
end;
Bitmap.Width:=Width;
Bitmap.Height:=Height;
Bitmap.Canvas.Brush.Color:=FillColor;
Bitmap.Canvas.FillRect(Rect(0,0,Width,Height));
// StretchDraw original image
ARect:=Rect(AWidthOffset,AHeightOffset,AWidth+AWidthOffset,AHeight+AHeightOffset);
Bitmap.Canvas.StretchDraw(ARect,JpegImage);
// Assign back to the Jpeg, and save to the file
JpegImage.Assign(Bitmap);
JpegImage.SaveToStream(OutStream);
finally
Bitmap.Free;
end;
finally
JpegImage.Free;
end;
end;

procedure CreateThumbnail(const InFileName, OutFileName: string;
Width, Height: Integer; FillColor: TColor=clWhite); overload;
var
InStream, OutStream: TFileStream;
begin
InStream:=TFileStream.Create(InFileName,fmOpenRead);
try
OutStream:=TFileStream.Create(OutFileName,fmOpenWrite or fmCreate);
try
CreateThumbnail(InStream,OutStream,Width,Height,FillColor);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
end;

end.
 
apolloone,是用Delphi的TImage显示的吗?
 
apolloone,有没现成的例子,这些代码看得不明白,我想要得是显示JPG图片的缩列图的功能,
 
将这两个过程放到你的软件中即可调用,并生成缩列图
procedure CreateThumbnail(InStream, OutStream: TStream;
Width, Height: Integer; FillColor: TColor=clWhite); overload;
procedure CreateThumbnail(const InFileName, OutFileName: string;
Width, Height: Integer; FillColor: TColor=clWhite); overload;
 
本公司诚聘软件工程师数名,要求大专以上学历,计算机专业,熟练掌握DELPHI+SQL编程,能开发C/S 或B/S结构的应用软件,二年以上工作经验,有在软件公司工作经验优先考虑。有意者请联系QQ419682853邮箱:ljy42af@gdou.com
 
后退
顶部