Click here to Skip to main content
15,900,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody, I'm having problems on reading some resources (images file .jpg) in my generated application from the CodeDOM.
I really don't understand how all it work , I hope you can give me help.

I'm trying to compilate a Test.Vb File with my source VB.NET code inside using Codedom ;
I use this code in one btncompile Button to compilate and add a 'xp.jpg' image to my generated assembly :

Dim Success As Boolean
        Success = CompileExecutable(Application.StartupPath & "\Test.Vb")
        Dim byteStream As New FileStream(Application.StartupPath & "\xp.JPEG", FileMode.Open)
        Dim bytes(CInt(byteStream.Length - 1)) As Byte
        byteStream.Read(bytes, 0, CInt(byteStream.Length))

        Using rw As New ResourceWriter(".\xp.resources")
            rw.AddResource("\xp", byteStream)
        End Using
        MessageBox.Show("Test.vb " & Success.ToString & ControlChars.NewLine)

And this is the CompileExecutable function :

Dim sourceFile As System.IO.FileInfo = New System.IO.FileInfo(sourceName)
        Dim provider As System.CodeDom.Compiler.CodeDomProvider = Nothing
        Dim compileOk As Boolean = False
        If sourceFile.Extension.ToUpper(Globalization.CultureInfo.InvariantCulture) = ".CS" Then
            provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp")
        ElseIf sourceFile.Extension.ToUpper(Globalization.CultureInfo.InvariantCulture) = ".VB" Then
            provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("VisualBasic")
            Console.WriteLine("Source file must have a .cs or .vb extension")
        End If

        If Not provider Is Nothing Then
            Dim exeName As String = String.Format("{0}{1}.exe", sourceFile.Directory, sourceFile.Name.Replace(".", "_"))

            Dim cp As System.CodeDom.Compiler.CompilerParameters = New System.CodeDom.Compiler.CompilerParameters()
            Dim references() As String = {"System.Windows.Forms.dll", "Microsoft.VisualBasic.dll", "System.dll", "System.Drawing.dll"}
            cp.CompilerOptions = "/target:winexe /m:Form1"

            cp.GenerateExecutable = True
            cp.OutputAssembly = exeName  
            cp.GenerateInMemory = False
            cp.TreatWarningsAsErrors = False
            cp.EmbeddedResources.Add(Application.StartupPath & "\xp.resources")
            Dim cr As System.CodeDom.Compiler.CompilerResults = provider.CompileAssemblyFromFile(cp, sourceName)

I used EmbeddedResources.Add and seems it works because now my generated file is more big and so it contains my resource 'xp.resources' file inside.

Now, how can I read my 'xp.resources' into the generated assembly ?
I'm receiving a 'null value return' when I try to load my image with this code :

Dim _imageStream As Stream
   Dim _assembly As [Assembly]

   _assembly = [Assembly].GetExecutingAssembly()
   _imageStream =  _assembly.GetManifestResourceStream("My.Resources.xp.JPEG")

Dim xp As System.Drawing.Image
xp = System.Drawing.Image.FromStream(_imageStream)
      Me.BackgroundImage = xp

Why the value returns null ? How can I avoid this or there is another way to read this embedded resource?
Updated 14-Jul-14 0:02am
Member 8078971 14-Jul-14 6:09am    
nobody could help me I guess.. ?

If someone read I think the problem is in this line :

_imageStream = _assembly.GetManifestResourceStream("My.Resources.xp.JPEG")

I don't understand if 'My.Resources' is embedded in the assembly so the error could be here. Seems that CodeDOM canno't use the 'my' prefix (any attempt of using it in others parts of code is failed) but I don't understand where my xp.resources is saved . How can I replace it if this is the error??


Is my point of view but I'm here to ask your help, I could obvioulsy wrong.
CHill60 14-Jul-14 7:09am    
If you change "My.Resources.xp.JPEG" to "YourNameSpace.xp.JPEG" does that help ... obviously replacing "YourNameSpace" with whatever your namespace is
Member 8078971 14-Jul-14 8:44am    
So I'm in the wrong way because the NameSpace I have in my code is 'My.Resources'. Anyway I took his content from another project (the resources.designer.vb file under resources.resx) which contains the same image that I want to load from my first project (the Test.VB file).
Could be this a wrong procedure? In the Windows Form (Test.VB file) that I should compile I have : 1. Form.Designer content 2.Resource.Designer content took from another project (under My.Resources NameSpace) 3.My main code
I assume I wrong procedure to write/read data but seriously I googled everything and readed tons of threads and I cannot make this to work
CHill60 14-Jul-14 9:25am    
Is the resource definitely named "xp.JPEG"? - you must use the exact case and spelling
Member 8078971 14-Jul-14 10:31am    
yes it is the same, i also changed it two times..I'm really stuck on it, aslo tried with resourcemanager and nothing new, only more errors. By assuming my way to create and write the 'xp.resources' file from the 'xp.JPEG' file is correct, the only thing could be wrong is the reader function. But it seems correct, so I guess the error is into the 'My.Resources' NameSpace (in effect, either I have it in my code or I haven't it, I get the same error). So there is something other I should import with my .resources file , such as a .resx file (?)
Or I wrong all the procedure, I really don't know because I tried everything

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