这是一个完整的程序,运行:
图片框如图所选中,
但我不知道图片的来源。图片框中的属性找了,都没有找到运行时候显示出的图片。
我是有一个这程序的文件包的,但我不知道图片的来源。
图片框中的属性找了,都没有找到运行时候显示出的图片。
复制.FORM1.cs内容到新建的代码段里是行不通的,图像文件插入picturebox1中的image和initialimage都是没办法显示的。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 实验4._1
{
public partial class Form1 : Form
{
private int picno;
private Bitmap[] bitmap;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 500;
hScrollBar1.Value = 1000 - 500;
bitmap = new Bitmap[9];
picno = 0;
for (int i =1; i < 9; i++)
{
string file = Application.StartupPath + "\\wolf\\" + i.ToString() + ".jpg";
bitmap[i-1] = new Bitmap(file);
}
} private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
timer1.Interval = 1000-hScrollBar1.Value; } private void button1_Click(object sender, EventArgs e)
{
if (button1.Text =="开始")
{
button1.Text = "停止";
timer1.Enabled = true;
}
else
{
button1.Text ="开始" ;
timer1 .Enabled =false ;
}
} private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = bitmap[picno];
picno = (picno + 1) % 9;
pictureBox1.Left = pictureBox1.Left +10;
if (pictureBox1.Left > this.Width) pictureBox1.Left = 0;
}
}
}
我试着用其他图片显示效果都失败了,比如我在initialmage中插入一组“DG01.bmp”-"DG5.bmp"图像
错误提示这两句: string file = Application.StartupPath + "\\DG0" + i.ToString() + ".bmp";
bitmap[i - 1] = new Bitmap(file);不知是图片插错控件地方了,还是怎么地,无法用自己的图片组来运行。
下面的代码是在程序运行时如何访问 From1.resx 中的这个图片try
{
// 获取程序集中的所有资源文件
System.Reflection.Assembly assembly =
System.Reflection.Assembly.GetExecutingAssembly(); string[] resourceNames =
assembly.GetManifestResourceNames(); // 这个可以查看到很多资源的名字
// string[] resourceNames =
// this.pictureBox1.GetType().Assembly.GetManifestResourceNames(); Console.WriteLine(string.Join("\r\n", resourceNames)); // 在我的程序中输出的是:
// CSDNSamples.Form1.resources
// CSDNSamples.Properties.Resources.resources
// CSDNSamples.Form2.resources // 一般情况下各窗体和自定义控件的资源命名规则是
// AssemblyName.FormName(UserControlName).resources // 我就直接利用名称 CSDNSamples.Form1.resources 获取资源流
System.IO.Stream resourceStream =
assembly.GetManifestResourceStream("CSDNSamples.Form1.resources"); // 通过 resourceStream 创建一个 ResourceReader 对象
System.Resources.ResourceReader reader =
new System.Resources.ResourceReader(resourceStream); System.Collections.IDictionaryEnumerator dic =
reader.GetEnumerator(); while (dic.MoveNext())
{
Console.WriteLine(dic.Key.ToString());
this.BackgroundImage = dic.Value as Image;
} reader.Close();
resourceStream.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
现在图片我找到了,是在DBUG文件夹中,是怎么放进DBUG中的,是手动添加控件中还是编程写出来的?试验成功后回我,谢谢。