我有以下標記代碼,它具有一個容器div元素和嵌套在div內的img標記。容器div具有寬度,高度,頂部和左側CSS樣式屬性。C#.NET:調整容器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表單頁面,有一個文件標籤讓用戶從本地硬盤上傳的原始圖像。上傳的圖像需要調整大小並保存爲具有最佳縮放比例的另一個縮略圖。 「最佳縮放」意味着縮略圖圖像具有寬度和高度成比例的比例,並且縮略圖必須位於容器格內。
我的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)
{
...
}