mrb*_*lah 7 c# asp.net image-processing image-scaling
我想缩放图像,但我不希望图像看起来偏斜.
图像必须为115x115(长x宽).
图像的高度(长度)不能超过115像素,但如果需要,宽度可以小于115但不能超过115.
这很棘手吗?
您需要保留纵横比:
float scale = 0.0;
if (newWidth > maxWidth || newHeight > maxHeight)
{
if (maxWidth/newWidth < maxHeight/newHeight)
{
scale = maxWidth/newWidth;
}
else
{
scale = maxHeight/newHeight;
}
newWidth = newWidth*scale;
newHeight = newHeight*scale;
}
Run Code Online (Sandbox Code Playgroud)
在代码中,最初newWidth/newHeight是图像的宽度/高度.
您想要缩放图像并保留纵横比:
float MaxRatio = MaxWidth / (float) MaxHeight;
float ImgRatio = source.Width / (float) source.Height;
if (source.Width > MaxWidth)
return new Bitmap(source, new Size(MaxWidth, (int) Math.Round(MaxWidth /
ImgRatio, 0)));
if (source.Height > MaxHeight)
return new Bitmap(source, new Size((int) Math.Round(MaxWidth * ImgRatio,
0), MaxHeight));
return source;
Run Code Online (Sandbox Code Playgroud)
应该对您有帮助,如果您对这个想法感兴趣:维基百科关于图像长宽比的文章
| 归档时间: |
|
| 查看次数: |
9051 次 |
| 最近记录: |