为什么这个简单的C#试用版没有工作 [英] why doesn't this simple C# tryout work

查看:119
本文介绍了为什么这个简单的C#试用版没有工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这造成了条纹而不是点。为什么?我正在尝试绘制各个像素。另一种方法也被尝试过(使用fillrectangle),它也没有给出所需的结果,得到了条而不是点。

  protected override void OnPaint(PaintEventArgs pea)
{

Graphics g = pea.Graphics; (int y = 1; y <= Width; y ++)

for(int x = 1; x <= Height; x ++)
{
System.Random rand = new System.Random();
Color c =(Color.FromArgb(rand.Next(256),
rand.Next(256),
rand.Next(256)));
// SolidBrush myBrush = new SolidBrush(c);
// g.FillRectangle(myBrush,x,y,1,1);


位图pt =新位图(1,1);
pt.SetPixel(0,0,c);
g.DrawImageUnscaled(pt,x,y);


$ b $ / code $ / pre>

}



这里发生了什么?

解决方案

您应该 创建每次都有一个新的 Random 对象。既然这样会给你一遍又一遍重复的完全相同的随机数字。



改为使用相同的Random对象,只需调用 Next 即可。


this creates streaks instead of dots. why ? I am trying to paint individual pixels. another method was also tried (using fillrectangle) , which also didn't give the desired result, got bars instead of dots.

protected override void OnPaint(PaintEventArgs pea )
    {

         Graphics g = pea.Graphics ;

         for ( int y = 1 ; y <= Width ; y++ )
         {
            for (  int x  =  1  ; x <=  Height    ; x++ )
            {
                System.Random rand = new  System.Random() ;
                Color c =   (Color.FromArgb(rand.Next(256),
                                    rand.Next(256),
                                    rand.Next(256)));
            // SolidBrush myBrush = new SolidBrush(c);
            // g.FillRectangle(myBrush, x, y, 1, 1);


                 Bitmap pt = new Bitmap(1, 1);
                 pt.SetPixel(0, 0, c);
                 g.DrawImageUnscaled(pt, x, y);

            }
        }

}

what is happening here ?

解决方案

You should not create a new Random object each time. Since that will give you the exact same "random" numbers repeated over and over.

Instead use the same Random object and just call Next on in.

这篇关于为什么这个简单的C#试用版没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆