using System.Collections.Generic; |
using System.Drawing.Drawing2D; |
using System.Drawing.Imaging; |
/// 以图片中心为轴心,截取正方型,然后等比缩放 |
/// <remarks>吴剑 2010-11-23</remarks> |
/// <param name="postedFile">原图HttpPostedFile对象</param> |
/// <param name="fileSaveUrl">缩略图存放地址</param> |
/// <param name="side">指定的边长(正方型)</param> |
/// <param name="quality">质量(范围0-100)</param> |
public static void CutForSquare(System.Web.HttpPostedFile postedFile, string fileSaveUrl, int side, int quality) |
string dir = Path.GetDirectoryName(fileSaveUrl); |
if (!Directory.Exists(dir)) |
Directory.CreateDirectory(dir); |
System.Drawing.Image initImage = System.Drawing.Image.FromStream(postedFile.InputStream, true ); |
if (initImage.Width <= side && initImage.Height <= side) |
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); |
int initWidth = initImage.Width; |
int initHeight = initImage.Height; |
if (initWidth != initHeight) |
System.Drawing.Image pickedImage = null ; |
System.Drawing.Graphics pickedG = null ; |
if (initWidth > initHeight) |
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight); |
pickedG = System.Drawing.Graphics.FromImage(pickedImage); |
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight); |
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight); |
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); |
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth); |
pickedG = System.Drawing.Graphics.FromImage(pickedImage); |
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth); |
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth); |
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); |
initImage = (System.Drawing.Image)pickedImage.Clone(); |
System.Drawing.Image resultImage = new System.Drawing.Bitmap(side, side); |
System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage); |
resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
resultG.Clear(Color.White); |
resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel); |
ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders(); |
ImageCodecInfo ici = null ; |
foreach (ImageCodecInfo i in icis) |
if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif" ) |
EncoderParameters ep = new EncoderParameters(1); |
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ( long )quality); |
resultImage.Save(fileSaveUrl, ici, ep); |
/// 以图片中心为轴心,截取正方型,然后等比缩放 |
/// <remarks>吴剑 2010-11-23</remarks> |
/// <param name="postedFile">原图HttpPostedFile对象</param> |
/// <param name="fileSaveUrl">缩略图存放地址</param> |
/// <param name="side">指定的边长(正方型)</param> |
/// <param name="quality">质量(范围0-100)</param> |
public static void CutForSquare(System.IO.Stream fromFile, string fileSaveUrl, int side, int quality) |
string dir = Path.GetDirectoryName(fileSaveUrl); |
if (!Directory.Exists(dir)) |
Directory.CreateDirectory(dir); |
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true ); |
if (initImage.Width <= side && initImage.Height <= side) |
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); |
int initWidth = initImage.Width; |
int initHeight = initImage.Height; |
if (initWidth != initHeight) |
System.Drawing.Image pickedImage = null ; |
System.Drawing.Graphics pickedG = null ; |
if (initWidth > initHeight) |
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight); |
pickedG = System.Drawing.Graphics.FromImage(pickedImage); |
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight); |
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight); |
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); |
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth); |
pickedG = System.Drawing.Graphics.FromImage(pickedImage); |
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth); |
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth); |
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); |
initImage = (System.Drawing.Image)pickedImage.Clone(); |
System.Drawing.Image resultImage = new System.Drawing.Bitmap(side, side); |
System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage); |
resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
resultG.Clear(Color.White); |
resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel); |
ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders(); |
ImageCodecInfo ici = null ; |
foreach (ImageCodecInfo i in icis) |
if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif"
|
|
请发表评论