使用我的资源图像 - Compact Framework

时间:2013-01-10 11:06:46

标签: c# image windows-mobile compact-framework picturebox

我将一个Image添加到我的项目资源(Windows Mobile 6.1)。我想使用此图像来设置我的表单中的一些PictureBox的PictureBox.Image属性。我尝试以下代码:

pictureBox1.Image = Properties.Resources.my_image;
pictureBox2.Image = Properties.Resources.my_image;
pictureBox3.Image = Properties.Resources.my_image;

...

pictureBoxN.Image = Properties.Resources.my_image;

问题在于,有时图像仅显示在某些PictureBox中(当我尝试设置图像时,我得到TargetInvocationException),而不是全部。为什么?我该如何解决这个问题?

修改

InnerException的StackTrace:

  

在Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)中   System.Drawing.Bitmap._InitFromMemoryStream(MemoryStream mstream)中   System.Drawing.Bitmap..ctor(Stream stream)in   System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo   rtci,BindingFlags invokeAttr,Binder binder,Object parameters,   CultureInfo culture,Boolean isBinderDefault,Assembly caller,Boolean   verifyAccess,StackCrawlMark& stackMark)in   System.Reflection.RuntimeConstructorInfo.Invoke(的BindingFlags   invokeAttr,Binder binder,Object []参数,CultureInfo文化)   在System.Reflection.ConstructorInfo.Invoke(Object []参数)中   System.Resources.ResourceReader.CreateResource(类型objType,类型[]   ctorParamTypes,Object [] ctorParameters)in   System.Resources.ResourceReader.LoadBitmap(Int32 typeIndex)in   System.Resources.ResourceReader.LoadObjectV2(Int32 pos,   ResourceTypeCode&安培; typeCode)in   System.Resources.ResourceReader.LoadObject(Int32 pos,   ResourceTypeCode&安培; typeCode)in   System.Resources.RuntimeResourceSet.GetObject(String key,Boolean   在System.Resources.ResourceManager.GetObject(String name,   文化信息文化)   Icons_Control.Properties.Resources.get_glass_empty()in   Icons_Control.ListItem.set_CompletitionStatus(eCompletionStatus值)   在Icons_Control.ListItem..ctor()中   Icons_Control.ListItem..ctor(eItemType类型)in   Icons_Control.MainForm.menuItem3_Click(Object sender,EventArgs e)in   System.Windows.Forms.MenuItem.OnClick(EventArgs e)in   System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis,WM wm,Int32   System.Windows.Forms.Form.WnProc中的wParam,Int32 lParam)(WM wm,Int32   wParam,Int32 lParam)in   System.Windows.Forms.Control._InternalWnProc(WM wm,Int32 wParam,   Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)中的Int32 lParam)   在System.Windows.Forms.Application.Run(Form fm)中   Icons_Control.Program.Main()

1 个答案:

答案 0 :(得分:2)

我的猜测是你的内存或其他资源不足。图像资源有点危险。每次获得资源时,都会创建一个新资源。您可能只想创建一个my_image实例,并且您可能希望在使用它之后将其丢弃。

Image myImage = Properties.Resources.my_image;

pictureBox1.Image = myImage;
pictureBox2.Image = myImage;
pictureBox3.Image = myImage;
pictureBox4.Image = myImage;
...
pictureBoxN.Image = myImage;

// Later on when you are done using it
myImage.Dispose();

对大多数CF应用程序来说非常重要,不要浪费内存。

相关问题