2014-01-09 91 views
0

我试图在Windows Phone应用程序加载图像

这里从嵌入式资源加载图像是我的项目设置:

模块1 = Phone应用程序

单词数= ClassLibrary.dll

Module1调用Module2为手机应用程序创建所有数据对象。

当Module2创建对象时,我想从资源加载图像。

资源是“为Default.png”,并保存在“图像”目录(生成操作=嵌入的资源,复制到输出目录=一直拷贝)

我使用的代码产生一个异常

ex = {"The request is not supported. "} 

下面是代码:

private void LoadImage() 
{ 
    Assembly curAssembly = null; 

    curAssembly = Assembly.GetExecutingAssembly(); 

    string [] names = curAssembly.GetManifestResourceNames(); 

    // names[0] = "Storage.Images.Default.png" 
    // so I know I am using the correct name 

    Stream resStream = curAssembly.GetManifestResourceStream("Storage.Images.Default.png"); 

    BitmapImage bitMapImage = new BitmapImage(); 

    try 
    { 
    bitMapImage.SetSource(resStream); 
    } 
    catch (Exception ex) 
    { 
    Debug.WriteLine(ex.ToString()); 
    } 
} 

你能帮助一个新手了呢? 感谢

回答

0

想象一下出...

我用加载资源错了名字......

从阵列中使用的名称返回在此调用

string [] names = curAssembly.GetManifestResourceNames(); 

它完美的作品

0

步骤来获取工作:

首先Appraoch

  1. 在VS的项目上单击右键,选择 “添加新项
  2. 从 “常规”标签选择“资源文件
  3. 打开resource_file.resx您刚刚添加到您的项目中并选择“添加现有项目”,然后选择您的图像。
  4. 然后然后在您的LoadImage方法,这样做:

    私人无效的LoadImage()
    {
              Drawing.Bitmap的BitmapImage = resource_file.name_of_your_image;
    }

注意:在这段代码中,我假定你选择你的资源文件的名称是 “resource_file”

第二种方法

System.Resources.ResourceManager rm = new System.Resources.ResourceManager(this.GetType().Assembly.GetName().Name + ".resource_file", this.GetType().Assembly); 
System.Drawing.Bitmap bmp= (System.Drawing.Bitmap)rm.GetObject("Baba"); 

或使用可使用System.Resources.ResourceReader等方法

+0

谢谢你的方法...我会给这些尝试...但他们不回答我的问题,为什么我无法加载资源,即使资源在模块内,如GetManifestResourceNames所示? – user3174075