简单实现winform图片幻灯片效果

前台添加一个转载图片的pannel、一个装载数字的pannel、timer控件就可以了。

由于我这个项目是通过webservice获取二进制流的图片格式,你如果需要用,只需要修改相应的代码就可以了

1 private int productID;
2 //private decimal price;
3 private int focusID = 0;//图片展示当前iD
4 private byte[][] bytes;
5 public int ProductID
6 {
7 get { return productID; }
8 set { productID = value; }
9 }
10 private void pictureBox1_Click(object sender, EventArgs e)
11 {
12 this.Close();
13 }
14
15 private void ProductView_Load(object sender, EventArgs e)
16 {
17 SRMProduct.ProductService productService = new SRMProduct.ProductService();
18 SRMProduct.T_Product tp = new SRMProduct.T_Product();
19 tp = productService.GetProductDetails(productID);
20 if (tp != null)
21 {
22 label3.Text = tp.DisplayName;
23 label4.Text = tp.SupplyPrice.ToString();
24 bytes = tp.ImgArray;
25 ShowFlashPictures();
26 }
27 }
28
29 private void ShowFlashPictures()
30 {
31 panel1.Controls.Clear();
32 int xp = 0;
33 for (int i = 0; i < bytes.Length; i++)
34 {
35 Label lbl = new Label();
36 lbl.Text = (i+1).ToString();
37 lbl.Tag = i;
38 if (i != focusID)
39 {
40 lbl.BackColor = Color.Black;
41 lbl.ForeColor = Color.White;
42 }
43 else
44 {
45 lbl.BackColor = Color.Black;
46 lbl.ForeColor = Color.Red;
47 }
48
49 lbl.Width = 12;
50 lbl.Height = 12;
51 lbl.Cursor = Cursors.Hand;
52 //lbl.Click += new System.EventHandler(this.Lbl_Click);
53 lbl.MouseEnter += new System.EventHandler(this.Lbl_Click);
54 xp = xp + 20;
55 lbl.Location = new Point(xp, 0);
56 panel1.Controls.Add(lbl);
57 }
58 panel2.Controls.Clear();
59 PictureBox pb = new PictureBox();
60 pb.BackgroundImage = Image.FromStream(BytesToStream(bytes[focusID]));
61 pb.Size = new System.Drawing.Size(340, 460);
62 pb.BackgroundImageLayout = ImageLayout.Stretch;
63 panel2.Controls.Add(pb);
64 focusID++;
65 if (focusID > 4)
66 {
67 focusID = 0;
68 }
69 }
70
71 private void Lbl_Click(object sender, EventArgs e)
72 {
73
74 Label lbl = (Label)sender;
75 focusID = int.Parse(lbl.Tag.ToString());
76 //this.timer1.Enabled = false;
77 }
78
79 private Stream BytesToStream(byte[] bytes)
80 {
81 Stream stream = new MemoryStream(bytes);
82 return stream;
83 }
84
85 private void timer1_Tick(object sender, EventArgs e)
86 {
87 ShowFlashPictures();
88 }
posted @ 2011-03-03 16:09  徐文峰  阅读(2416)  评论(0编辑  收藏  举报