我有以下标记代码,它具有一个容器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)
{
...
}