サムネイル画像の作成方法


たか  2003-09-23 01:47:56  No: 4962

ある画像のサムネイルを作成し、サーバーへ配置するという
プログラムを作成しています。

いろいろ調べましたが、どうしてもサムネイル画像がうまく
作成できません。

サンプルなどがあればほしいのですが、よろしくお願いいたします。


LupinⅢ  URL  2003-09-23 02:06:17  No: 4963

CanvasクラスのStretchDrawを使用することでサムネイル画像を作成することが出来ます。
StrechDrawの詳細についてはヘルプを参照してください


たか  2003-09-23 03:33:32  No: 4964

確かにStrechDrawを使用すると、小さくはなりますが、
画像そのものが、荒くなってしまいます。

Paintで縮小したような、きれいな画像にはならないでしょうか?


okb168  2003-09-23 04:10:35  No: 4965

画像の縦横比をそろえて縮小すればそこそこの画質になるかと思います。


okb168  2003-09-23 04:58:04  No: 4966

// サムネイル画像の作成(縦横比をそこそこ配慮したサンプル)

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などで検索してください。


にしの  2003-09-23 07:01:55  No: 4967

http://www.g32.org/graphics32/index.html
こちらが参考になるかと思います。


さどやま  URL  2003-10-04 07:57:50  No: 4968

中村拓男さんの次のサイトより画像のきれいな縮小関数"Shrink()"がダウンロードできます。
クォリティーは、Adobeと同等か、ほんのわずか上というのが私の評価です。
その他にも、画像処理の基本的な各種処理関数が備わっています。

http://www.asahi-net.or.jp/~HA3T-NKMR/DGS/DownLoad.htm


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加