• 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";
            }
  • 相关阅读:
    Oracle存储过程实现返回多个结果集 在构造函数方法中使用 dataset
    刷卡客户端实现
    winform TreeView 节点选择
    flask入门
    redis介绍及常见问题总结
    微信消息推送
    redis介绍及在购物车项目中的应用,用户认证
    django实现支付宝支付
    nginx+uWSGI+django部署web服务器
    linux上部署redis实现与Python上的redis交互(有坑)
  • 原文地址:https://www.cnblogs.com/wangjingblogs/p/3712082.html
Copyright © 2020-2023  润新知