掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
サムネイル画像の作成方法 (ID:4966)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
// サムネイル画像の作成(縦横比をそこそこ配慮したサンプル) 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などで検索してください。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.