我们已经有很长时间了,并尝试了网络中的许多不同修复程序。到目前为止,什么都没有。
问题:基本图像可以节省良好,缩略图在保存上失败。在下面的解决方案之前,我已经尝试为所有图像(基本图像,600x600和300x300拇指(创建单独的流,但这也不起作用。所有流都是从相同的字节阵列构造的。请记住这一点:这在开发环境,测试环境和Test2环境中都可以正常工作,但是在生产环境中没有工作。我已经检查了文件夹上的所有设置/环境变量/权限,所有设置的设置与测试环境相同。
路径的进入如下:
- 基本路径:"〜/images/imageupl/"
- 缩略图添加。路径:"拇指/"
- 缩略图添加。路径:"拇指/"
- 图像名称结构:" x_yyymmdd_hhmmss_xx.png"
这些路径都是正确的 - 因为它可以使用DEV/TEST/TEST2环境。
对此的任何帮助都非常感谢!
编辑:到目前为止我们尝试过的内容:
- 设置网络和IISUSER的权限
- 为从原始源数据构建的每个图像使用单独的流
- 根据其他示例添加thread.sleep(30 (
- 从调整大小的一个图中创建一个新的位图并保存该
- 测试其目录是否在生产上发行的不同途径
编辑2:供参考,这是一个ASP.NET MVC5 Web应用程序,运行.NET Framework 4.7.2。
图像处理器类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
using Newtonsoft.Json;
using System.Threading;
namespace CMS_MVC.Classes
{
public class ImageProcessor : IDisposable
{
public enum PathType : int
{
Relative = 1,
Static = 2
}
private Stream _ImageStream { get; set; }
private HttpContext _Context { get; set; }
public Image BaseImage { get; set; }
private int _instanceId { get; set; }
public ImageProcessor(int instanceId, Stream imageStream, HttpContext context)
{
this._ImageStream = imageStream;
this._Context = context;
this._instanceId = instanceId;
this.BaseImage = Image.FromStream(this._ImageStream);
}
public ImageProcessor(int instanceId, byte[] imageByteArray, HttpContext context)
{
this._Context = context;
this._instanceId = instanceId;
this._ImageStream = new MemoryStream(imageByteArray);
this.BaseImage = Image.FromStream(this._ImageStream);
}
public ImageProcessor(int instanceId, string imagePath, PathType pathType, HttpContext context)
{
this._Context = context;
this._instanceId = instanceId;
if (pathType == PathType.Relative)
{
this._ImageStream = new MemoryStream(File.ReadAllBytes(this._Context.Server.MapPath(imagePath)));
this.BaseImage = Image.FromStream(this._ImageStream);
}
else
{
this._ImageStream = new MemoryStream(File.ReadAllBytes(imagePath));
this.BaseImage = Image.FromStream(this._ImageStream);
}
}
public Dictionary<string, bool> SaveImages(string baseImageSavePath, string imageName, Dictionary<string, Tuple<int, int>> thumbnails = null)
{
Dictionary<string, bool> results = new Dictionary<string, bool>();
string lastResult = "main";
results.Add(lastResult, true);
try
{
this.BaseImage.Save(this._Context.Server.MapPath(Path.Combine(baseImageSavePath, imageName)), ImageFormat.Png);
if (thumbnails != null)
{
foreach (var thumbnail in thumbnails)
{
lastResult = thumbnail.Value.Item1.ToString() + "_" + thumbnail.Value.Item2.ToString();
results.Add(lastResult, true);
using (Bitmap thumbBitmap = this.ResizeImage(thumbnail.Value.Item1, thumbnail.Value.Item2))
{
Thread.Sleep(50);
thumbBitmap.Save(this._Context.Server.MapPath(Path.Combine(baseImageSavePath + thumbnail.Key, imageName)), ImageFormat.Png);
Thread.Sleep(50);
}
}
}
}
catch (Exception ex)
{
results[lastResult] = false;
// Log event
}
return results;
}
private Bitmap ResizeImage(int targetWidth, int targetHeight)
{
Tuple<int, int> destSize = this.CalculateThumbnailSizeAspectRatio(targetWidth, targetHeight);
var destRect = new Rectangle(0, 0, destSize.Item1, destSize.Item2);
var destImage = new Bitmap(destSize.Item1, destSize.Item2);
destImage.SetResolution(this.BaseImage.HorizontalResolution, this.BaseImage.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(this.BaseImage, destRect, 0, 0, this.BaseImage.Width, this.BaseImage.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
private Tuple<int, int> CalculateThumbnailSizeAspectRatio(int targetWidth, int targetHeight)
{
// Resize calculations
}
public void Dispose()
{
if (this._ImageStream != null) this._ImageStream.Dispose();
if (this.BaseImage != null) this.BaseImage.Dispose();
}
}
}
来自文档网站:
系统中的类。在Windows或ASP.NET服务中不支持Drawing名称空间。尝试从这些应用程序类型之一中使用这些类别可能会产生意外的问题,例如服务性能降低和运行时间异常。
然后,该网站将您引导到WICS,但这不是一个好主意。服务器上也不支持WPF媒体类(但缺少有关此的同样清晰的声明(。
出现了各种替代方案,这里是一个较旧的问题。
您最好的选择是寻找一个完全管理的解决方案,A.O。Imagessharp。
我仍在寻找有关System.Drawing.Common
,.NET核心库的类似语句或见解。