Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

VB
Private Overloads Sub Refresh(tag As String)
       Dim profile As New Profile(tag)
       Dim list As List(Of Hero) = profile.HeroList

       Me.panelHeroList.Controls.Clear()
       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
           Else
               Dim bmp As New Bitmap(myStream)
               btn.BackgroundImage = bmp
               btn.BackgroundImageLayout = ImageLayout.None
           End If

           AddHandler btn.Click, AddressOf btn_Click

           panelHeroList.Controls.Add(btn)

           System.Math.Max(System.Threading.Interlocked.Increment(index), index - 1)
       Next

   End Sub



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

C#
private void Refresh(string tag)
        {
            Profile profile = new Profile(tag);
            List<Hero> list = profile.HeroList;

            this.panelHeroList.Controls.Clear();
            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);

                panelHeroList.Controls.Add(btn);

                index++;
            }


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

1 solution

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.

—SA
 
Share this answer
 
Comments
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 vb.net, 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900