繁体   English   中英

VB.NET帮助资源

[英]VB.NET Help Resource

最近研究vb.net,但我仍未弄清楚如何从资源中删除映像或可执行文件。 如果我放入图像并添加到资源中,则我将提取C:\\ foto.jpg该怎么办? 在此先感谢您,我对vb的了解是英语

您可以使用GetManifestResourceStream方法将嵌入式图像作为流获取,在缓冲区中读取此流,然后使用WriteAllBytes方法将其写入光盘:

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

这应该适用于较小的文件,但是如果您的程序集中嵌入了一些非常大的文件,则最好分块读取它们,以避免占用大量内存:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM