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"
-
六六分期app的软件客服如何联系?不知道吗?加qq群【895510560】即可!标题:六六分期
阅读:19143|2023-10-27
-
今天小编告诉大家如何处理win10系统火狐flash插件总是崩溃的问题,可能很多用户都不知
阅读:9973|2022-11-06
-
今天小编告诉大家如何对win10系统删除桌面回收站图标进行设置,可能很多用户都不知道
阅读:8317|2022-11-06
-
今天小编告诉大家如何对win10系统电脑设置节能降温的设置方法,想必大家都遇到过需要
阅读:8686|2022-11-06
-
我们在使用xp系统的过程中,经常需要对xp系统无线网络安装向导设置进行设置,可能很多
阅读:8627|2022-11-06
-
今天小编告诉大家如何处理win7系统玩cf老是与主机连接不稳定的问题,可能很多用户都不
阅读:9643|2022-11-06
-
电脑对日常生活的重要性小编就不多说了,可是一旦碰到win7系统设置cf烟雾头的问题,很
阅读:8611|2022-11-06
-
我们在日常使用电脑的时候,有的小伙伴们可能在打开应用的时候会遇见提示应用程序无法
阅读:7991|2022-11-06
-
今天小编告诉大家如何对win7系统打开vcf文件进行设置,可能很多用户都不知道怎么对win
阅读:8642|2022-11-06
-
今天小编告诉大家如何对win10系统s4开启USB调试模式进行设置,可能很多用户都不知道怎
阅读:7527|2022-11-06
|
|
请发表评论