C#读取资源文件的图片
作者:C/S框架网  发布日期:2013-07-17 17:41:34
  C#读取资源文件的图片


IO.Stream strm = GetType().Assembly.GetManifestResourceStream("namespace.picture.bmp");
img = new Bitmap(Image.FromStream(strm), width, height);
strm.Close();

如果是通过项目属性->资源嵌入,直接这样以用就可以了(假设资源名字叫MyBmp):

  Bitmap myBmp = WindowsApplication1.Properties.Resources.MyBmp;

btnClose.Image =(Bitmap)Bitmap.FromStream(this.GetType().Assembly.GetManifestResourceStream ("Buttom_Close.bmp"));

范例:

首先在项目里先开一个Form窗体..并且加入一个图片文件...
并且将图片文件→属性→建置动作→改成"内嵌资源",如下图所示:

贴图图片

C# Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;//要记得using
namespace WindowsApplication1
{
   public partial class GetResourceStream : Form
   {
      public GetResourceStream()
      {
         InitializeComponent();
      }
      private void GetResourceStream_Load(object sender, EventArgs e)
      {
         Assembly asm = Assembly.GetExecutingAssembly();
         string name = asm.GetName().Name;
         Bitmap bmp = new Bitmap(asm.GetManifestResourceStream(name ".f6.jpg"));//加载图片资源
         this.pictureBox1.Image = bmp;
      }
   }
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898


上一篇 下一篇