2012-06-30 85 views
0

我做了一个启动画面程序,它从一个文件夹加载图片并间隔更改图像,所以效果是 - 跳动的心脏。但我想加载这个图像不是从文件夹,而是在程序集中的资源。我不知道如何加载这个位图数组,你能帮助我吗?这是我的代码:从C#资源获取位图数组

private static string imagefile; 
    private static int selected = 0; 
    private static int begin; 
    private static int end = 0; 
    // private static string path = SplashDemo.Properties.Resources.ResourceManager.GetStream(); 
    private static string path = "C:/Users/Desktop/Desktop/New folder"; 
    private static string[] folderFile = null; 

    [STAThread ()] 
    private static void Main () 
    { 

     Splasher.Splash = new SplashScreen (); 
     Splasher.ShowSplash (); 
    // TIMER 
     System.Timers.Timer timer = new System.Timers.Timer(); 
     timer.Interval = 50; 
     timer.Enabled = true; 
     timer.AutoReset = true; 
     timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick); 
    // END TIMER 


    // GET ASSEMBLY FILS 
     System.Reflection.Assembly thisExe; 
     thisExe = System.Reflection.Assembly.GetExecutingAssembly(); 
     string[] resources = thisExe.GetManifestResourceNames(); 
     // Bitmap[] image = new Bitmap[string]; 
     // Bitmap[] imagew = new Bitmap[10]; 
     // Bitmap image = new Bitmap(file); 
    // string[] embeddedResources = Assembly.GetExecutingAssembly().GetManifestResourceNames(); 
     // END GET ASSEMBLY FILS 


     // GET DIRECTORY FILES 
     string[] part1 = null, part2 = null, part3 = null; 
     part1 = Directory.GetFiles(path, "*.png"); 
     part2 = Directory.GetFiles(path, "*.jpeg"); 
     part3 = Directory.GetFiles(path, "*.bmp"); 
     folderFile = new string[part1.Length + part2.Length + part3.Length]; 
     Array.Copy(part1, 0, folderFile, 0, part1.Length); 
     Array.Copy(part2, 0, folderFile, part1.Length, part2.Length); 
     Array.Copy(part3, 0, folderFile, part1.Length + part2.Length, part3.Length); 
     bool beating = true; 
     selected = 0; 
     begin = 0; 
     end = folderFile.Length; 
     while (beating == true) 
     { 
      ImageListener.Instance.ReceiveImage(string.Format(@"{0}", imagefile)); 
     } 


    } 

    public static void timer_Tick(object sender, EventArgs e) 
    { 
     ChangeImage(); 
    } 

    public static void ChangeImage() 
    { 
     if (selected == folderFile.Length - 1) 
     { 
      selected = 0; 
      imagefile = (folderFile[selected]); 
     } 
     else 
     { 
      selected = selected + 1; 
      imagefile = (folderFile[selected]); 
     } 
    } 

回答

0
  1. 图像添加到您的项目
  2. 右键单击图像 - 单击属性
  3. 在属性窗口中,将生成操作设置为嵌入的资源

例如,如果您添加的图片在程序集MyAssemblyName中名为Resources的文件夹中被称为MyPictureName.jpg,则需要执行以下操作:

var stream = Assembly.GetExecutingAssembly() 
    .GetManifestResourceStream("MyAssemblyName.Resources.MypictureName.jpg"); 

pictureBox1.Image = new Bitmap(stream); 
+0

谢谢,但我加了“Resources”,所以代码是“MyAssemblyName.Resources.MypictureName.jpg” – user1493114

+0

太好了。我已经更新了答案。现在是为你工作吗?如果是,请不要忘记接受答案。 –

+0

完成。再次感谢:) – user1493114