Hi, Recently the study vb.net but I still have not figured out how do I remove an image or an executable from the resource. If I put an image and add to the resource and I would extract C: \ foto.jpg how can I do? Thanks in advance, I apologize for my vb knowledge is that English
A:
You could use the GetManifestResourceStream method to get the embedded image as a stream, read this stream in a buffer and write it to the disc using the WriteAllBytes method:
Using Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("AssemblyName.image.jpg")
Dim Buffer(Stream.Length) As Byte
Stream.Read(Buffer, 0, Buffer.Length)
File.WriteAllBytes("image.jpg", Buffer)
End Using
This should work for small files, but if you have some very big files embedded inside your assembly it would be better to read them in chunks to avoid consuming lots of memory:
Using stream As Stream = Assembly.GetExecutingAssembly.GetManifestResourceStream("AssemblyName.image.jpg")
Using fileStream As FileStream = File.Create("image.jpg")
Dim bytesRead As Integer
Dim buf As Byte() = New Byte(1023) {}
Do While (bytesRead = stream.Read(buf, 0, buf.Length) > 0)
Dim actual As Byte() = New Byte(bytesRead - 1) {}
Buffer.BlockCopy(buf, 0, actual, 0, actual.Length)
fileStream.Write(actual, 0, actual.Length)
Loop
End Using
End Using
Darin Dimitrov
2010-04-25 16:17:24
An object reference not set to an instance of an objectlineDim Buffer (Stream.Length) As Byte
2010-04-25 16:30:54
This happens because you didn't specify the correct name of the embedded resource. For example if the current assembly is called `AssemblyName` and you have `image.jpg` embedded at the root, the name of the resource would be `AssemblyName.image.jpg`. Open up the generated assembly with reflector and look at the embedded resources section to see what's the precise name.
Darin Dimitrov
2010-04-25 16:35:55
Still nothing, anyway I would also include executable. Exe "I apologize for my ignorance but can you post a detailed guide??Thanks in advance
2010-04-25 18:39:01