【发布时间】:2015-08-08 03:13:34
【问题描述】:
我正在尝试调整图片大小,使其适合 640x640 尺寸并保持纵横比。
例如,如果这是原图:http://i.imgur.com/WEMCSyd.jpg 我想用这种方式调整大小:http://i.imgur.com/K2BalOm.jpg 以保持纵横比(基本上,图像始终在中间,并保持纵横比,其余空间保持白色)
我曾尝试用 C# 编写一个包含以下代码的程序:
Bitmap originalImage, resizedImage;
try
{
using (FileStream fs = new FileStream(textBox1.Text, System.IO.FileMode.Open))
{
originalImage = new Bitmap(fs);
}
int imgHeight = 640;
int imgWidth = 640;
if (originalImage.Height == originalImage.Width)
{
resizedImage = new Bitmap(originalImage, imgHeight, imgWidth);
}
else
{
float aspect = originalImage.Width / (float)originalImage.Height;
int newHeight;
int newWidth;
newWidth = (int)(imgWidth / aspect);
newHeight = (int)(newWidth / aspect);
if (newWidth > imgWidth || newHeight > imgHeight)
{
if (newWidth > newHeight)
{
newWidth = newHeight;
newHeight = (int)(newWidth / aspect);
}
else
{
newHeight = newWidth;
newWidth = (int)(newHeight / aspect);
}
}
resizedImage = new Bitmap(originalImage, newWidth, newHeight);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
但它并没有按我需要的方式工作。
【问题讨论】:
-
@jmelosegui 感谢您的链接,但我真的不懂 jQuery。这就是我尝试用 C# 实现它的原因。
标签: c# image image-resizing