コンテナー div 要素と、div 内にネストされた img タグを持つ次のマークアップ コードがあります。コンテナー div には、幅、高さ、上、左の CSS スタイル プロパティがあります。
最初にアップロードされた画像の幅と高さは、コンテナー div よりも大きくても小さくてもかまいません。したがって、最初にアップロードされた画像はサイズ変更して適切にスケーリングし、コンテナー div の境界内に収まるようにサムネイル画像として保存する必要があります。サイズ変更されたサムネイル画像は、次のマークアップでソース (src) として表示されます。
<div id="divContainer" style="width: 600px; height: 450px; top: 50px; left: 20px;">
<img src="[my resized and well scaled thumbnail source]..." id="imgResizedThumnail" />
</div>
別の .NET フォーム ページには、ユーザーがローカル ハードディスクから元の画像をアップロードできるファイル タグがあります。そのアップロードされた画像のサイズを変更し、最適なスケーリングで別のサムネイル画像として保存する必要があります。「最適なスケーリング」とは、サムネイル画像の幅と高さの比率が比例していることを意味し、サムネイルはコンテナー div 内に配置する必要があります。
私の C# .NET メソッドは次のようになります。そのメソッドのコード ロジックについて質問があります。
...
using System.Drawing;
public void SaveThumbnailImageWithbestScaling(Image originalImage, int containerDivWidth, int containerDivHeight)
{
// input containerDivWidth and containerDivHeight are dynamic!
// 1. How can I calculate the scale variable?
double scale = ??? // how can I do the codes here?
// 2. Use that scale to determine the dimension of resized thumbnail image from
// the input originalImage for following variables "thumnailWidth" and "thumnailHeight"
string thumbnailFilename = "myThumnailFileName";
int thumnailWidth = ??? // how can I do the codes here?
int thumnailHeight = ??? // how can I do the codes here?
Bitmap thumnailImage = CreateThumbnail(thumbnailFilename,int thumnailWidth, int thumnailHeight);
// 3. save thumbnail
SaveThumnail(thumnailImage);
}
public void Bitmap CreateThumbnail(string lcFilename,int lnWidth, int lnHeight)
{
...
}
public void thumnailImage (Bitmap thumnail)
{
...
}