ある画像のサムネイルを作成し、サーバーへ配置するという
プログラムを作成しています。
いろいろ調べましたが、どうしてもサムネイル画像がうまく
作成できません。
サンプルなどがあればほしいのですが、よろしくお願いいたします。
CanvasクラスのStretchDrawを使用することでサムネイル画像を作成することが出来ます。
StrechDrawの詳細についてはヘルプを参照してください
確かにStrechDrawを使用すると、小さくはなりますが、
画像そのものが、荒くなってしまいます。
Paintで縮小したような、きれいな画像にはならないでしょうか?
画像の縦横比をそろえて縮小すればそこそこの画質になるかと思います。
// サムネイル画像の作成(縦横比をそこそこ配慮したサンプル)
function AspectResize(hBMP :HBitmap; w,h : Integer;BkColor:TColor):HBitmap;
Var
SrcBitmap,DestBitmap,tmpBitmap :TBitmap;
FRatio :Double;
begin
SrcBitmap :=TBitmap.Create;
DestBitmap :=TBitmap.Create;
tmpBitmap :=TBitmap.Create;
SrcBitmap.Handle := hbmp;
SrcBitmap.PixelFormat :=pf24bit;
Try
// 幅・高さ どちらかが規定サイズより大きい場合
if (SrcBitmap.Width > w) or ( SrcBitmap.Height > h) then
begin
FRatio:=SrcBitmap.Width / SrcBitmap.Height;
if (SrcBitmap.Height<=SrcBitmap.Width) then
begin
tmpBitmap.Assign(SrcBitmap);
SrcBitmap.Width := w;
if Round(w / FRatio)>=h then
SrcBitmap.Height :=h
else
SrcBitmap.Height := Round(w / FRatio);
SrcBitmap.Canvas.StretchDraw(SrcBitmap.canvas.ClipRect,tmpBitmap);
end
else
begin
tmpBitmap.Assign(SrcBitmap);
SrcBitmap.Width := Round(h * FRatio);
SrcBitmap.Height := h;
SrcBitmap.Canvas.StretchDraw(SrcBitmap.canvas.ClipRect,tmpBitmap);
end;
end;
// 画像をセンタリングする (サムネイル画像用)
DestBitmap.PixelFormat :=pf24bit;
SrcBitmap.PixelFormat :=pf24bit;
DestBitmap.Width :=w;
DestBitmap.Height :=h;
DestBitmap.canvas.Brush.color :=BkColor;
DestBitmap.canvas.FillRect(Rect(0,0,w,h));
DestBitmap.canvas.Draw((w-SrcBitmap.Width) div 2,(h-SrcBitmap.Height) div 2,SrcBitmap);
Result :=DestBitmap.ReleaseHandle;
finally
SrcBitmap.Free;
DestBitmap.Free;
tmpBitmap.free;
end;
end;
(*----------------------------------------------------------------------------*)
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
image1.Picture.Bitmap.LoadFromFile(OpenDialog1.filename);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if not image1.picture.bitmap.Empty then
image1.picture.bitmap.Handle:=
AspectResize(image1.picture.bitmap.ReleaseHandle,300,300,clwhite);
end;
(*----------------------------------------------------------------------------*)
// 画質がもっとよいサンプルが欲しい場合は「リサンプリング、resample、resample.pas」などをGoogleなどで検索してください。
http://www.g32.org/graphics32/index.html
こちらが参考になるかと思います。
中村拓男さんの次のサイトより画像のきれいな縮小関数"Shrink()"がダウンロードできます。
クォリティーは、Adobeと同等か、ほんのわずか上というのが私の評価です。
その他にも、画像処理の基本的な各種処理関数が備わっています。
http://www.asahi-net.or.jp/~HA3T-NKMR/DGS/DownLoad.htm
ツイート | ![]() |