asp.Netピクチャが指定サイズを超えた後に等比例してピクチャを圧縮する方法
///
///
///
///
public string ResizePic()
{
#region
bool IsImgFile = true; //
string filePathName = "123"; // ( )
string fileName = "a.jpg"; //
string fileSysName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + fileName; //
string filePath = ""; //
string strImgPath = "/fileupload/"; //
if (IsImgFile)
{
int maxWidth = 600; //
int maxHeight = 400; //
System.Drawing.Image imgPhoto =
System.Drawing.Image.FromFile(Server.MapPath(strImgPath) + filePathName + "/" + fileSysName);
int imgWidth = imgPhoto.Width;
int imgHeight = imgPhoto.Height;
if (imgWidth > imgHeight) //
{
if (imgWidth > maxWidth) //
{
float toImgWidth = maxWidth; //
float toImgHeight = imgHeight / (float)(imgWidth / toImgWidth); //
System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto,
int.Parse(toImgWidth.ToString()),
int.Parse(toImgHeight.ToString()));
string strResizePicName = Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
img.Save(strResizePicName); //
filePath = strImgPath + filePathName + "/_small_" + fileSysName; //
}
}
else
{
if (imgHeight > maxHeight)
{
float toImgHeight1 = maxHeight;
float toImgWidth1 = imgWidth / (float)(imgHeight / toImgHeight1);
System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto,
int.Parse(toImgWidth1.ToString()),
int.Parse(toImgHeight1.ToString()));
string strResizePicName = Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
img.Save(strResizePicName);
filePath = strImgPath + filePathName + "/_small_" + fileSysName;
}
}
}
return filePath;
#endregion
}