@functions{
public void GetThumbnailView(string originalImagePath, int height, int width)
{
//Consider Image is stored at path like "ProductImage\\Product1.jpg"
//Now we have created one another folder ProductThumbnail to store thumbnail image of product.
//So let name of image be same, just change the FolderName while storing image.
string thumbnailImagePath = originalImagePath;
originalImagePath = originalImagePath.Replace("thumb_", "");
//If thumbnail Image is not available, generate it.
if (!System.IO.File.Exists(Server.MapPath(thumbnailImagePath)))
{
System.Drawing.Image imThumbnailImage;
System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath));
double originalWidth = OriginalImage.Width;
double originalHeight = OriginalImage.Height;
double ratioX = (double)width / (double)originalWidth;
double ratioY = (double)height / (double)originalHeight;
double ratio = ratioX < ratioY ? ratioX : ratioY; // use whichever multiplier is smaller
// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
imThumbnailImage = OriginalImage.GetThumbnailImage(newWidth, newHeight,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
imThumbnailImage.Save(Server.MapPath(thumbnailImagePath), System.Drawing.Imaging.ImageFormat.Jpeg);
imThumbnailImage.Dispose();
OriginalImage.Dispose();
}
}
public bool ThumbnailCallback() { return false; }
}
在另一个StackOverflow的问题中,我找到了这段代码并真的很喜欢它,但在使用它时,创建缩略图时出现了以下问题:
应用程序中的服务器错误。
内存不足。描述:当前网络请求的执行过程中发生未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息以及其在代码中的起源。
异常详情:System.OutOfMemoryException: 内存不足。
源错误:
Line 199: {
Line 200: System.Drawing.Image imThumbnailImage;
Line 201: System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath.ToString()));
Line 202:
Line 203: double originalWidth = OriginalImage.Width;
Source File: c:\Inetpub\wwwroot\Lokal\Views\Stok\SatisRaporu.cshtml
Line: 201
我的好奇心让我进入了异常详细信息,并看到了这个:
//
// Summary:
// Creates an System.Drawing.Image from the specified file.
//
// Parameters:
// filename:
// A string that contains the name of the file from which to create the System.Drawing.Image.
//
// Returns:
// The System.Drawing.Image this method creates.
//
// Exceptions:
// System.OutOfMemoryException:
// The file does not have a valid image format.-or- GDI+ does not support the
// pixel format of the file.
//
// System.IO.FileNotFoundException:
// The specified file does not exist.
//
// System.ArgumentException:
// filename is a System.Uri.
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static Image FromFile(string filename);
但是,该文件夹中的所有图片都具有“.jpg”扩展名,所以对我来说似乎很奇怪。如果我无法从“.jpg”创建缩略图,我还能做什么?
我实际上想了解是否有人尝试在“.jpg”文件上尝试过,并且遇到了问题?如果没有问题发生,我可能做错了什么?
一个小注释:我在使用Razor语法的视图中进行此操作。我对C#语言略有了解,并且每天都在提高我的知识水平。
编辑:
如何调用该函数:
GetThumbnailView("../pics/thumb_" + (("0000000" + stocks.stockcode).Substring(("0000000" + stocks.stockcode).Length - 7, 7)) + ".jpg", 200, 200);
OutOfMemoryException
异常被抛出。我对编写这段代码的人失去了一点尊重。他们应该验证输入并抛出另一种异常。 - DavidGetThumbnailView("../pics/thumb_" + (("0000000" + stocks.stockkode).Substring(("0000000" + stocks.stockkode).Length - 7, 7)) + ".jpg", 200, 200);
,其中包含“.jpg”后缀。 - Berker Yüceer