简体   繁体   English

.FillRectangle 不绘制任何东西

[英].FillRectangle not drawing anything

I'm having trouble figuring out why .FillRectangle is not working for me.我无法弄清楚为什么 .FillRectangle 对我不起作用。

Furthermore, as it's not thorwing any exception, I ran out of ideas why it could be, so I need some help here:/此外,由于它没有抛出任何异常,我想不出为什么会这样,所以我需要一些帮助:/

The part of the code affected is this受影响的代码部分是这样的

try
{
    using (FileStream fileStream = File.OpenRead(filePath))
    {
        using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 1024))
        {
            String line;
            int mW = Convert.ToInt32(Math.Round(this.Size.Width / 2d));
            int mH = Convert.ToInt32(Math.Round(this.Size.Height / 2d));
            SolidBrush myBrush;
            myBrush = new SolidBrush(Color.Black);
            PointF A = new PointF(0f, 0f);
            Graphics g = this.CreateGraphics();
            g.TranslateTransform(mW, mH);
            while ((line = streamReader.ReadLine()) != null)
            {
                string[] points = line.Split(',');
                A = new PointF((float)(Convert.ToDecimal(points[0])), ((float)(Convert.ToDecimal(points[1]))));
                //A is defined correctly, as A.X and A.Y both have values
                g.FillRectangle(myBrush, A.X, A.Y, 1f, 1f);
            }
        }
    }
}
catch (Exception p)
{
    MessageBox.Show(p.Message);
}

Any help would be supper appreciated, as my knowledge about Grpahics is very limited任何帮助将不胜感激,因为我对 Grpahics 的了解非常有限

Thanks in advance,提前致谢,

Rubén :)鲁本 :)

Ok, that helped a lot :D.好的,这很有帮助:D。 Thanks!谢谢!

Posting comment as an Answer then:发表评论作为答案然后:

this.CreateGraphics is the likely culprit. this.CreateGraphics可能是罪魁祸首。 This draws to a temp surface that gets erased when the Form refreshes.这将绘制到一个临时表面,当表单刷新时该表面会被擦除。 Either use e.Graphics in the Paint() Event, or draw to a Bitmap with Graphics.FromImage() and display that.在 Paint() 事件中使用e.Graphics ,或者使用Graphics.FromImage()绘制到位图并显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM