自定义绘制图片的一个小技巧

要在picturebox中自由绘制图片,一开始将绘制函数写入在Paint中

public void ff_Paint(object sender, PaintEventArgs e)
 {
  CreatShow(Graphics.FromHwnd(fpic.Handle));  //这样卡爆了.
 }

void fpic_Paint(object sender, PaintEventArgs e)
{
CreatShow(e.Graphics); //这样卡爆了.
}

 

结果加载了几张图片后,程序就卡爆了,而且经常不显示,说明不管是针对picturebox控件本身,还是它的父载体panel, 显示销量都极差.

后来思考了很久 换了一种方式, 

//先创建一个位图
            Bitmap bp = new Bitmap(fpic.Width, fpic.Height); // 用于缓冲输出的位图对象
            Graphics g = Graphics.FromImage(bp);

//再将内容绘制在位图之上
            for (int i = 0; i < Picspathary.Count; i++)
            {
                Drawone(g, Picspathary[i], i);
            }

//最后
            fpic.Image = bp;

这样之后, 一点都不卡了.

//网上看到的 说加了这句话效果好

        public Form1()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true); 

        }

  

 

posted on 2015-02-06 14:19  飞翔蚂蚁  阅读(193)  评论(0编辑  收藏  举报

导航