码迷,mamicode.com
首页 > 其他好文 > 详细

C#简单的图片合成及防止并发的办法

时间:2014-05-07 02:34:38      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   ext   

bubuko.com,布布扣
        /// <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";
        }
bubuko.com,布布扣

 

C#简单的图片合成及防止并发的办法,布布扣,bubuko.com

C#简单的图片合成及防止并发的办法

标签:style   blog   class   code   java   ext   

原文地址:http://www.cnblogs.com/wangjingblogs/p/3712082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!