首页 > 代码库 > C#简单的图片合成及防止并发的办法
C#简单的图片合成及防止并发的办法
/// <summary> /// 合成图 /// </summary> private string ComposeCarBrandBadImage(AnonAttachmentFilter filter) { filter.pageIndex = 1; filter.pageSize = 100; IList<AnonAttachment> attachmentList = B_Attachment.Instance.GetList(filter); int height = 250 * ((int)((attachmentList.Count + 1) / 2)); height = height <= 0 ? 320 : height; Bitmap _newBitmap = new Bitmap(645, height); //_newBitmap.SetPixel(250, 300,Color.White); Graphics _graphics = Graphics.FromImage(_newBitmap); _graphics.Clear(Color.White); int x = 0, y = 0; string path = string.Empty,copyPath=string.Empty; List<string> pathlist = new List<string>(); for (int i = 0; i < attachmentList.Count; i++) { x = (i + 1) % 2 == 0 ? 325 : 20; y = 245 * ((int)(i / 2)) + 5; path = System.Web.HttpContext.Current.Server.MapPath(".." + attachmentList[i].Path); if (!File.Exists(path)) continue;//如果文件不存在。 // 用于生成图片的拷贝,处理并发操作时导致的无法进行读写操作的问题 copyPath = path.Substring(0, path.LastIndexOf("\\") + 1) + Guid.NewGuid().ToString() + path.Substring(path.LastIndexOf("\\")+1); File.Copy(path, copyPath); pathlist.Add(copyPath); Image img = System.Drawing.Image.FromFile(copyPath); _graphics.DrawImage(img, x, y, 300, 240); img.Dispose(); } if (attachmentList.Count == 0) { _newBitmap = new Bitmap(600, 320); //_newBitmap.SetPixel(250, 300,Color.White); _graphics = Graphics.FromImage(_newBitmap); _graphics.Clear(Color.White); _graphics.DrawImage(System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("../_theme/images/CarBad.gif")), 0, 0, 600, 320); } //_graphics.DrawString("文字", new Font(FontFamily.GenericSerif, 10), Brushes.Yellow, 50, 50); _newBitmap.Save(System.Web.HttpContext.Current.Server.MapPath("../_upload/carBrandBadImg/") + filter.FromId + "/ComposeCarBrandBadImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); _graphics.Dispose(); foreach (string ph in pathlist) { File.Delete(ph); } return "../_upload/carBrandBadImg/" + filter.FromId + "/ComposeCarBrandBadImage.jpg"; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。