在.NET中生成缩略图的最快,最可靠的方法是什么?我需要获取任何图像,以JPEG格式压缩并调整大小.
我已经看过GDI +的几个例子,一些非自由组件,我记得WPF有一些关于成像的好东西.GDI +已经很老了,WPF的东西可能在服务器环境中没有任何好处.
这必须在ASP.NET MVC应用程序中运行,该应用程序在完全信任的情况下运行,并且如果可能的话,同步运行.
你会推荐什么?
更新:
基于Mantorok的回答我已经得出了这个例子,但它仍然是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 …Run Code Online (Sandbox Code Playgroud) 拍摄以下两张图片:
开发版 - IIS7 Windows 7 Pro 64位机

实时版本 - IIS7 Windows Server 2008 64位机器

请注意,Live版本是如何"像素化"并且看起来质量低,但Dev版本却流畅,消除锯齿并且看起来很好.这些都是由相同的代码生成的:
' Settings
Dim MaxHeight As Integer = 140
Dim MaxWidth As Integer = 140
Dim WorkingFolderPath As String = "\\server\share\bla\"
Dim AllowedFileExtensions As New ArrayList
AllowedFileExtensions.Add(".jpg")
AllowedFileExtensions.Add(".jpeg")
' Select an image to use from the WorkingFolder
Dim ImageFileName As String = ""
Dim WorkingFolder As New IO.DirectoryInfo(WorkingFolderPath)
Dim SourceImages As IO.FileInfo() = WorkingFolder.GetFiles()
For Each SourceImage As IO.FileInfo In SourceImages
If AllowedFileExtensions.Contains(SourceImage.Extension.ToLower) = True …Run Code Online (Sandbox Code Playgroud)