![](/img/trans.png)
[英]What is the simplest way to copy a file from local to a shared folder by a PowerShell script?
[英]Copy a PNG file from the Resources to a local folder
我正在為應用程序創建安裝程序,但在將 PNG 文件從我的資源復制到本地文件夾時遇到問題。
我已經嘗試過像File.Copy
, File.WriteAllBytes()
這樣的常用東西,但似乎沒有任何效果。 我只得到錯誤:
位圖無法轉換為 Byte()
If System.IO.File.Exists(FileFolderOther & "\LogoReport.png") = False Then
File.Copy(My.Resources.Logo_Reports, FileFolderOther & "\LogoReport.png", True)
End If
If System.IO.File.Exists(FileFolderOther & "\LogoReport.png") = False Then
File.WriteAllBytes(FileFolderOther & "\LogoReport.png", My.Resources.Logo_Reports)
End If
我只想將文件(PNG、TXT 等)從My.Resources
到本地文件夾。
My.Resources.[SomeImage]
返回一個 Image 對象。
您可以使用Image.Save方法將圖像保存到光盤:
Dim destinationPath = Path.Combine(FileFolderOther, "LogoReport.png")
Using myLogo As Bitmap = My.Resources.Logo_Reports
myLogo.Save("d:\testImage.png", ImageFormat.Png)
End Using
File.Exist()
檢查僅在出於某種原因不想覆蓋同名文件時才需要。 如果該文件存在,它將被無錯覆蓋。
Using
語句允許處理由ResourceManager工廠創建的圖像。 如果您需要存儲該圖像,請將其分配給 Field/Property 並在容器 Form/owner Class 關閉/處置時處置它。
您已經對圖像類型 ( .Png
) 進行了硬編碼。
也許那是該位圖的正確原始格式。 相反,如果您不知道資源圖像(或任何其他圖像)的類型,並且希望保留原始格式,則可以使用Image.RawFormat.Guid屬性派生用於創建位圖的編解碼器並確定正確的ImageCodecInfo將 Guid 與Codec FormatID屬性進行比較。
我正在添加一個將圖像質量設置為100%
的EncoderParameter :
Using myLogo As Bitmap = My.Resources.Logo_Reports
Dim codec As ImageCodecInfo = ImageCodecInfo.GetImageEncoders().
FirstOrDefault(Function(enc) enc.FormatID = myLogo.RawFormat.Guid)
' Assunimg codec is not nothing, otherwise abort
Dim fileName = $"LogoReport.{codec.FormatDescription.ToLower()}"
Dim qualityParam As EncoderParameter = New EncoderParameter(ImageCodec.Quality, 100L)
Dim codecParms As EncoderParameters = New EncoderParameters(1)
codecParms.Param(0) = qualityParam
Dim destinationPath = Path.Combine(FileFolderOther, fileName)
myLogo.Save(destinationPath, codec, codecParms)
End Using
它已經完成並且我已經正確測試了它,但是要知道我已經在項目文件夾“Debug\TmpFolder\”中創建了一個名為“TmpFolder”的新文件夾,然后試試這個代碼:
Private Sub BtbCopyFromResource_Click(sender As Object, e As EventArgs) Handles BtbCopyFromResource.Click
Try
'My.Computer.FileSystem.CurrentDirectory is the function for ===> current Project path, namly to Debug Folder
My.Resources.LogoReports.Save(My.Computer.FileSystem.CurrentDirectory & "\TmpFolder\db3451.png")
MsgBox("Done")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
希望對各位兄弟有幫助。 ^_^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.