/// <summary> /// 保存文件 /// </summary> /// <param name="fileInfo"></param> /// <param name="stream"></param> /// <param name="extraArgs"></param> /// <returns></returns> public override async Task <FileMetaInfo> SaveAsync(FileMetaInfo fileInfo, Stream stream, IDictionary <string, object> extraArgs) { if (extraArgs == null || !extraArgs.ContainsKey("context")) { throw new ArgumentException("extraArgs 参数需要context键值"); } var context = extraArgs["context"].ToString(); var rule = FileStoreRuleManager.GetRuleByName(context) as ImageStoreRule; if (rule == null) { throw new Exception("未找到指定规则或默认规则:" + context); } fileInfo.Id = Guid.NewGuid().ToString(); var name = GetInnerFileName(fileInfo); var dir = rule.GetSaveDir(); await SaveFileAsync(dir, name, stream); fileInfo.Path = Path.Combine(rule.GetSaveDir(), name).Replace("\\", "/"); //保存文件信息 await MetaInfoStore.SaveAsync(fileInfo, rule.Id); return(fileInfo); }
/// <summary> /// 添加水印 /// </summary> /// <param name="imageStream"></param> /// <returns></returns> private async Task <Stream> AddWaterMarkAsync(Stream imageStream) { imageStream.Position = 0; var stream = new MemoryStream(); using (var imageF = new ImageFactory()) { imageF.Load(imageStream); var image = imageF.Image; var watermarkImage = FileStoreRuleManager.GetWatermarkImage(image.Size); var imgl = new ImageLayer(); imgl.Image = watermarkImage; imageF.AutoRotate(); imageF.Overlay(imgl); imageF.Save(stream); return(stream); } using (var image = Image.FromStream(imageStream.Duplicate())) { var watermarkImage = FileStoreRuleManager.GetWatermarkImage(image.Size); return(image.AddWatermark((Image)watermarkImage.Clone(), 5, 100, 10)); } }
/// <summary> /// 保存 /// </summary> /// <param name="fileInfo"></param> /// <param name="stream"></param> /// <param name="extraArgs"></param> /// <returns></returns> public override async Task <FileMetaInfo> SaveAsync(FileMetaInfo fileInfo, Stream stream, IDictionary <string, object> extraArgs) { if (extraArgs == null || !extraArgs.ContainsKey("context")) { throw new ArgumentException("extraArgs 参数需要context键值"); } var context = extraArgs["context"].ToString(); var rule = FileStoreRuleManager.GetRuleByName(context) as ImageStoreRule; if (rule == null) { throw new Exception("未找到指定规则或默认规则:" + context); } Guid fileId; bool modify = false; ; if (extraArgs.ContainsKey("fileid") && Guid.TryParse(extraArgs["fileid"].ToString(), out fileId)) { int rotate; if (extraArgs.ContainsKey("rotate") && int.TryParse(extraArgs["rotate"].ToString(), out rotate)) { modify = true; var fi = await GetOriginalAsync(fileId.ToString()); fileInfo = fi.Item1; var fileStream = fi.Item2; var imageFactory = new ImageFactory(); var ms = new MemoryStream(); imageFactory.Load(fileStream).AutoRotate().Rotate(rotate).Save(ms); ms.Position = 0; stream = ms; fileStream.Dispose(); } } else if (!fileInfo.Extension.ToLower().EndsWith("gif")) { var ms = new MemoryStream(); var imageF = new ImageFactory(); imageF.Load(stream).AutoRotate().Save(ms); ms.Position = 0; stream.Position = 0; stream = ms; } ; if (string.IsNullOrWhiteSpace(fileInfo.Id) || fileInfo.Id == default(Guid).ToString()) { fileInfo.Id = Guid.NewGuid().ToString(); } await SaveInternalAsync(fileInfo, stream, extraArgs, rule, modify); return(fileInfo); }