C#代码:
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); // Note that the name for the embedded resource is case sensitive and must match the file name. Bitmap bmp = new Bitmap(asm.GetManifestResourceStream(asm.GetName().Name + "." + "pic.png")); e.Graphics.DrawImage(bmp,5,8,new Rectangle(0,bmp.Width,bmp.Height),GraphicsUnit.Pixel)
GetManifestResourceStream得到的Stream是null(VB中为Nothing)的解决其实最主要的是在 将图片资源的属性设置为"嵌入的资源":"属性-生成操作-嵌入的资源" 注意GetManifestResourceStream函数的参数:asm.GetName().Name获得的只是当前命名空间的名称, 如果在"pic.png"图片在当前工程的Photo目录下,那么该参数就该写成 asm.GetName().Name + "."+ "Photo" + "." + " pic.png"。
VB代码
Function GetResourceImage(ByVal aStrFileName As String) As Image Dim MyAsm As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly() Dim aStream As System.IO.Stream = MyAsm.GetManifestResourceStream(MyAsm.GetName().Name & "." & aStrFileName) If aStream Is Nothing Then Return Nothing Else Return New Drawing.Bitmap(aStream) End If End Function