C#簡単な画像合成と同時発生防止の方法

7125 ワード

        /// <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";

        }