简体   繁体   English

.NET 2中资源的奇怪问题

[英]Strange issue with resources in .NET 2

In VS 2010 SP 1 I've created WinForms application for .Net Framework 2. And I try to get icon from resources and assign it to form icon: 在VS 2010 SP 1中,我为.Net Framework 2创建了WinForms应用程序。我尝试从资源中获取图标并将其分配给表单图标:

this.Icon = global::tester.Properties.Resources.icon_gray;

Icon resource icon_gray.ico was added from VS designer to Resources.resx in my developer PC Windows 7 x86 with .NET Framework 3.5 SP1, and .NET 4. 图标资源icon_gray.ico已从VS设计器添加到我的开发人员PC Windows 7 x86(带.NET Framework 3.5 SP1和.NET 4)中的Resources.resx。

But got the below error in Windows XP SP 3 with only .NET Framework 2 SP 2 但是Windows XP SP 3 with only .NET Framework 2 SP 2Windows XP SP 3 with only .NET Framework 2 SP 2中出现以下错误

Error : 错误

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ComponentModel.Win32Exception: Параметр задан неверно
   at System.Drawing.Icon.Initialize(Int32 width, Int32 height)
   at System.Drawing.Icon..ctor(SerializationInfo info, StreamingContext context)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._SerializationInvoke(Object target, SignatureStruct& declaringTypeSig, SerializationInfo info, StreamingContext context)
   at System.Reflection.RuntimeConstructorInfo.SerializationInvoke(Object target, SerializationInfo info, StreamingContext context)
   at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context)
   at System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder)
   at System.Runtime.Serialization.ObjectManager.DoFixups()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Resources.ResourceReader.DeserializeObject(Int32 typeIndex)
   at System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode)
   at System.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode)
   at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase, Boolean isString)
   at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase)
   at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream)
   at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture)
   at tester.Properties.Resources.get_icon_gray()
   at tester.Form1.button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

How to fix this? 如何解决这个问题? Edit: With png: 编辑:使用png:

pictureBox1.Image = global::tester.Properties.Resources.icon_gray1;

everything is fine 一切顺利

For those who have encountered this problem, make sure you check the icon in the project properties window and also, the icon property in all of you forms. 对于遇到此问题的用户,请确保检查项目属性窗口中的图标以及所有表单中的图标属性。 They all have to be 32x32 in order to work in Windows XP. 它们都必须是32x32才能在Windows XP中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM