sjk.Properties.Resources._1051 用反射Reflection.MemberInfo 试试看,我没试过。
解决方案 »
- 请教:C#winform中DATAGRID,如果在表格最后一列放如两个以上的LinkLabel,并添加单击事件
- 关于做网站的几点疑问
- printDocument1.Print() 提示 Unknown error (0xe0434f4d)
- c# combobox 自动匹配问题
- java 加密 c#解密问题!
- 各位大哥高手们!给小弟说下WebServices怎样加密!
- --==我才开始转VS2005,不知道在2005中如何将aspx文件与cs文件分开,将编译好的类库供aspx调用==--
- 高手帮帮忙吧,我快郁闷死了
- C#中如何把大写数字转换为阿拉伯数字,各位大侠请帮忙,在线等
- C# 一个checkbox实现全选和取消(就是为了确认收货用的,商品处于卖家发货这个状态是才可以有确认收货)
- Click Once自动更新弹出后,我点跳过后,下次登陆就不提示了,请问怎么解决?
- 关于MMS彩信发送
我试了也不行的.
直接这么使用没有问题,现在我想把sjk.Properties.Resources._1051送给一个字符串变量,
string str1 = "sjk.Properties.Resources._1051";
这样就比较灵活,比如我要用pictureBox1.Image来显示下一个图片,sjk.Properties.Resources._1052.
我只需要改变这个变量str1="sjk.Properties.Resources._1052";
但是把它送给控件
pictureBox1.Image =str1;(这一句调试通不过)
应该如何改.
为谢!
我觉得楼主的问题很明确啊,sjk.Properties.Resources._1051 谁都能猜到是Image类型的(.ico,.jpg,.bmp等)只不过这个问题不知道如何在C#下解决。要是在VBScript或JavaScript中就比较简单,用Eval()函数就能实现。
如果你的资源是个Bitmap,用pictureBox1.Image = new Bitmap("sjk.Properties.Resources._1051")就可以了
如果是其它类型的资源,就生成相应实例就可以了。
中的"sjk.Properties.Resources._1051"用一个字符串变量来代替.应该如何做.
比如:str1="sjk.Properties.Resources._1051"那么:pictureBox1.Image =new Bitmap(str1),这么写却不对,应该如何写
sjk.Properties.Resources._1051是资源文件中的图吧,怎么找到他的绝对路径是问题所在,我感觉
查一下MSDN就可以了。
class Program
{
static void Main(string[] args)
{
Type t = typeof(Resources);
Resources r1 = new Resources(); System.Reflection.PropertyInfo pi = t.GetProperty("_1052");
int w2=(int)pi.GetValue(r1,null); Console.WriteLine(w2);
}
} class Resources
{
public int _1051
{
get { return 1051; }
} public int _1052
{
get { return 1052; }
} public int _1053
{
get { return 1053; }
} }
如果_1052什么的是Resources的静态static成员,可以参照下面的例子,我就不具体写了 http://topic.csdn.net/t/20041210/09/3632423.html
...
using System.Reflection;
using System.Resources;namespace sjk
{
... private void button3_Click(object sender, EventArgs e)
{
string str1 = "_1052"; //在str1中只存放资源图像的索引名称
this.pictureBox1.Image = GetResourceByKey("sjk.ImageResource", str1);
}
public static Image GetResourceByKey(string resourceName, string key)
{
// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager(resourceName, Assembly.GetExecutingAssembly()); rm.IgnoreCase = true; // Retrieve the value of the string resource named
Image strValue = (Image)rm.GetObject(key); rm = null; return strValue;
}}
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;namespace FormTestA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = FormTestA.Properties.Resources.Image1;
pictureBox1.Image = GetImageFromResource("Image1");
pictureBox1.Image = GetImageFromResource("FormTestA.Properties.Resources.Image1");
pictureBox1.Image = GetImageFromResource("FormTestA.Properties.Resources", "Image1");
//pictureBox1.Image = GetImageFromResource("Properties.Resources", "Image1"); //错误, 需要类型的完全限定名称
} private Image GetImageFromResource(string fullname)
{
if (fullname == null) throw new ArgumentNullException("fullname"); int index; return (index = fullname.LastIndexOf('.')) == -1 ? GetImageFromResource("FormTestA.Properties.Resources", fullname)
: GetImageFromResource(fullname.Substring(0, index), fullname.Substring(index + 1));
} private Image GetImageFromResource(string type, string resource)
{
return (Image)Type.GetType(type).GetProperty(resource, BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
}
}
}
在用 sjk.Properties.Resources.str1
pictureBox1.Image =sjk.Properties.Resources.str1
你这样做试试。
pictureBox1.Image =(Image)Properties.Resources.ResourceManager.GetObject(pic); 我们就是这样做的。。 给分吧 呵呵
pictureBox1.Image =(Image)Properties.Resources.ResourceManager.GetObject(pic);
可以得