.FillRectangle不绘制任何东西 [英] .FillRectangle not drawing anything
问题描述
此外,由于它没有任何例外,所以我跑出了一些想法,为什么它可能是,所以我在这里需要一些帮助:/ b / b>
受影响的代码部分是这个
<$ p $ (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);
图形g = this.CreateGraphics();
g.TranslateTransform(mW,mH); ((line = streamReader.ReadLine())!= null)
{
string [] points = line.Split(',');
A = new PointF((float)(Convert.ToDecimal(points [0])),((float)(Convert.ToDecimal(points [1]))));
// A的定义是正确的,因为A.X和A.Y都有值
g.FillRectangle(myBrush,A.X,A.Y,1f,1f);
$ b catch(例外p)
{
MessageBox.Show(p.Message);
}
任何帮助都会超赞,因为我对Grpahics的知识是非常有限 在此先感谢, Rubén:) 好的,这有很大的帮助:D。 发表评论作为答案然后: 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 Any help would be supper appreciated, as my knowledge about Grpahics is very limited Thanks in advance, Rubén :) Ok, that helped a lot :D. Thanks! Posting comment as an Answer then: 这篇关于.FillRectangle不绘制任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
this.CreateGraphics
是可能的罪魁祸首。这将绘制到临时表面,当表单刷新时会被擦除。在Paint()事件中使用 e.Graphics
,或者使用 Graphics.FromImage()
绘制位图显示。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);
}
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.