将PNG文件从资源复制到本地文件夹

时间:2019-04-19 13:44:24

标签: vb.net

我正在为应用程序创建安装程序,但是在将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复制到本地文件夹中。

2 个答案:

答案 0 :(得分:1)

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工厂创建的图像。如果需要存储该图像,则将其分配给“字段/属性”,并在“表单/所有者类”容器关闭/处理后进行处理。


您已对图像类型(.Png)进行了硬编码。
也许那是该位图的正确原始格式。相反,如果您不知道资源映像(或任何其他映像)的类型是什么,并且想要保留原始格式,则可以使用Image.RawFormat.Guid属性来派生用于创建位图的编解码器,并确定将Guid与ImageCodecInfo属性进行比较的正确Codec FormatID

我要添加一个EncoderParameter并将图像质量设置为100%

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

答案 1 :(得分:0)

这已经完成并且我已经正确测试了,但是要知道我已经在项目文件夹“ 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

希望这对你们所有兄弟有帮助。 ^ _ ^

enter image description here

相关问题