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)
rw.Generate()
rw.Close()
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")
Else
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.ReferencedAssemblies.AddRange(references)
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?