Solved

cant get bitmap to be used as embedded resource, code supplied

Posted on 2004-08-10
3
669 Views
Last Modified: 2013-12-03
hi, i cant get 2 bitmaps to be seen as embedded resources.
i copied the bitmaps out of syncfusions datagridbuttons demo.
i added them by  using the solution explorer > add existing item.
i also tried adding them by File > Add Existing item
i set the files' build action to embedded resource.
i also tried the build action on content.
i have spent a lot of time on this and its as if theres a connection i need to make before it can see
the files as embedded resources. i did compile it first before i ran it in debug mode.
i just get the error

An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll
Additional information: 'null' is not a valid value for 'stream'.

in this section:
  Dim streamBitMap As System.IO.Stream = assemblyExecution.GetManifestResourceStream(BitMapName)
  Dim bitmapEmbedded As Bitmap = New Bitmap(streamBitMap)

here is all the code, you need 2 bitmaps, one called buttonface.bmp and buttonfacepressed.bmp
but its all available at www.tapeus.co.za/download/eeicon.zip

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents Icons As ImageList
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private _buttonFace As Bitmap
    Private _buttonFacePressed As Bitmap

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Attempt1()
        Attempt2()
    End Sub
    Private Sub Attempt1()
        Dim strm As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("buttonface.bmp")
        _buttonFace = New Bitmap(strm)
        strm = Me.GetType().Assembly.GetManifestResourceStream("buttonfacepressed.bmp")
        _buttonFacePressed = New Bitmap(strm)
    End Sub
    Private Sub Attempt2()
        _buttonFace = GetEmbeddedBitmap("buttonface.bmp")
        _buttonFacePressed = GetEmbeddedBitmap("buttonfacepressed.bmp")
    End Sub
    Public Function GetEmbeddedBitmap(ByVal BitMapName As String) As Bitmap
        Dim assemblyExecution As System.Reflection.Assembly = Me.GetType.Assembly.GetEntryAssembly()
        Dim namespaceName As String = assemblyExecution.GetName().Name.ToString()
        BitMapName = namespaceName & "." & BitMapName
        Dim streamBitMap As System.IO.Stream = assemblyExecution.GetManifestResourceStream(BitMapName)
        Dim bitmapEmbedded As Bitmap = New Bitmap(streamBitMap)
        Return bitmapEmbedded
    End Function
End Class
0
Comment
Question by:jxharding
  • 2
3 Comments
 
LVL 27

Assisted Solution

by:planocz
planocz earned 250 total points
ID: 11762079
Use with a image list
  Private Sub InitializeIcons()
        Dim executing_assembly As System.Reflection.Assembly = Me.GetType.Assembly.GetEntryAssembly()
        ' Get the namespace.
        Dim my_namespace As String = executing_assembly.GetName().Name.ToString()
        Me.ImageList1.Images.AddStrip(Image.FromStream(Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(my_namespace +" MYbmp.bmp")))
    End Sub
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11763186
Hi jxharding,

Your attemps where correct, you just forgot to set the build action for your bitmaps to embedded resource.
Right click on your bitmap choose properties and set the build action to embedded resource
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 250 total points
ID: 11763221
I just tried it with this code and it works perfectly

    Private Sub Attempt1()
        Dim CurrentAssembly As Reflection.Assembly = _
        Reflection.Assembly.GetExecutingAssembly
        Dim BitMapStream As IO.Stream = _
        CurrentAssembly.GetManifestResourceStream( _
        "WindowsApplication3.smiley.BMP")
        Dim SmileyBitmap As Drawing.Bitmap = New Bitmap(BitMapStream)
        PictureBox1.Image = SmileyBitmap
    End Sub

if you try this without setting the build action to embedded resource you will get your error

An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll
Additional information: 'null' is not a valid value for 'stream'.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question