日期:2014-05-20  浏览次数:20981 次

C# GDI+绘图 为什么会有重影?
private void button1_Click(object sender, EventArgs e)
 {
            int height = 400, width = 600;
            Bitmap image = new Bitmap(height, width);
            Graphics g = Graphics.FromImage(image);//创建一个对象
            g.Clear(Color.White);
            Pen mypen = new Pen(new SolidBrush(Color.Blue), 1);
            g.DrawLine(mypen, 100, 80, 100, 340);
            this.panel1.BackgroundImage = image;
}

就是上面这样一段简单的代码,预期是画出一条竖线,可是它却出现了两条,第一次尝试GDI+,求解

------解决方案--------------------
设置panel1的backgroundimagelayout属性,如果是tile,图像会平铺,看上去像两条。
------解决方案--------------------


可以考虑采用“双缓存”技术:


        private void button1_Click(object sender, EventArgs e)
        {
            //int height = 400, width = 600;
            //Bitmap image = new Bitmap(height, width);
            Bitmap image = new Bitmap(DisplayRectangle.Width, DisplayRectangle.Height);

            using (Graphics g = Graphics.FromImage(image))
            {
                g.Clear(Color.White);
                Pen mypen = new Pen(new SolidBrush(Color.Blue), 1);
                g.DrawLine(mypen, 100, 80, 100, 340);
            }

            this.panel1.BackgroundImage = image;

        }