public void Draimg()
{
Image img = Image.FromFile(@"Img\1.png");
Point[] point ={
new Point(){X=-img.Height/2,Y=-img.Width/2},
new Point(){X=-img.Width/2,Y=0},
new Point(){X=0,Y=-img.Height/2},
new Point(){X=0,Y=0}
};
for (int i = 0; i < panel1.Controls.Count; i++)
{
if(panel1.Controls[i] is Panel)
{
Panel p= panel1.Controls[i] as Panel;
Graphics g = p.CreateGraphics();
//将bitmap中对应的1/4给绘制出来
g.DrawImage(img, point[i]);
}
}
//int i=-1;
//foreach (Control item in panel1.Controls)
//{
// if (item is Panel)
// {
// i++;
// Panel p = item as Panel;
// Graphics g = p.CreateGraphics();
// //将bitmap中对应的1/4给绘制出来
// g.DrawImage(img,point[i]);
// }
// else
// {
// return;
// }
//}
}
原因我已经找到!
Panel控件 里面大概有这么一个集合
panel1.Controls 控件集合
这可能是堆栈的问题
当你在panel1控件添加子控件时
添加顺序为
panel2
panel4
panel5
那么实际读取顺序是
panel5
panel4
panel2
所以要看好你添加控件的顺序
个人觉得是g.DrawImage这行有错,为了测试,把这句换成简单地输出字符串看
e.Graphics.DrawString(i.ToString(), new Font("宋体", 30), Brushes.Red, 10, 10);
绘制出来的结果有什么问题?