在.NET中嵌入和使用资源文件

来源:互联网 发布:多玩魔盒mac版国服 编辑:程序博客网 时间:2024/06/05 08:00
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

嵌入和使用资源文件,以下是全部源代码:
  
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Reflection;
  using System.IO;
  using System.Diagnostics;
  
  namespace ResourceDemo
  {
   /// <summary>
   /// Summary description for Form1.
   /// </summary>
   public class Form1 : System.Windows.Forms.Form
   {
   ArrayList pics;
   private System.Windows.Forms.GroupBox groupBox1;
   private System.Windows.Forms.PictureBox pBox;
   private System.Windows.Forms.Button btnDisplay;
   private System.Windows.Forms.TextBox txtInfo;
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ComponentModel.Container components = null;
  
   public Form1()
   {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
  
   // Instantiate our ArrayList
   pics = new ArrayList();
   }
  
   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   protected override void Dispose( bool disposing )
   {
   if( disposing )
   {
   if (components != null)
   {
   components.Dispose();
   }
   }
   base.Dispose( disposing );
   }
  
   #region Windows Form Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
   this.pBox = new System.Windows.Forms.PictureBox();
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.btnDisplay = new System.Windows.Forms.Button();
   this.txtInfo = new System.Windows.Forms.TextBox();
   this.groupBox1.SuspendLayout();
   this.SuspendLayout();
   //
   // pBox
   //
   this.pBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.pBox.Location = new System.Drawing.Point(8, 8);
   this.pBox.Name = "pBox";
   this.pBox.Size = new System.Drawing.Size(264, 272);
   this.pBox.TabIndex = 0;
   this.pBox.TabStop = false;
   this.pBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
   //
   // groupBox1
   //
   this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
   this.txtInfo,
   this.btnDisplay});
   this.groupBox1.Location = new System.Drawing.Point(288, 8);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(192, 264);
   this.groupBox1.TabIndex = 1;
   this.groupBox1.TabStop = false;
   //
   // btnDisplay
   //
   this.btnDisplay.Location = new System.Drawing.Point(48, 24);
   this.btnDisplay.Name = "btnDisplay";
   this.btnDisplay.Size = new System.Drawing.Size(96, 23);
   this.btnDisplay.TabIndex = 0;
   this.btnDisplay.Text = "Display Picture";
   this.btnDisplay.Click += new System.EventHandler(this.button1_Click);
   //
   // txtInfo
   //
   this.txtInfo.Location = new System.Drawing.Point(8, 56);
   this.txtInfo.Multiline = true;
   this.txtInfo.Name = "txtInfo";
   this.txtInfo.ReadOnly = true;
   this.txtInfo.Size = new System.Drawing.Size(176, 200);
   this.txtInfo.TabIndex = 2;
   this.txtInfo.Text = "txtInfo";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(496, 293);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
   this.groupBox1,
   this.pBox});
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.groupBox1.ResumeLayout(false);
   this.ResumeLayout(false);
  
   }
   #endregion
  
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main()
   {
   Application.Run(new Form1());
   }
  
   private void button1_Click(object sender, System.EventArgs e)
   {
   // go to a random picture in our arraylist and
   // display it
   Random generator = new Random();
   Bitmap bmp = pics[ generator.Next(pics.Count) ] as Bitmap;
   if(!(null==bmp))
   {
   pBox.Image = bmp;
   }
   bmp = null;
   generator = null;
   }
  
   private void Form1_Load(object sender, System.EventArgs e)
   {
   Stream imgStream = null;
   Bitmap bmp = null;
  
   // get a reference to the current assembly
   Assembly a = Assembly.GetExecutingAssembly();
  
   // get a list of resource names from the manifest
   string [] resNames = a.GetManifestResourceNames();
  
   // populate the textbox with information about our resources
   // also look for images and put them in our arraylist
   txtInfo.Clear();
  
   txtInfo.Text += String.Format("Found {0} resourcesrn", resNames.Length);
   txtInfo.Text += "----------rn";
   foreach(string s in resNames)
   {
   txtInfo.Text += s + "rn";
   if(s.EndsWith(".bmp"))
   {
   // attach to stream to the resource in the manifest
   imgStream = a.GetManifestResourceStream(s);
   if( !(null==imgStream) )
   {
   // create a new bitmap from this stream and
   // add it to the arraylist
   bmp = Bitmap.FromStream( imgStream ) as Bitmap;
   if( !(null==bmp) )
   {
   pics.Add( bmp );
   }
   bmp = null;
   imgStream.Close();
   imgStream = null;
   }
   }
   }
   txtInfo.Text += "----------rn";
   txtInfo.Text += String.Format("Found {0} Bitmapsrn",
   pics.Count);
   }
   }
  }

 <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 贞子从电视里爬出来怎么办 2个月的婴儿吓到怎么办 排卵日同房了没避孕怕怀孕怎么办 妻子因为我欺骗她要跟我离婚怎么办 老婆用苹果手机共享我的位置怎么办 孕妇餐后2小时血糖偏高怎么办 孕妇血糖餐后2小时数值高怎么办 股市退市的话股民的钱怎么办 美股股票退市了手里的股票怎么办 坐高铁安检时怕把包包弄坏了怎么办 很贵的包包高铁安检怎么办 如果过高铁安检东西被扣留怎么办 邻居家小孩把我家东西弄坏了怎么办 邻居早上6点放音乐扰民怎么办 隔壁楼邻居天天放音乐很吵怎么办 发现老公在卧室安了摄像头怎么办 憋的尿又没有厕所的时候怎么办 一岁宝宝拉屎总是拉出血怎么办 孩子鼻窦炎鼻子堵有白色鼻涕怎么办 家里阳台上老是有蝙蝠趴着怎么办 空调太冷在空调房里该怎么办 分手了怎么办不要挽回要重新吸引 过敏体质没打疫苗的孩子入学怎么办 遇到不认识的小姐姐问我问题怎么办 小孩孑脖子洛忱了痛怎么办 挤奶把乳腺挤肿了不出奶怎么办 遇到好兄弟在背后捅刀子怎么办 面对出轨还不想离婚的老公怎么办 法院执行局把案划错了不承认怎么办 手机nfc功能被手机壳挡住怎么办 飞信短信登录验证码达到上限怎么办 手机自带短信软件没了怎么办 老婆出轨孩子又3个不知道怎么办 骑电动车摔跤小脚趾疼有点肿怎么办 第一次太疼了有心理阴影了怎么办 天梭机械表调了时间忽然慢怎么办 支付宝信用住到酒店没房间怎么办 拳头打在硬上骨头肿了怎么办 领导决策出现了失误你该怎么办 我的直销团队走到瓶颈了怎么办 孕妇8个月便秘大便带血怎么办