0
我想從用戶那裏獲取圖像並將其重新調整爲特定尺寸,但問題是: 我不知道用戶的圖像尺寸,我必須將尺寸重新調整爲特定尺寸,但變形是這裏的負擔。如何將未知尺寸的圖像調整爲特定尺寸而沒有太多變形?
我該如何解決這個問題?有什麼算法嗎? 或有任何源代碼最好在.net? 此致敬意。
我想從用戶那裏獲取圖像並將其重新調整爲特定尺寸,但問題是: 我不知道用戶的圖像尺寸,我必須將尺寸重新調整爲特定尺寸,但變形是這裏的負擔。如何將未知尺寸的圖像調整爲特定尺寸而沒有太多變形?
我該如何解決這個問題?有什麼算法嗎? 或有任何源代碼最好在.net? 此致敬意。
就變形而言,可以使用裁剪和調整大小的組合。您的用戶可以幫助您裁剪。
我發現這個代碼在Code Project用一個簡單的谷歌搜索的.net resize an image
imgPhoto = FixedSize(imgPhotoVert, 300, 300);
imgPhoto.Save(WorkingDirectory +
@"\images\imageresize_3.jpg", ImageFormat.Jpeg);
imgPhoto.Dispose();
....
static Image FixedSize(Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width/(float)sourceWidth);
nPercentH = ((float)Height/(float)sourceHeight);
if(nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Red);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}