问题
I'm having trouble figuring out why .FillRectangle is not working for me.
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
Thanks in advance,
Rubén :)
回答1:
Ok, that helped a lot :D. Thanks!
Posting comment as an Answer then:
this.CreateGraphics is the likely culprit. 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.
来源:https://stackoverflow.com/questions/27766924/fillrectangle-not-drawing-anything