在C#中如何使用資源的方法

來源:互聯網
上載者:User

標籤:blog   http   ar   io   os   使用   sp   檔案   art   


總結一個在C#中如何使用資源的方法:
一:使用本地檔案
1、將本地要加入的資源文本(視頻,圖片,文本或其它)加入項目,比如我們現在加入一個up.bmp的圖片到項目中,且放在檔案夾Resources下面,
2、將up.bmp的產生操作設定為"內嵌資源"
3、讀取資源:
Assembly assembly = Assembly.GetEntryAssembly();
//不知為什麼,上面的有時候不行,可以這樣寫: Assembly assembly = this.GetType().Assembly;
System.IO.Stream stream = assembly.GetManifestResourceStream("OutlookBar.Resources.up.bmp");
//OutlookBar表示命名空間,Resources表示檔案夾名稱;
//如查直接放在項目的根目錄下,則寫為: assembly.GetManifestResourceStream("OutlookBar.up.bmp");
Bitmap upImage = (Bitmap)Image.FromStream(stream);


第二種方法:建立一個.resx檔案(TestProject.resx),直接加入up.bmp檔案,
讀取方法: Bitmap upImage = TestProject.up;


第三種方法:單獨建一個資源檔來儲存up.bmp檔案

1:將寫入:
if (!File.Exists("TestProject.resources"))
File.Create("TestProject.resources").Dispose();

using (IResourceWriter rw = new ResourceWriter("TestProject.resources"))
{
Image img = Image.FromFile(@"D:\up.bmp");
rw.AddResource("up.bmp", img);
rw.Close();
}


2、讀取檔案
using (ResourceSet rs = new ResourceSet("TestProject.resources"))
{
var image = (Bitmap)rs.GetObject("down.bmp");
pictureBox1.Image = image;
rs.Close();
}

NET的資源並不限於.resx檔案,可以採用任意儲存形式
:具體見 http://www.cnblogs.com/artech/archive/2010/12/16/Reource_framewok_01.html

在C#中如何使用資源的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.