[原创]winform_PC宴会图片抽奖/文字抽奖

14年6月 好友结婚

14年4月左右知道他们婚礼由迎宾照抽奖的环节

问我有没有可以用的抽奖软件

我网上找了一会儿,就放弃了,自己做一个更快不是?

14年6月,PC宴会图片抽奖软件成功使用

---

操作示意图:

图片文字模式2选1

1.文字模式只需要指定txt文本文件路劲

会按序滚动txt文本文件内容进行抽奖

2.图片模式需要设定

清晰图片文件夹

缩略图文件夹

缩略倍数

后,生成缩略图即可

---

部分代码:

main.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.IO;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Windows.Forms;
 10 using System.Threading;
 11 
 12 namespace partyGifts
 13 {
 14     public partial class Main : Form
 15     {
 16         public Main()
 17         {
 18             InitializeComponent();
 19         }
 20 
 21         private void Main_Load(object sender, EventArgs e)
 22         {
 23             this.cbType.SelectedIndex = 0;
 24         }
 25 
 26         private void cbType_SelectedIndexChanged(object sender, EventArgs e)
 27         {
 28             if (this.cbType.SelectedItem.ToString().Equals("图片"))
 29             {
 30                 this.btnSource.Text = "图片文件夹(不含子目录)";
 31                 parms.type = "图片";
 32                 parms.path = "";
 33             }
 34             else if (this.cbType.SelectedItem.ToString().Equals("文字"))
 35             {
 36                 this.btnSource.Text = "文字文件(*.txt)";
 37                 parms.type = "文字";
 38                 parms.path = "";
 39             }
 40         }
 41 
 42         private void btnSource_Click(object sender, EventArgs e)
 43         {
 44             if (this.cbType.SelectedItem.ToString().Equals("图片"))
 45             {
 46                 FolderBrowserDialog fbd = new FolderBrowserDialog();
 47                 if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 48                 {
 49                     this.lblSource.Text = fbd.SelectedPath;
 50                     parms.path = fbd.SelectedPath;
 51                 }
 52             }
 53             else if (this.cbType.SelectedItem.ToString().Equals("文字"))
 54             {
 55                 OpenFileDialog ofd = new OpenFileDialog();
 56                 ofd.Filter = "文本文件(*.txt)|*.txt";
 57                 ofd.Multiselect = false;
 58                 if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 59                 {
 60                     this.lblSource.Text = ofd.FileName;
 61                     parms.path = ofd.FileName;
 62                 }
 63             }
 64         }
 65 
 66         private void btnBgColor_Click(object sender, EventArgs e)
 67         {
 68             ColorDialog cd = new ColorDialog();
 69             cd.FullOpen = true;
 70             if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 71             {
 72                 this.btnBgColor.BackColor = cd.Color;
 73                 parms.bgColor = cd.Color;
 74             }
 75         }
 76 
 77         private void btnFontColor_Click(object sender, EventArgs e)
 78         {
 79             ColorDialog cd = new ColorDialog();
 80             cd.FullOpen = true;
 81             if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 82             {
 83                 this.btnFontColor.BackColor = cd.Color;
 84                 parms.fontColor = cd.Color;
 85             }
 86         }
 87 
 88         private void numTime_ValueChanged(object sender, EventArgs e)
 89         {
 90             parms.timeNum = (int)this.numTime.Value;
 91         }
 92 
 93         private void btnStart_Click(object sender, EventArgs e)
 94         {
 95             if (!parms.path.Equals(""))
 96             {
 97                 gifts g = new gifts();
 98                 g.Show();
 99             }
100             else
101             {
102                 MessageBox.Show("请选择数据来源!");
103             }
104         }
105 
106         private void txtFTxt_TextChanged(object sender, EventArgs e)
107         {
108             parms.firstTxt = this.txtFTxt.Text;
109         }
110 
111         private void btnFPic_Click(object sender, EventArgs e)
112         {
113             OpenFileDialog ofd = new OpenFileDialog();
114             ofd.Filter = "图片文件(*.jpg,*.jpeg,*.gif,*.bmp,*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
115             ofd.Multiselect = false;
116             if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
117             {
118                 this.lblFPic.Text = ofd.FileName;
119                 parms.firstPicUrl = ofd.FileName;
120             }
121             else
122             {
123                 this.lblFPic.Text = "";
124                 parms.firstPicUrl = "";
125             }
126         }
127 
128         private void cb1_CheckedChanged(object sender, EventArgs e)
129         {
130             parms.cb1 = this.cb1.Checked;
131         }
132 
133         private void btnSlPath_Click(object sender, EventArgs e)
134         {
135             FolderBrowserDialog fbd = new FolderBrowserDialog();
136             if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
137             {
138                 this.lblSl.Text = fbd.SelectedPath;
139                 parms.thumbPath = fbd.SelectedPath;
140             }
141             else
142             {
143                 this.lblSl.Text = "";
144                 parms.thumbPath = "";
145             }
146         }
147 
148         private void btnDoSl_Click(object sender, EventArgs e)
149         {
150             MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
151             DialogResult dr = MessageBox.Show("该处理会删除缩略图文件夹后重新生成文件夹\n是否继续?", "提示信息", messButton);
152             if (dr == DialogResult.OK)
153             {
154                 if (new DirectoryInfo(parms.thumbPath).Exists)
155                 {
156                     foreach (FileInfo item in new DirectoryInfo(parms.thumbPath).GetFiles())
157                     {
158                         item.Delete();
159                     }
160                 }
161                 
162                 if (parms.path != "" && parms.type == "图片")
163                 {
164                     DirectoryInfo di = new DirectoryInfo(parms.path);
165                     FileInfo[] fiLst = di.GetFiles();
166                     foreach (FileInfo fi in fiLst)
167                     {
168                         parms.MakeThumbnail(fi.FullName, parms.thumbPath + "\\" + fi.Name, System.Windows.Forms.SystemInformation.VirtualScreen.Width / (int)numBS.Value, System.Windows.Forms.SystemInformation.VirtualScreen.Height / (int)numBS.Value, "W");
169                     }
170                     MessageBox.Show("缩略图生成完成,请继续操作");
171                 }
172                 else
173                 {
174                     MessageBox.Show("请选择图片模式,且填写完图片文件夹路径");
175                 }
176             }
177         }
178     }
179 }

gifts.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.IO;
 10 using System.Runtime.InteropServices;
 11 
 12 namespace partyGifts
 13 {
 14     public partial class gifts : Form
 15     {
 16 
 17         [DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
 18         public extern static void ShowCursor(int status);
 19 
 20         public gifts()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         private Timer timer = new Timer();
 26 
 27         private void gifts_KeyUp(object sender, KeyEventArgs e)
 28         {
 29             switch (e.KeyData)
 30             {
 31                 case Keys.Escape:
 32                     timer.Stop();
 33                     parms.txtList.Clear();
 34                     this.Close();
 35                     return;
 36                 case Keys.Space:
 37                     parms.playSts = !parms.playSts;
 38                     if (!parms.playSts)
 39                     {
 40                         if (parms.type.Equals("图片") && parms.cb1)
 41                         {
 42                             timer.Stop();
 43                             this.lblFont.Visible = false;
 44                             this.picBox.ImageLocation = parms.txtList[selectIndex];
 45                             this.picBox.Refresh();
 46                         }
 47                         else if (parms.type.Equals("图片") && parms.thumbPath != "")
 48                         {
 49                             this.picBox.ImageLocation = parms.path + "\\" + this.imgNameAllLst[selectIndex];
 50                         }
 51                     }
 52                     else { 
 53                         if (parms.type.Equals("图片") && parms.cb1)
 54                         {
 55                             timer.Stop();
 56                             this.lblFont.Visible = false;
 57                         }
 58                     }
 59                     break;
 60                 default: break;
 61             }
 62             goOn();
 63         }
 64 
 65         private void goOn()
 66         {
 67             if (parms.playSts)
 68             {
 69                 //getOut
 70                 if (-1 != selectIndex)
 71                 {
 72                     parms.txtList.RemoveAt(selectIndex);
 73                     if (parms.type.Equals("图片"))
 74                     {
 75                         imgNameLst.RemoveAt(selectIndex);
 76                         imgNameAllLst.RemoveAt(selectIndex);
 77                     }
 78                     if (0 == parms.txtList.Count)
 79                     {
 80                         timer.Stop();
 81                         parms.txtList.Clear();
 82                         MessageBox.Show("全部中奖,抽奖结束");
 83                         this.Close();
 84                         return;
 85                     }
 86                     if (0 != selectIndex)
 87                     {
 88                         selectIndex--;
 89                     }
 90                     else
 91                     {
 92                         selectIndex = parms.txtList.Count;
 93                     }
 94                 }
 95                 else
 96                 {
 97                     if (parms.type.Equals("图片"))
 98                         this.picBox.Visible = true;
 99                     if (parms.type.Equals("图片") && parms.cb1)
100                     {
101                         this.picBox.Controls.Add(this.lblFont);
102                     }
103                 }
104                 timer.Start();
105             }
106             else
107             {
108                 timer.Stop();
109             }
110         }
111         private Label lblFont = new Label();
112         private void gifts_Load(object sender, EventArgs e)
113         {
114             timer.Interval = parms.timeNum;
115             timer.Tick += timer_Tick;
116             lblFont.ForeColor = parms.fontColor;
117             this.BackColor = parms.bgColor;
118             lblFont.Font = new Font(lblFont.Font, FontStyle.Bold);
119             lblFont.Font = new Font(this.Font.FontFamily, 150);
120             lblFont.BackColor = Color.Transparent;
121             lblFont.Size = new Size(this.Width, this.Height);
122             lblFont.Top = 0;
123             lblFont.Left = 0;
124             lblFont.Text = parms.firstTxt;
125             lblFont.TextAlign = ContentAlignment.MiddleCenter;
126 
127             this.picBox.Left = 0;
128             this.picBox.Top = 0;
129             this.picBox.Size = new Size(this.Width, this.Height);
130 
131             if (!parms.firstPicUrl.Equals(string.Empty))
132             {
133                 //lblFont.Image = new Bitmap(parms.firstPicUrl);
134                 //this.lblFont.Visible = false;
135                 this.picBox.Visible = true;
136                 this.picBox.ImageLocation = parms.firstPicUrl;
137                 this.picBox.Controls.Add(lblFont);
138             }
139             else
140             {
141                 this.picBox.Visible = false;
142                 this.Controls.Add(lblFont);
143             }
144             if (parms.type.Equals("文字"))
145             {
146                 StreamReader sr = new StreamReader(new FileStream(parms.path, FileMode.Open), Encoding.UTF8);
147                 string lstTmp = sr.ReadLine();
148                 while (lstTmp != null)
149                 {
150                     parms.txtList.Add(lstTmp);
151                     lstTmp = sr.ReadLine();
152                 }
153             }
154             else
155             {
156                 DirectoryInfo di;
157                 if (parms.thumbPath != "")
158                 {
159                     di = new DirectoryInfo(parms.thumbPath);
160                 }
161                 else
162                 {
163                     di = new DirectoryInfo(parms.path);
164                 }
165                 FileInfo[] fiLst = di.GetFiles();
166                 foreach (FileInfo fi in fiLst)
167                 {
168                     parms.txtList.Add(fi.FullName);
169                     imgNameLst.Add(fi.Name.Replace(fi.Extension, ""));
170                     imgNameAllLst.Add(fi.Name);
171                 }
172                 //if (parms.firstPicUrl.Equals(string.Empty) && !parms.cb1)
173                     //this.picBox.InitialImage = new Bitmap(parms.txtList[0]);
174             }
175             ShowCursor(0);
176         }
177         private List<string> imgNameLst = new List<string>();
178         private List<string> imgNameAllLst = new List<string>();
179         private int selectIndex = -1;
180         void timer_Tick(object sender, EventArgs e)
181         {
182             selectIndex = ++selectIndex % parms.txtList.Count;
183             if (parms.type.Equals("文字"))
184             {
185                 this.lblFont.Text = parms.txtList[selectIndex];
186             }
187             else
188             {
189                 this.lblFont.Visible = false;
190                 if (!parms.cb1)
191                 {
192                     this.picBox.ImageLocation = parms.txtList[selectIndex];
193                 }
194                 else
195                 {
196                     this.picBox.Image = null;
197                     this.lblFont.Visible = true;
198                     this.lblFont.Text = this.imgNameLst[selectIndex];
199                 }
200                 //this.picBox.Image = imgLst[selectIndex];
201             }
202         }
203 
204         private void gifts_FormClosing(object sender, FormClosingEventArgs e)
205         {
206             parms.playSts = false;
207             //this.picBox.InitialImage.Dispose();
208             this.picBox.Dispose();
209             ShowCursor(1);
210         }
211 
212     }
213 }

parms.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Linq;
  5 using System.Text;
  6 
  7 namespace partyGifts
  8 {
  9     public class parms
 10     {
 11         public static string path = "";
 12         public static string type = "";
 13         public static int timeNum = 150;
 14         public static Color bgColor;
 15         public static Color fontColor;
 16         public static List<string> txtList = new List<string>();
 17         public static bool playSts = false;
 18         public static string firstTxt = "尽请期待";
 19         public static string firstPicUrl = string.Empty;
 20         public static bool cb1 = false;
 21         public static string thumbPath = "";
 22 
 23         /**/
 24         /// <summary> 
 25         /// 生成缩略图 
 26         /// </summary> 
 27         /// <param name="originalImagePath">源图路径(物理路径)</param> 
 28         /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 
 29         /// <param name="width">缩略图宽度</param> 
 30         /// <param name="height">缩略图高度</param> 
 31         /// <param name="mode">生成缩略图的方式</param>     
 32         public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
 33         {
 34             Image originalImage = Image.FromFile(originalImagePath);
 35 
 36             int towidth = width;
 37             int toheight = height;
 38 
 39             int x = 0;
 40             int y = 0;
 41             int ow = originalImage.Width;
 42             int oh = originalImage.Height;
 43 
 44             switch (mode)
 45             {
 46                 case "HW"://指定高宽缩放(可能变形)                 
 47                     break;
 48                 case "W"://指定宽,高按比例                     
 49                     toheight = originalImage.Height * width / originalImage.Width;
 50                     break;
 51                 case "H"://指定高,宽按比例 
 52                     towidth = originalImage.Width * height / originalImage.Height;
 53                     break;
 54                 case "Cut"://指定高宽裁减(不变形)                 
 55                     if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
 56                     {
 57                         oh = originalImage.Height;
 58                         ow = originalImage.Height * towidth / toheight;
 59                         y = 0;
 60                         x = (originalImage.Width - ow) / 2;
 61                     }
 62                     else
 63                     {
 64                         ow = originalImage.Width;
 65                         oh = originalImage.Width * height / towidth;
 66                         x = 0;
 67                         y = (originalImage.Height - oh) / 2;
 68                     }
 69                     break;
 70                 default:
 71                     break;
 72             }
 73 
 74             //新建一个bmp图片 
 75             Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
 76 
 77             //新建一个画板 
 78             Graphics g = System.Drawing.Graphics.FromImage(bitmap);
 79 
 80             //设置高质量插值法 
 81             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
 82 
 83             //设置高质量,低速度呈现平滑程度 
 84             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
 85 
 86             //清空画布并以透明背景色填充 
 87             g.Clear(Color.Transparent);
 88 
 89             //在指定位置并且按指定大小绘制原图片的指定部分 
 90             g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
 91                 new Rectangle(x, y, ow, oh),
 92                 GraphicsUnit.Pixel);
 93 
 94             try
 95             {
 96                 //以jpg格式保存缩略图 
 97                 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
 98             }
 99             catch (System.Exception e)
100             {
101                 throw e;
102             }
103             finally
104             {
105                 originalImage.Dispose();
106                 bitmap.Dispose();
107                 g.Dispose();
108             }
109         }
110 
111     }
112 }

---

CSDN代码及程序下载:

 http://download.csdn.net/detail/wangxsh42/8391929

使用者:下载后,可以直接使用:

partyGifts\bin\Debug\partyGifts.exe

转载于:https://www.cnblogs.com/wangxinsheng/p/4245426.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值