提问 c# vs2008

陈杉菜 2019-03-07 05:34:13
刚刚接触c#
想做一个计时器

开始是这样的界面,后来老师给了数码管的位图,打算重新修改一下
目前改成了这个样子


但是对pictureBox完全没有接触过,网上很多资料也看不太懂
就想问一下大佬们,

1.怎么在.cs 文件中实现对pictureBox属性值的更改

2.命名位图的名字是1.bmp 2.bmp……然后计算出秒数和分钟数之后怎样才能直接更改imag属性呢
目前我用的是如下巨长的写法



还有一个问题就是

pictureBox的image属性用的相同的图片显示结果也相同,但出现这种情况是怎么回事
...全文
1604 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
解决了就好,这个论坛有点不好的地方就是无法修改已发的贴,少写了2字,不是说人笨,说的是方法笨,刚学我也一样都是用的最原始笨办法,之后逐渐提升。
陈杉菜 2019-03-11
  • 打赏
  • 举报
回复
引用 24 楼 智者知已应修善业 的回复:
如果觉得笨换个思路就好了,如:
不不不不 没有觉得笨 我的问题已经解决了 谢谢
  • 打赏
  • 举报
回复

https://blog.csdn.net/xianfajushi/article/details/4763800
  • 打赏
  • 举报
回复
如果觉得笨换个思路就好了,如:
侠客刀 2019-03-09
  • 打赏
  • 举报
回复
你写的代码出错了
  • 打赏
  • 举报
回复
引用 14 楼 OrdinaryCoder 的回复:
网上下载的字体是DLL 你把应用程序.exe和字体资源DLL打包到一起就可以了
是这样的!
  • 打赏
  • 举报
回复

陈杉菜 2019-03-09
  • 打赏
  • 举报
回复
引用 12 楼 无情时尚 的回复:
做了一个更新时间的方法,不考虑效率,上代码,已添加注释!
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;
        }
    }
}
我的timer滴答事件是这样写的 可以指点我一下吗
陈杉菜 2019-03-09
  • 打赏
  • 举报
回复
引用 12 楼 无情时尚 的回复:
做了一个更新时间的方法,不考虑效率,上代码,已添加注释!
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;
        }
    }
}
谢谢你呀 我昨天换了另一种笨笨的方法好像成功了 虽然发帖的时候也做出来了,但是效果不太好
OrdinaryCoder 2019-03-09
  • 打赏
  • 举报
回复
网上下载的字体是DLL 你把应用程序.exe和字体资源DLL打包到一起就可以了
陈杉菜 2019-03-09
  • 打赏
  • 举报
回复
引用 10 楼 胖叔叔写代码 的回复:
………………我说句不当说的话这个字体很好下载到你用这个字体用Lable实现节省资源又快捷,非要用图片的原因是什么?
呃我确实没想到这个 可是 这样 指标不治本呀,在这台电脑上能用起来在其他电脑上就用不起来了 不过话说回来 可以直接把字体和 这个应用小程序绑在一起吗 要怎么做呢
无情时尚 2019-03-09
  • 打赏
  • 举报
回复
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);
}
}
}
陈杉菜 2019-03-09
  • 打赏
  • 举报
回复
引用 17 楼 以专业开发人员为伍 的回复:
[quote=引用 14 楼 OrdinaryCoder 的回复:] 网上下载的字体是DLL 你把应用程序.exe和字体资源DLL打包到一起就可以了
是这样的! [/quote] 好像出来点问题 我在网上找到的资源都是ttf文件,没有dll 怎么办呢
陈杉菜 2019-03-09
  • 打赏
  • 举报
回复
引用 17 楼 以专业开发人员为伍 的回复:
[quote=引用 14 楼 OrdinaryCoder 的回复:] 网上下载的字体是DLL 你把应用程序.exe和字体资源DLL打包到一起就可以了
是这样的! [/quote] 谢谢谢谢 关于字体的问题我应该解决了
OrdinaryCoder 2019-03-08
  • 打赏
  • 举报
回复
10楼正解,网上可以下载到数码管字体
  • 打赏
  • 举报
回复
………………我说句不当说的话这个字体很好下载到你用这个字体用Lable实现节省资源又快捷,非要用图片的原因是什么?
assky124 2019-03-08
  • 打赏
  • 举报
回复
可以用类似CSS切图的方式,把这些数字图片全部放到一起生成一张大的图片,然后根据数值获取相应的图片
zy010101 2019-03-08
  • 打赏
  • 举报
回复
太长的那块代码用查表法不就行了!
无情时尚 2019-03-08
  • 打赏
  • 举报
回复


做了一个更新时间的方法,不考虑效率,上代码,已添加注释!

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;
}
}
}
陈杉菜 2019-03-07
  • 打赏
  • 举报
回复
引用 1 楼 左耳边的期盼 的回复:
代码贴出来 看看
https://paste.ubuntu.com/p/ypm9JTtVW3/ 这是代码 中间有些太长了被我注释掉了 运行出来的效果不太理想
加载更多回复(6)

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧