2010-04-25 61 views
0

最近研究vb.net,但我仍然沒有想出如何從資源中刪除圖像或可執行文件。 如果我把一個圖像,並添加到資源,我會提取 C:\ foto.jpg 我該怎麼辦? 在此先感謝,我爲我的VB知識道歉是英語VB.NET幫助資源

回答

0

你可以使用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 
+0

對象參照的文ce未設置爲對象的實例 line Dim Buffer(Stream.Length)As Byte – user325439 2010-04-25 16:30:54

+0

發生這種情況是因爲您沒有指定嵌入資源的正確名稱。例如,如果當前程序集名爲'AssemblyName',並且在根目錄下嵌入了'image.jpg',則資源的名稱將爲'AssemblyName.image.jpg'。用反射器打開生成的程序集並查看嵌入的資源部分,看看準確的名稱是什麼。 – 2010-04-25 16:35:55

+0

還是什麼都沒有,反正我也會包含可執行文件。埃克「 我爲我的無知道歉,但你可以發佈詳細的指南?? 在此先感謝 – user325439 2010-04-25 18:39:01