Draw lines in windows mobile

578 Views Asked by At

I am using windows mobile and want to draw line or write names in screen?

I searched GDI+ or related but not able to get it done. How can I draw lines? My code on mouse down event is as below but its not smooth and gaps in line not proper line draw.

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();
1

There are 1 best solutions below

0
On

The simplest way to do this is to use a Graphics class instance (although you may have to play with the positioning to get the line placement exactly correct for the width that you're looking for):

                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();