0

複数の Bitmap パーツを組み合わせて 1 つの Bitmap を構築し、1 つの Task を返すメソッドで、いくつかの Async メソッドを使用しています。

ユニットはすべて正常に機能し、非同期メソッドと Task を返すメソッドの追加を開始しました。

このエラーは、2 つのスレッドが同じリソースにアクセスしようとしていることを示していると思いますが、私は 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>

たくさんのコードを投稿するのは嫌いですが、これらはいくつかのネストされた Async メソッドであるため、このエラーが発生する理由である可能性があるため、他に方法がわかりません。

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 に答える