百叶窗效果显示图像

Posted on   努力成长静待花开  阅读(408)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  Bitmap类的GetPixel和SetPixel方法

  public Color GetPixel (int x,int y)    //获取bitmap图像中指定像素的颜色

  public void SetPixel (int x, int y,Color color)  //设置bitmap图像中指定像素的颜色

实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private void button1_Click(object sender, EventArgs e)
{
    Bitmap myBitmap = (Bitmap)this.BackgroundImage.Clone();     //使用窗体背景的副本创建Bitmap对象
    int intWidth = myBitmap.Width;                              //记录图片的宽度
    int intHeight = myBitmap.Height/20;                         //记录图片的制定高度
    Graphics myGraphics = this.CreateGraphics();                //创建窗体的Graphics对象
    myGraphics.Clear(Color.WhiteSmoke);                         //使用指定颜色清空窗体背景
    Point[] myPoints=new Point[20];                             //定义数组
    for (int i = 0; i < 20; i++)                                //记录百叶窗个节点的位置
    {
        myPoints[i].X = 0;
        myPoints[i].Y = i * intHeight;
    }
    Bitmap bitmap=new Bitmap(myBitmap.Width,myBitmap.Height);   //创建Bitmap对象
    for (int m = 0; m < intHeight; m++)                        
    {
        for (int n = 0; n < 20; n++)
        {
            for (int i = 0; i < intWidth; i++)
            {
                bitmap.SetPixel(myPoints[n].X + i, myPoints[n].Y + m,
                    myBitmap.GetPixel(myPoints[n].X + i, myPoints[n].Y + m));
            }
        }
        this.Refresh();                                         //绘制无效
        this.BackgroundImage = bitmap;                          //显示百叶窗效果
        System.Threading.Thread.Sleep(100);                     //线程挂起
    }
}

  

努力加载评论中...
点击右上角即可分享
微信分享提示