Click here to Skip to main content
13,096,660 members (96,396 online)
Rate this:
Please Sign up or sign in to vote.
See more:
This is the VB version and below is the C# version. C# version works. I used (in the VB version) Dim myStream As Stream = ... and I used GetEntryAssembly() to no avail. Still get the same MessageBox to trap the Null exception that gets thrown.

 Private Overloads Sub Refresh(tag As String)
        Dim profile As New Profile(tag)
        Dim list As List(Of Hero) = profile.HeroList
        Dim index As Integer = 0
        For Each hero As Hero In list
            btn.Location = New Point(2 + index * 110, 2)
            btn.Size = New Size(108, 128)
            btn.Text = [String].Format("{0} {1}( {2} )", list(index).Level, list(index).Name, list(index).ParagonLevel)
            btn.ImageAlign = ContentAlignment.MiddleCenter
            btn.TextAlign = ContentAlignment.BottomCenter
            btn.Tag = hero
            Dim myStream As Stream = Assembly.GetEntryAssembly().GetManifestResourceStream(hero.HeadPicture)
            If myStream Is Nothing Then
                MessageBox.Show("Image cannot be opened...Sorry", "System Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
                Dim bmp As New Bitmap(myStream)
                btn.BackgroundImage = bmp
                btn.BackgroundImageLayout = ImageLayout.None
            End If
            AddHandler btn.Click, AddressOf btn_Click
            System.Math.Max(System.Threading.Interlocked.Increment(index), index - 1)
    End Sub

It works in C# just fine, and NO I'm not switching to C#.

private void Refresh(string tag)
            Profile profile = new Profile(tag);
            List<Hero> list = profile.HeroList;
            int index = 0;
            foreach (Hero hero in list)
                Button btn = new Button();
                btn.Location = new Point(2 + index * 110, 2);
                btn.Size = new Size(108, 128);
                btn.Text = String.Format("{0} {1}( {2} )", list[index].Level, list[index].Name, list[index].ParagonLevel);
                btn.ImageAlign = ContentAlignment.MiddleCenter;
                btn.TextAlign = ContentAlignment.BottomCenter;
                btn.Tag = hero;

                using (Stream myStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(hero.HeadPicture))
                    Bitmap bmp = new Bitmap(myStream);
                    btn.BackgroundImage = bmp;
                    btn.BackgroundImageLayout = ImageLayout.None;
                btn.Click += new EventHandler(btnHero_Click);

Posted 27-Sep-12 10:03am
Updated 27-Sep-12 12:37pm
Sergey Alexandrovich Kryukov 27-Sep-12 16:35pm
It's certainly VB.NET. OP just needs more adequate topic tags.
rspercy65 28-Sep-12 3:39am
The Using...End Using is recognized in VB

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Unfortunately, I cannot see what is the difference between posted code and the C# code, because you did not show the C# code. It's more likely that the difference is in the arguments of GetManifestResourceStream. I can also suspect that executing assemblies are different, or the resources. By the way, this code is sensitive to the place where it is called, because it is can be called in a different assembly, which would make in not working, apparently. I would rather advise to use GetEntryAssembly whenever possible and put resource in the entry assembly of each application. Yes, I know that this is not 100% suitable for all cases.

Now, is it an image file? Of some known type? Chances are, you don't need to use this resource stream at all. At the same time, it's also likely you really need it. For example, you put the application icon in the manifest resource (otherwise the Shell would not see it), and then you would need to reuse the same image as the window or form icon. Then you really would need to read the manifest resource.

However, let's assume for a minute that this is not the case, by any chance. If so, you would be much better off with .res resources. It's the best to add an image as "Add existing file". The file will be added to the project (it will be also copied in the project file tree, so make sure you don't create a double copy) and reference it in the resource file. If the file is, say *.png (many types are supported), you will find the auto-generated static property of the type Bitmap in the file generated and placed as a child node of the resource file. All would you need is just using this static property: it's always ready to use, initialized, etc.

Sorry that the answer is probably incomplete, but, as I said above, some information is still missing.

rspercy65 29-Sep-12 14:51pm
Is there anybody of the 9 million plus user in here that knows the correct way to use
Assembly.GetExecutingAssembly()GetManifestResourceStream(embeddedResource) in any Visual studio version(2005 to 2012) in VB. I have googled around 30+ pages and no one seams to know how to do this. I have used Ildasm on my app to get the correct assembly and file name. I can retrieve it but it will not pass to the myStream variable. In all versions of, this is all the same. I am beginning to think that it does not work at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100