0

进入页面后,我使用刷新方法重绘控件。如果我查看任务管理器,我可以看到最初是 40.000K 内存,进入另一个页面后它将是 50.000,依此类推,但在达到 100-130 或更多之后,在某些页面之后它又会是 100。

我只创建了第一次 100 个控件,您可以在其中看到图片。如果我尝试转到下一页,它将只更新游戏名称、图片等值。我正在使用 onPaint 事件来绘制控件,而 IDisposable 控件则使用 using()..页码是标签。所有游戏控件都是在 floatLayout.Game 控件中,图片所在的位置。我不调用手动绘制事件,我只是调用刷新或无效方法。在第二个代码中,有时我会得到 nullReference 异常,用于 e.graphics。e.graphics 有时为空.....我不知道为什么会这样。

在此处输入图像描述 在此处输入图像描述

我不知道什么时候,但过了一段时间,每次都不一样。我不知道如何手动使程序崩溃,这种情况突然发生

这是我的两个代码:

我的游戏控制油漆:

protected override void OnPaint(PaintEventArgs e)
{

    base.OnPaint(e);

    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    if (this.ClientRectangle.Width > 0 && this.ClientRectangle.Height > 0)
        using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
               Color.LightGray,
               Color.Silver,
               80F))
        {
            e.Graphics.FillRectangle(brush, this.ClientRectangle);
        }
    e.Graphics.DrawRectangle(componentBorderColor, 0, 0, this.Width - 1, this.Height - 1);

    if (AllGameInfoModel == null)
    {
        e.Graphics.DrawString("Kraunama" + loadingText, Namefont, NameBrush, 26, ControlHelpers.MeasureString("Kraunama", Namefont).Height / 2);
    }
    if (AllGameInfoModel != null)
    {
        NameTextSize = e.Graphics.MeasureString(AllGameInfoModel.Name, Namefont);
        e.Graphics.DrawString(AllGameInfoModel.Name, Namefont, NameBrush, 26, ControlHelpers.MeasureString(AllGameInfoModel.Name, Namefont).Height / 3);

        e.Graphics.DrawRectangle(componentBorderColor, 0, 0, this.Width - 1, 24);

        if (showControl == false)
        {

        }
        else
        {
            if (makeBigerControl == false)
                coverLocationHeight = Height - 35;
            if (Cover != null)
            {
                e.Graphics.DrawImage(Cover, new Rectangle(4, 30, coverLocationWidth, coverLocationHeight));
            }
            else
            {
                //Jei nėra paveikslėlio
                e.Graphics.DrawImage(CoverNoImage, new Rectangle(4, 30, coverLocationWidth, coverLocationHeight));

            }
            int LocationX = coverLocationWidth + 6;
            int LocationY = 30;
            if (AllGameInfoModel.ReleaseDate != null)//Išleidimo data
            {

                SizeF yearSize = ControlHelpers.drawString(e, "Metai:", LocationX, LocationY, null, new Font(ControlFont.FontFamily, ControlFont.Size, FontStyle.Bold));
                ControlHelpers.drawString(e, AllGameInfoModel.ReleaseDate.Value.Year.ToString(), yearSize.Width + LocationX, LocationY, null, ControlFont);

                LocationY = LocationY + (int)yearSize.Height;

                SizeF releaseDateSize = ControlHelpers.drawString(e, "Išleidimo data:", LocationX, LocationY, null, new Font(ControlFont.FontFamily, ControlFont.Size, FontStyle.Bold));
                ControlHelpers.drawString(e, AllGameInfoModel.ReleaseDate.Value.ToShortDateString(), releaseDateSize.Width + LocationX, LocationY, null, ControlFont);

                LocationY = LocationY + (int)releaseDateSize.Height;
            }
            if (!string.IsNullOrEmpty(AllGameInfoModel.Genre)) //Žanras
            {
                SizeF genreSize = ControlHelpers.drawString(e, "Žanras:", LocationX, LocationY, null, new Font(ControlFont.FontFamily, ControlFont.Size, FontStyle.Bold));
                ControlHelpers.drawString(e, getGenreInLt(AllGameInfoModel.Genre), genreSize.Width + LocationX, LocationY, null, ControlFont);

                LocationY = LocationY + (int)genreSize.Height;
            }
            if (!string.IsNullOrEmpty(AllGameInfoModel.DevelopersPublishers) && string.IsNullOrEmpty(AllGameInfoModel.Developers) && string.IsNullOrEmpty(AllGameInfoModel.Publishers))//Leidėjai, kūrėjai
            {
                SizeF genreSize = ControlHelpers.drawString(e, "Kūrėjai / Leidėjai:", LocationX, LocationY, null, new Font(ControlFont.FontFamily, ControlFont.Size, FontStyle.Bold));
                ControlHelpers.drawString(e, AllGameInfoModel.DevelopersPublishers, genreSize.Width + LocationX, LocationY, null, ControlFont);

                LocationY = LocationY + (int)genreSize.Height;
            }
            if (!string.IsNullOrEmpty(AllGameInfoModel.Developers)) //Kūrėjai
            {
                SizeF genreSize = ControlHelpers.drawString(e, "Kūrėjai:", LocationX, LocationY, null, new Font(ControlFont.FontFamily, ControlFont.Size, FontStyle.Bold));
                ControlHelpers.drawString(e, AllGameInfoModel.Developers, genreSize.Width + LocationX, LocationY, null, ControlFont);

                LocationY = LocationY + (int)genreSize.Height;
            }
            if (!string.IsNullOrEmpty(AllGameInfoModel.Publishers)) //Leidėjai
            {
                SizeF genreSize = ControlHelpers.drawString(e, "Leidėjai:", LocationX, LocationY, null, new Font(ControlFont.FontFamily, ControlFont.Size, FontStyle.Bold));
                ControlHelpers.drawString(e, AllGameInfoModel.Publishers, genreSize.Width + LocationX, LocationY, null, ControlFont);

                LocationY = LocationY + (int)genreSize.Height;
            }
            bool isPegi = false;
            if ((AllGameInfoModel.AgeQualification != null) && (AllGameInfoModel.AgeQualification != 0)) //Amžiaus cenzas
            {
                using (Bitmap picture = new Bitmap(@"Images\pegi\" + AllGameInfoModel.AgeQualification + ".gif"))
                    e.Graphics.DrawImage(picture, new Rectangle(LocationX, LocationY, pegiImageHeightWidth, pegiImageHeightWidth));//40

                using (Pen ageRectangleBrush = new Pen(Color.Black))
                    e.Graphics.DrawRectangle(ageRectangleBrush, new Rectangle(LocationX, LocationY, pegiImageHeightWidth, pegiImageHeightWidth));

                isPegi = true;
            }

            /////Pegi Informacija//////
            int cordsX = LocationX + pegiImageHeightWidth + 5;//45
            foreach (string pegiPicture in PegiPictures)
            {
                var pegiCheck = AllGameInfoModel["Is" + pegiPicture];
                if (pegiCheck != null)
                    if (string.Equals("true", pegiCheck.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        using (Bitmap picture = new Bitmap(@"Images\Pegi\" + pegiPicture + ".gif"))
                            e.Graphics.DrawImage(picture, new Rectangle(cordsX, LocationY, pegiImageHeightWidth, pegiImageHeightWidth));

                        e.Graphics.DrawRectangle(componentBorderColor, new Rectangle(cordsX, LocationY, pegiImageHeightWidth, pegiImageHeightWidth));

                        cordsX = cordsX + pegiImageHeightWidth + 5;//45
                        isPegi = true;

                    }
            }
            if (isPegi)
            {
                LocationY = LocationY + pegiImageHeightWidth + 7;//47
            }
            if (!string.IsNullOrEmpty(AllGameInfoModel.Description)) //Aprašymas
            {
                descriptionFlag.Location = new Point(LocationX, LocationY + 3);
                descriptionFlag.Visible = true;

                ControlHelpers.drawString(e, "Angliškas:", LocationX + descriptionFlag.Width, LocationY + 3, null, new Font(ControlFont.FontFamily, ControlFont.Size, FontStyle.Underline | FontStyle.Bold));
                LocationY = LocationY + 20;

                Rectangle rect = new Rectangle();
                rect.Location = new Point(LocationX, LocationY + 3);
                rect.Width = this.Width - LocationX - 1;
                if (MakeBigerControl)
                {
                    rect.Height = 300;
                }
                else
                {
                    rect.Height = 40;
                }
                //using (SolidBrush descriptionBrush = new SolidBrush(Color.Black))
                e.Graphics.DrawString(Description, ControlFont, Brushes.Black, rect);


            }
            if (!string.IsNullOrEmpty(AllGameInfoModel.Platform)) //Platforma
            {
                if (!string.IsNullOrEmpty(PlatformPicture))
                    using (Bitmap picture = new Bitmap(@"Images\Platforms\" + PlatformPicture + ".png"))
                        //e.Graphics.DrawImage(picture,new Rectangle(this.Width/2 + picture.Width/2, this.Height/2 - picture.Height/2,picture.Width, picture.Height)); //40
                        e.Graphics.DrawImage(picture, new Rectangle(this.Width - picture.Width - 2, 27, picture.Width, picture.Height)); //40
            }
            //Draw End
        }

    }

}

我的星控油漆:

protected override void OnPaint(PaintEventArgs e)
    {
    //if (e.Graphics != null)
    //{
        base.OnPaint(e);
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        for (int i = 0; i < stars.Count; i++)
        {
            PointF[] pts = StarPoints(5, stars[i].starBounds);
            //using (Pen pen = new Pen(Color.Black))
            e.Graphics.DrawPolygon(controlColor, pts);

            //using (SolidBrush solidBrush = new SolidBrush(stars[i].backColor))
                e.Graphics.FillPolygon(stars[i].backColor, pts, FillMode.Winding); //FillMode.Winding

            SizeF textSize = ControlHelpers.MeasureString((i + 1) + "", RateFont);
            Rectangle textBounds = new Rectangle();
            textBounds.Width = stars[i].starBounds.Width;
            textBounds.Height = stars[i].starBounds.Height;
            textBounds.Location = new Point(stars[i].starBounds.X + textBounds.Width/2 - (int) textSize.Width/2,
            textBounds.Height/2 - (int) textSize.Height/2);

            //using (SolidBrush solidBrush = new SolidBrush(Color.Black))
            //using (Font font = new Font("Times New Roman", 6, FontStyle.Bold))
            e.Graphics.DrawString((i + 1) + "", RateFont, Brushes.Black, textBounds);


        }

    }
4

0 回答 0