1

我正在使用 windows mobile 并想在屏幕上画线或写名字?

我搜索了 GDI+ 或相关但无法完成。我怎样才能画线?我的鼠标按下事件代码如下,但它不平滑,并且线条中的间隙不正确的线条绘制。

int radius = 3; //Set the number of pixel you wan to use here
                //Calculate the numbers based on radius
                int x0 = Math.Max(e.X - (radius / 2), 0),
                    y0 = Math.Max(e.Y - (radius / 2), 0),
                    x1 = Math.Min(e.X + (radius / 2), pbBackground.Width),
                    y1 = Math.Min(e.Y + (radius / 2), pbBackground.Height);
                Bitmap bm = (Bitmap)pbBackground.Image; //Get the bitmap (assuming it is stored that way)
                for (int ix = x0; ix < x1; ix++)
                {
                    for (int iy = y0; iy < y1; iy++)
                    {
                        bm.SetPixel(ix, iy, Color.Black); //Change the pixel color, maybe should be relative to bitmap
                    }
                }
                pbBackground.Refresh();
4

1 回答 1

2

执行此操作的最简单方法是使用 Graphics 类实例(尽管您可能必须使用定位来使线条位置与您正在寻找的宽度完全正确):

                int radius = 3; //Set the number of pixel you wan to use here
                //Calculate the numbers based on radius
                int x0 = Math.Max(e.X - (radius / 2), 0),
                    y0 = Math.Max(e.Y - (radius / 2), 0),
                    x1 = Math.Min(e.X + (radius / 2), pbBackground.Width),
                    y1 = Math.Min(e.Y + (radius / 2), pbBackground.Height);
                Bitmap bm = (Bitmap)pbBackground.Image; //Get the bitmap (assuming it is stored that way)
                using (Graphics g = Graphics.FromImage(bm))
                {
                    Pen p = new Pen(Color.Black, radius);
                    g.DrawLine(p, x0, y0, x1, y1);
                    p.Dispose();
                }


                pbBackground.Refresh();
于 2012-05-24T17:29:43.783 回答