![](/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.