Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB9.0 VB10.0
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
 
        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#.
 
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 27-Sep-12 10:03am
rspercy6526.2K
Edited 27-Sep-12 12:37pm
v2
Comments
Sergey Alexandrovich Kryukov at 27-Sep-12 16:35pm
   
It's certainly VB.NET. OP just needs more adequate topic tags.
--SA
rspercy65 at 28-Sep-12 3:39am
   
The Using...End Using is recognized in VB

1 solution

Rate this: bad
good
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.
 
—SA
  Permalink  
Comments
rspercy65 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2014
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