用户选择上传成为头像的图片大都不是规格的正方形,怎么将它压缩成规定大小的正方形,而保证图片不变形呢?不如说一张图片原来大小是高234px,宽456px的,要压缩成为高50px,宽50px的头像。
我当时考虑得先将原来的宽度截取为234px,超出的部分不裁掉。问题就是不知道该怎么裁?向大家请教一下。这面是将图片进行压缩的代码。
参数:filename是压缩后生成的文件名;attachment是一个附件类;settings是图片的设置信息。//////////////////////////////////////////////////////////////////////////////////////
public static void HandllingPicture(string filename, Attachment attachment, ImageSettings settings)
{
Attachment pictureData = attachment; if((pictureData.Content == null) || (pictureData.Content.Length == 0))
{
return;
} MemoryStream stream = new MemoryStream();
stream.Write(pictureData.Content, 0, pictureData.Content.Length);
stream.Position = 0;
Bitmap image = (Bitmap)Bitmap.FromStream(stream, true);
stream.Close(); Graphics graph;
Bitmap bitmap = new Bitmap(settings.Width, settings.Height);
graph = Graphics.FromImage(bitmap);
graph.InterpolationMode = InterpolationMode.HighQualityBicubic; if(settings.Brightness != -1)
{
float brightness = (settings.Brightness - 50) / 100.0f;
ImageAttributes attrs = new ImageAttributes();
ColorMatrix colorMatrix = new ColorMatrix(new float[][] {
new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },
new float[] { brightness, brightness, brightness, 0.0f, 1.0f }
} );
attrs.SetColorMatrix(colorMatrix); Bitmap bitmapB = new Bitmap(settings.Width, settings.Height);
Graphics graphB = Graphics.FromImage(bitmapB);
graphB.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphB.DrawImage(image, 0, 0, settings.Width, settings.Height);
graph.DrawImage(bitmapB, new Rectangle(0, 0, bitmapB.Width, bitmapB.Height), 0, 0, bitmapB.Width, bitmapB.Height, GraphicsUnit.Pixel, attrs);
graphB.Dispose();
bitmapB.Dispose();
}
else
{
graph.DrawImage(image, 0, 0, settings.Width, settings.Height);
} ImageCodecInfo codec = GetEncoderInfo("image/jpeg"); EncoderParameters eps = new EncoderParameters(1);
eps = new EncoderParameters();
eps.Param[0] = new EncoderParameter(Encoder.Quality, (long)settings.Quality); try { bitmap.Save(filename, codec, eps); }
catch { (new E5haException(E5haExceptionType.AccessDenied, "Permission to save cache file denied (" + filename + ")")).Log(); } bitmap.Dispose();
graph.Dispose();
eps.Dispose();
image.Dispose();
}////////////////////////////////////////////////////////////////////////////////
类ImageSettings
public class ImageSettings
{
public int Width;
public int Height;
public int Quality;
public int Brightness;
public bool MaintainRatio;
public bool OriginalSize;
public bool UpScale; public ImageSettings()
{
MaintainRatio = true;
OriginalSize = false;
UpScale = false;
Brightness = -1;
} public ImageSettings(int width, int height, int quality, bool maintainRatio, bool originalSize, bool upScale)
{
Width = width;
Height = height;
Quality = quality;
MaintainRatio = maintainRatio;
OriginalSize = originalSize;
UpScale = upScale;
Brightness = -1;
} public ImageSettings(int width, int height, int quality, bool maintainRatio) : this()
{
Width = width;
Height = height;
Quality = quality;
MaintainRatio = maintainRatio;
}
}///////////////////////////////////////////////////////////////////////
类Attachment
public class Attachment
{
int length = 0;
int userID = 0;
int height = 0;
int width = 0;
DateTime created;
string contentType;
string fileName;
byte[] content = new byte[0]; Guid attachmentID = Guid.Empty;  
int downloadCount = 0;
string realFileName; // 保存在磁盘中的文件名 bool isPicture = false; // 附件是否是图片 public Attachment()
{} public Attachment(HttpPostedFile postedFile, int userID)
{
this.userID = userID;
DoAttachment(postedFile);
} public Attachment (HttpPostedFile postedFile) 
{
DoAttachment(postedFile);  
} private void DoAttachment(HttpPostedFile postedFile)
{
SiteSettings settings = E5haContext.Current.SiteSettings;
Stream stream = postedFile.InputStream;

int length = postedFile.ContentLength;
string contentType = postedFile.ContentType; fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("\\") + 1);

bool water = settings.AttachmentWater && 
contentType.ToLower().StartsWith("image/") && 
contentType.ToLower() != "image/gif"; DoAttachment(stream, length, contentType, fileName,water);
} public void DoAttachment (Stream stream, int length, string contentType, string fileName, bool water)
{
SiteSettings settings = E5haContext.Current.SiteSettings;

this.length = length;
this.contentType = contentType;
this.fileName = fileName;

attachmentID = Guid.NewGuid();

realFileName =  System.Guid.NewGuid().ToString() + ".jpg"; // 将文件改名存储,扩展名改为.jpg
stream.Position = 0; 
this.length = Convert.ToInt32(stream.Length);
content = new Byte[stream.Length];
stream.Read(content, 0, this.length);
} /// <summary>
/// 扩展名
/// </summary>
public string Extension
{
get 
{
return Path.GetExtension(fileName);
}
} /// <summary>
/// 唯一标志附件的ID
/// </summary>
public Guid AttachmentID
{
get 
{
return attachmentID;
}
set 
{
attachmentID = value;
}
} public bool IsPicture
{
get
{
return isPicture;
}
set
{
isPicture = value;
}
} public int Height
{
get
{
return height;
}
set
{
height = value;
}
} public int Width
{
get
{
return width;
}
set
{
width = value;
}
} public int Length 
{
get 
{
return length;
}
set 
{
length = value;
}
} public string ContentType 
{
get 
{
return contentType;
}
set 
{
contentType = value;
}
} public string FileName 
{
get 
{
return fileName;
}
set 
{
fileName = value;
} } /// <summary>
/// 保存在磁盘中的文件名
/// </summary>
public string RealFileName
{
get 
{
return realFileName;
}
set 
{
realFileName = value;
}
} public Byte[] Content 
{
get 
{
return content;
}
set 
{
content = value;
}
} public int UserID 
{
get { return userID; }
set { userID = value; }
} public int DownloadCount
{
get { return downloadCount;}
set { downloadCount = value;}
} public DateTime DateCreated 
{
get 
{
return created;
}
set 
{
created = value;
}
} }