在 Windows Mobile 中绘制线条
本文关键字:绘制 Windows Mobile | 更新日期: 2023-09-27 18:35:24
我正在使用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();
最简单的
方法是使用 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();