在.NET中生成缩略图的最快、最可靠的方法是什么?我需要得到任何图像,压缩它在JPEG和调整它的大小。
我看过几个GDI+的例子,一些非免费的组件,我记得WPF有一些关于映像的好东西。GDI+已经很旧了,它在服务器环境中可能没有任何好处。
这必须在完全信任的ASP.NET MVC应用程序中工作,如果可能的话,还要同步运行。
你有什么推荐的?
更新:
基于Mantorok's answer,我已经解决了这个例子,但它仍然是GDI+,如果我尝试处理大图,它就会崩溃:
public void GenerateThumbnail(String filename, Int32? desiredWidth,
Int32? desiredHeight, Int64 quality, Stream s)
{
using (Image image = Image.FromFile(filename))
{
Int32 width=0, height=0;
if ((!desiredHeight.HasValue && !desiredWidth.HasValue) ||
(desiredHeight.HasValue && desiredWidth.HasValue))
throw new ArgumentException(
"You have to specify a desired width OR a desired height");
if (desiredHeight.HasValue)
{
width = (desiredHeight.Value * image.Width) / image.Height;
height = desiredHeight.Value;
}
else
{
height = (desiredWidth.Value * image.Height) / image.Width;
width = desiredWidth.Value;
}
using (var newImage = new Bitmap(width, height))
using (var graphics = Graphics.FromImage(newImage))
using (EncoderParameter qualityParam =
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
quality))
using (EncoderParameters encoderParams = new EncoderParameters(1))
{
graphics.DrawImage(image, 0, 0, width, height);
ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders().
Single(e => e.MimeType.Equals("image/jpeg",
StringComparison.Ordinal));
encoderParams.Param[0] = qualityParam;
newImage.Save(s, jpegCodec, encoderParams);
}
}
}
发布于 2011-05-05 03:38:54
对于密集的服务器端代码,我建议您使用GDI+以外的其他技术,这些技术不是为逐个块处理图像而设计的(以流方式)。
您可以使用Windows Imaging Component 或WPF来执行此任务。这里有一个很好的例子,说明了如何以一种快速的、更重要的、可伸缩的方式做到这一点:
The fastest way to resize images from ASP.NET. And it’s (more) supported-ish.
发布于 2011-05-05 03:14:55
多年来,这让我感觉很好:
public static void CreateThumbnail(string filename, int desiredWidth, int desiredHeight, string outFilename)
{
using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename))
{
float widthRatio = (float)img.Width / (float)desiredWidth;
float heightRatio = (float)img.Height / (float)desiredHeight;
// Resize to the greatest ratio
float ratio = heightRatio > widthRatio ? heightRatio : widthRatio;
int newWidth = Convert.ToInt32(Math.Floor((float)img.Width / ratio));
int newHeight = Convert.ToInt32(Math.Floor((float)img.Height / ratio));
using (System.Drawing.Image thumb = img.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailImageAbortCallback), IntPtr.Zero))
{
thumb.Save(outFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
public static bool ThumbnailImageAbortCallback()
{
return true;
}
发布于 2011-05-04 21:33:46
我使用ImageMagick进行照片处理
已更新
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using ImageMagickObject;
namespace photostorage.Models
{
public class PhotoProcessing
{
public MagickImage ResizeImg(string filepath, string filename)
{
Object[] rotate = new Object[] { filepath + "/" + filename,
"-auto-orient", filepath + "/" + filename };
Object[] big = new Object[] { filepath + "/" + filename,
"-resize", "800", filepath + "/" + "big_" + filename };
Object[] middle = new Object[] { filepath + "/big_" + filename,
"-resize", "400", filepath + "/" + "mid_" + filename };
Object[] small = new Object[] { filepath + "/mid_" + filename,
"-resize", "200", filepath + "/" + "small_" + filename };
Object[] crop = new Object[] { filepath + "/small_" + filename,
"-resize", "50", filepath + "/" + "crop_" + filename };
ImageMagickObject.MagickImage img =
new ImageMagickObject.MagickImage();
img.Convert(rotate);
img.Convert(big);
img.Convert(middle);
img.Convert(small);
img.Convert(crop);
return img;
}
}
}
控制器:
PhotoProcessing resizeImg = new PhotoProcessing();
[HttpPost]
public string Index(params,params,params...)
{
var GetResize = resizeImg.ResizeImg(
destinationFolder + "/" + curFolder, fullFileName);
}
https://stackoverflow.com/questions/5882307
复制相似问题