110,579
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace MergeImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int curTime;
private int lastTime;
private Bitmap[] img = new Bitmap[11];
private bool isStart = true;
private int curSecond = 0;
private void Form1_Load(object sender, EventArgs e)
{
img[0] = new Bitmap(@"image/0.png");
img[1] = new Bitmap(@"image/1.png");
img[2] = new Bitmap(@"image/2.png");
img[3] = new Bitmap(@"image/3.png");
img[4] = new Bitmap(@"image/4.png");
img[5] = new Bitmap(@"image/5.png");
img[6] = new Bitmap(@"image/6.png");
img[7] = new Bitmap(@"image/7.png");
img[8] = new Bitmap(@"image/8.png");
img[9] = new Bitmap(@"image/9.png");
img[10] = new Bitmap(@"image/point.png");
}
//显示时间
Bitmap ShowTime(int h,int m, int s)
{
Bitmap[] tex = new Bitmap[8];
tex[0] = img[h/10];
tex[1] = img[h%10];
tex[2] = img[img.Length - 1];
tex[3] = img[m/10];
tex[4] = img[m%10];
tex[5] = img[img.Length - 1];
tex[6] = img[s/10];
tex[7] = img[s%10];
return MergeImage(tex);
}
//多张图片合并一张,不考虑效率问题
public Bitmap MergeImage(Bitmap[] tex)
{
if (tex.Length == 0) return null;
//定义新图的宽高
int width = 0, height = 0;
for (int i = 0; i < tex.Length; i++)
{
//新图的宽度
width += tex[i].Width;
if (i > 1)
{
//新图的高度,这里筛选为最高
if (tex[i].Height > tex[i - 1].Height)
height = tex[i].Height;
}
else height = tex[i].Height; //只有一张图
}
//初始Bitmap
Bitmap bitmap = new Bitmap(width, height);
int x = 0;
for (int i = 0; i < tex.Length; i++)
{
if (i > 0)
{
x += tex[i-1].Width;
}
for (int j = 0; j < tex[i].Width; j++)
{
for (int k = 0; k < tex[i].Height; k++)
{
Color color = tex[i].GetPixel(j, k);//获取原图片像素
bitmap.SetPixel(x + j, k, color);//设置新图片像素
}
}
}
return bitmap;
}
private void btnStart_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text)) return;
if (isStart)
curSecond = Convert.ToInt32(textBox1.Text);
isStart = true;
//用线程显示时间
new Thread(delegate ()
{
while (isStart)
{
curTime = Environment.TickCount;
if (curTime - lastTime >= 1000) //1秒更新
{
lastTime = Environment.TickCount;
curSecond--;
int hour = curSecond/3600;
int min = curSecond%3600/60;
int sec = curSecond%3600%60;
this.Invoke((EventHandler) delegate
{
pictureBox1.Image = ShowTime(hour, min, sec); //将时间更新到控件上
if (btnStart.Enabled)
btnStart.Enabled = false;
});
}
}
}).Start();
}
private void btnPause_Click(object sender, EventArgs e)
{
isStart = false;
btnStart.Enabled = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
btnStart.Enabled = true;
isStart = false;
curSecond = Convert.ToInt32(textBox1.Text);
}
}
}
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace MergeImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string curTime;
private string lastTime;
private Bitmap[] img = new Bitmap[11];
private void Form1_Load(object sender, EventArgs e)
{
img[0] = new Bitmap(@"image/0.png");
img[1] = new Bitmap(@"image/1.png");
img[2] = new Bitmap(@"image/2.png");
img[3] = new Bitmap(@"image/3.png");
img[4] = new Bitmap(@"image/4.png");
img[5] = new Bitmap(@"image/5.png");
img[6] = new Bitmap(@"image/6.png");
img[7] = new Bitmap(@"image/7.png");
img[8] = new Bitmap(@"image/8.png");
img[9] = new Bitmap(@"image/9.png");
img[10] = new Bitmap(@"image/point.png");
//用线程显示时间
new Thread(delegate()
{
while (true)
{
curTime = DateTime.Now.ToString("hh:mm:ss");
if (curTime != lastTime) //只有时间秒数改变的情况下,才更新
{
lastTime = curTime;
this.Invoke((EventHandler) delegate
{
pictureBox1.Image = ShowTime(); //将时间更新到控件上
});
}
}
}).Start();
}
//显示时间
Bitmap ShowTime()
{
Bitmap[] tex = new Bitmap[curTime.Length];
for (int i = 0; i < curTime.Length; i++)
{
string s = curTime.Substring(i, 1);
if (s == ":")
tex[i] = img[img.Length-1];
else
tex[i] = img[Convert.ToInt32(s)];
}
return MergeImage(tex);
}
//多张图片合并一张,不考虑效率问题
public Bitmap MergeImage(Bitmap[] tex)
{
if (tex.Length == 0) return null;
//定义新图的宽高
int width = 0, height = 0;
for (int i = 0; i < tex.Length; i++)
{
//新图的宽度
width += tex[i].Width;
if (i > 1)
{
//新图的高度,这里筛选为最高
if (tex[i].Height > tex[i - 1].Height)
height = tex[i].Height;
}
else height = tex[i].Height; //只有一张图
}
//初始Bitmap
Bitmap bitmap = new Bitmap(width, height);
int x = 0;
for (int i = 0; i < tex.Length; i++)
{
if (i > 0)
{
x += tex[i-1].Width;
}
for (int j = 0; j < tex[i].Width; j++)
{
for (int k = 0; k < tex[i].Height; k++)
{
Color color = tex[i].GetPixel(j, k);//获取原图片像素
bitmap.SetPixel(x + j, k, color);//设置新图片像素
}
}
}
return bitmap;
}
}
}