0

我在通过组合多个位图部分并返回一个任务来构建单个位图的方法中使用了一些异步方法。

一切正常单元我开始添加异步方法和返回任务的方法。

我认为该错误告诉我两个线程正在尝试访问相同的资源,但我不知道,因为我不是 Async 的大师。

这是我的 XML 错误:

    <Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Parameter is not valid.</ExceptionMessage>
<ExceptionType>System.ArgumentException</ExceptionType>
<StackTrace>
at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat) at System.Drawing.Graphics.MeasureString(String text, Font font) at CoverPage.CoverPage.<>c__DisplayClass1a.<GetBoxContainer>b__19() in c:\Users\Administrator\Documents\Visual Studio 11\Projects\StorefrontSystem\CoverPage\CoverPage.cs:line 219 at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at CoverPage.CoverPage.<>c__DisplayClass5.<>c__DisplayClassb.<<GetCoverPageAsync>b__2>d__10.MoveNext() in c:\Users\Administrator\Documents\Visual Studio 11\Projects\StorefrontSystem\CoverPage\CoverPage.cs:line 165
</StackTrace>
</Error>

我讨厌发布一堆代码,但我不知道该怎么做,因为它们是一些嵌套的异步方法,这可能是我收到此错误的原因。

public static Task<Bitmap> GetCoverPageAsync(IProjectInfo projectInfo, IElevation elevation)
    {
        return Task.Run(() =>
            {
                int height = DrawingOptions.PAGE_WIDTH_SIZE,
                    width = DrawingOptions.PAGE_HEIGHT_SIZE,
                    prevBottom = 0;

                short center = (short)(width / 2),
                      startHeight = 50,
                      interiorBorderRight = (short)(width - 40 - 400),
                      interiorLeftBorder = 220,
                      margin = 20;

                center -= 100;

                string genTxt;
                System.Drawing.Size txtSize;

                Bitmap coverPage = new Bitmap(width, height);
                coverPage.SetResolution(150, 150);

                StringBuilder sb = new StringBuilder();
                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                RectangleF rContent = new RectangleF();

                using (var dc = Graphics.FromImage(coverPage))
                {
                    dc.Clear(DrawingBase.BACK_COLOR);

                    using (var pen = DrawingOptions.GetDetailsPen(PenAlignment.Inset))
                    {
                        using (var sFont = DrawingOptions.FONT_SMALL)
                        using (var mFont = DrawingOptions.FONT_MEDIUM)
                        using (var lFont = DrawingOptions.FONT_LARGE)
                        {
                            //............................inner info..................................
                            //............................project location / contact..................
                            //shop drawings provided for
                            genTxt = "SHOP DRAWINGS PROVIDED FOR:";
                            txtSize = TextRenderer.MeasureText(genTxt, sFont);
                            prevBottom = startHeight + txtSize.Height;
                            dc.DrawString(genTxt, sFont, new SolidBrush(DrawingBase.LINE_COLOR), center - (txtSize.Width / 2) + margin, startHeight);
                            prevBottom += 25;
                            //seperator border
                            dc.DrawRectangle(pen, interiorLeftBorder, prevBottom, interiorBorderRight, 5);


                            //............................project info.......................
                            rContent = new RectangleF(interiorLeftBorder, prevBottom, interiorBorderRight, 300);
                            //dc.DrawRectangle(pen, recProjectContent.X, recProjectContent.Y, recProjectContent.Width, recProjectContent.Height);

                            sb.Append("\n\n").Append(projectInfo.ProjStreet).Append("\n").Append(projectInfo.ProjCity).Append(",")
                              .Append(projectInfo.ProjSt).Append(" ").Append(projectInfo.ProjZip).Append("\n").Append(projectInfo.ProjPhone)
                              .Append(" or ").Append(projectInfo.ProjEmail);

                            // Draw the text and the surrounding rectangle.
                            dc.DrawString(sb.ToString(), mFont, new SolidBrush(DrawingOptions.LINE_COLOR), rContent, stringFormat);

                            //Project name
                            genTxt = projectInfo.ProjectName;
                            txtSize = TextRenderer.MeasureText(genTxt, lFont);
                            dc.DrawString(genTxt, lFont, new SolidBrush(DrawingBase.LINE_COLOR), center - (txtSize.Width / 2), prevBottom + 25);
                            //............................project info end....................

                            //seperator border
                            dc.DrawRectangle(pen, interiorLeftBorder, rContent.Bottom, interiorBorderRight, 5);


                            //............................project installer.................................
                            //dc.DrawRectangle(pen, recInstallerContent.X, recInstallerContent.Y, recInstallerContent.Width, recInstallerContent.Height);
                            rContent = new RectangleF(interiorLeftBorder, rContent.Bottom, interiorBorderRight, 400);

                            sb.Clear();
                            sb.Append("Installer\n\n\n").Append(projectInfo.Street).Append("\n").Append(projectInfo.City).Append(", ")
                              .Append(projectInfo.StateCD).Append(" ").Append(projectInfo.ZipCode).Append("\n").Append(projectInfo.Phone)
                              .Append(" or ").Append(projectInfo.Email);
                            // Draw the text and the surrounding rectangle.
                            dc.DrawString(sb.ToString(), mFont, new SolidBrush(DrawingOptions.LINE_COLOR), rContent, stringFormat);
                            //installer name
                            genTxt = projectInfo.Name;
                            txtSize = TextRenderer.MeasureText(genTxt, lFont);
                            dc.DrawString(genTxt, lFont, new SolidBrush(DrawingBase.LINE_COLOR), center - (txtSize.Width / 2), rContent.Bottom + 85);
                            //............................project installer end..............................

                            //seperator bordre
                            dc.DrawRectangle(pen, interiorLeftBorder, rContent.Bottom, interiorBorderRight, 5);
                            //............................project installer end..............................

                            //............................alum cloud.................................
                            //TM
                            dc.DrawImageUnscaled(Resources.tm_200x142, center - (Resources.tm_200x142.Width / 2), (int)rContent.Bottom + 25);
                            prevBottom += 100 + (int)rContent.Bottom + Resources.tm_200x142.Height;

                            rContent = new RectangleF(interiorLeftBorder, rContent.Bottom, interiorBorderRight, 450);
                            //dc.DrawRectangle(pen, recACContent.X, recACContent.Y, recACContent.Width, recACContent.Height);
                            // Draw the text and the surrounding rectangle.
                            dc.DrawString("\n\n\n\nThe industry leader in CAD drafting software\nutilizing \"cloud computing\"\ntechnology.",
                                                mFont, new SolidBrush(DrawingOptions.LINE_COLOR), rContent, stringFormat);
                            //............................alum cloud end..............................

                            //............................inner info end..............................


                            //............................top info boxes..............................
                            //............................project framing system.......................
                            sb.Clear();
                            genTxt = "Doors: 0";
                            if (projectInfo.ProjectLeafs != null)
                            {
                                var leafs = projectInfo.ProjectLeafs.ToList<IProjectLeafs>();
                                if (leafs.Count() > 0)
                                {
                                    sb.Append("Doors: ");
                                    for (short i = 0; i < leafs.Count(); i++)
                                    {
                                        sb.Append(leafs[i].Total + " " + leafs[i].Stile + ", ");
                                    }
                                    genTxt = sb.ToString().TrimEnd(new char[] { ',', ' ' }) + " Stile";
                                }
                            }

                            sb.Clear();
                            sb.AppendLine("Framing: " + elevation.FrameName.Replace("|Storefront", "").Replace("Series", ""))
                              .AppendLine("Elevations: " + projectInfo.Elevations)
                              .AppendLine(genTxt)
                              .AppendLine("Material: " + "6063-T6 Aluminum")
                              .Append("Finish: " + elevation.Bays[0].FinishName + " Anodized");

                            //product info, top left
                            using (var canvasProdInfo = Task.Run(async () => await GetBoxContainer(610, 150, "Products", sb.ToString(), RotateFlipType.Rotate90FlipNone)).Result)
                            {
                                dc.DrawImageUnscaled(canvasProdInfo,/*left to right*/ margin + 2,/*up down*/  margin);
                            }
                            //............................project framing system end..............................

                            //notes, bottom left
                            //if (true/*project has a submittal date*/)
                            // {
                            //submitDate = " Original show drawings submittal on July 29, 2013.";
                            //}
                            // else
                            // {
                            genTxt = "1.) Please verify building codes.\n2.) Drawings have not been submitted for review.";
                            //}
                            using (var generalNotes = Task.Run(async () => await GetBoxContainer(610, 150, "General Notes", genTxt, RotateFlipType.Rotate90FlipNone)).Result)
                            {
                                dc.DrawImageUnscaled(generalNotes,/*left to right*/  margin + 2,/*up down*/ (coverPage.Height - generalNotes.Height) - margin + 2);
                            }
                            //............................top info boxes end...........................


                            //............................bottom info boxes.............................
                            //revisions, bottom right
                            using (var revisions = Task.Run(async () => await GetRevisions(610, 150)).Result)
                            {
                                dc.DrawImageUnscaled(revisions,/*left to right*/ coverPage.Width - 170 - margin,/*up down*/ (coverPage.Height - revisions.Height) - margin + 3);
                            }

                            //Drafting, top right
                            using (var draftNote = Task.Run(async () => await GetBoxContainer(610, 150, "Drafting Notes", "1.) " + projectInfo.Note, RotateFlipType.Rotate270FlipNone)).Result)
                            {
                                dc.DrawImageUnscaled(draftNote, /*left to right*/ coverPage.Width - 170 - margin,/*up down*/ margin - 1);
                            }
                            //............................bottom info boxes end..........................


                            //bottom line
                            dc.DrawLine(pen, coverPage.Width - 200, margin, coverPage.Width - 200, coverPage.Height - margin);
                            //top line
                            dc.DrawLine(pen, 200, margin, 200, coverPage.Height - margin);
                            //page edge
                            dc.DrawRectangle(pen, margin, margin, width - (margin * 2), height - (margin * 2));
                        };
                    };
                };

                coverPage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                return coverPage;
            });
    }

有什么建议么?

非常感谢这个!!!

4

2 回答 2

0

我相信您传递给该DrawString方法的某些参数对其无效。

它可能是:

  • 空字符串
  • 处置的对象,如字体或画笔
于 2013-07-30T17:28:28.240 回答
0

我发现了我的问题。

我以静态方式访问我的字体,如常量,在 using(){} 块内。

所以我写了一个函数,它返回一个新的常量字体副本,这样字体是否被处理并不重要。

于 2013-07-30T17:25:40.943 回答