【问题标题】:Use images of my resources - Compact Framework使用我的资源的图像 - Compact Framework
【发布时间】:2012-12-24 18:08:06
【问题描述】:

我将图像添加到我的项目资源 (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 流) 在 System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci,BindingFlags invokeAttr,Binder binder,Object参数, CultureInfo 文化,布尔 isBinderDefault,程序集调用者,布尔 verifyAccess、StackCrawlMark 和 stackMark)在 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化) 在 System.Reflection.ConstructorInfo.Invoke(Object[] 参数) 中 System.Resources.ResourceReader.CreateResource(类型 objType,类型 [] ctorParamTypes, Object[] ctorParameters) 在 System.Resources.ResourceReader.LoadBitmap(Int32 typeIndex) 在 System.Resources.ResourceReader.LoadObjectV2(Int32 位置, 资源类型代码&类型代码)在 System.Resources.ResourceReader.LoadObject(Int32 位置, 资源类型代码&类型代码)在 System.Resources.RuntimeResourceSet.GetObject(字符串键,布尔值 在 System.Resources.ResourceManager.GetObject(String name, CultureInfo文化)在 Icons_Control.Properties.Resources.get_glass_empty() 在 Icons_Control.ListItem.set_CompletitionStatus(eCompletionStatus 值) 在 Icons_Control.ListItem..ctor() 中 Icons_Control.ListItem..ctor(eItemType type) 在 Icons_Control.MainForm.menuItem3_Click(Object sender, EventArgs e) in System.Windows.Forms.MenuItem.OnClick(EventArgs e) 在 System.Windows.Forms.Menu.ProcessMnuProc(控制 ctlThis,WM wm,Int32 wParam, Int32 lParam) in System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam) 在 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()

【问题讨论】:

  • TargetInvocationException 通常有一个InnerException,可以提供更多细节。
  • 我很确定我的答案,只是好奇你的图片有多大,你分配给多少图片框?
  • @Alan 大约 60 Kb,我不能显示超过 5 张图片。

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


【解决方案1】:

我的猜测是您的内存或其他资源不足。图片资源有点危险。每次获得资源时,都会创建一个新资源。您可能只想创建一个 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 应用来说非常重要的是不要浪费内存。

【讨论】:

  • 您还可以将 Alan 的代码包装在 try...catch 块中,以显示更用户友好的消息并继续。
猜你喜欢
  • 2021-03-21
  • 2010-10-09
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多