简体   繁体   English

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

[英]Copy a PNG file from the Resources to a local folder

I'm creating an installer for an application but I have an issue with copying my PNG file to a local folder from my Resources.我正在为应用程序创建安装程序,但在将 PNG 文件从我的资源复制到本地文件夹时遇到问题。

I've tried the usual stuff like File.Copy , File.WriteAllBytes() but nothing seems to work.我已经尝试过像File.CopyFile.WriteAllBytes()这样的常用东西,但似乎没有任何效果。 I only get error:我只得到错误:

bitmap cannot be converted to Byte()位图无法转换为 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

I just want to copy files (PNG, TXT etc.) from My.Resources to a local folder.我只想将文件(PNG、TXT 等)从My.Resources到本地文件夹。

My.Resources.[SomeImage] returns an Image object. My.Resources.[SomeImage]返回一个 Image 对象。

You can use the Image.Save method to save the Image to disc:您可以使用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

The File.Exist() check is only needed if, for some reason, you don't want to overwrite a file with same name. File.Exist()检查仅在出于某种原因不想覆盖同名文件时才需要。 If the file exists, it will be overwritten without errors.如果该文件存在,它将被无错覆盖。

The Using statement allows to dispose of the Image created by the ResourceManager factory. Using语句允许处理由ResourceManager工厂创建的图像。 If you need to store that Image, assign it to a Field/Property and dispose of it when the container Form/owner Class is closed/disposed.如果您需要存储该图像,请将其分配给 Field/Property 并在容器 Form/owner Class 关闭/处置时处置它。


You have hard-coded the Image type ( .Png ).您已经对图像类型 ( .Png ) 进行了硬编码。
Maybe that's the correct, original, format of that bitmap.也许那是该位图的正确原始格式。 If you instead don't know what is the type of a Resource Image (or any other Image) and you want preserve the original format, you can derive the Codec used to create the bitmap, using the Image.RawFormat.Guid property and determine the correct ImageCodecInfo comparing the Guid with the Codec FormatID property.相反,如果您不知道资源图像(或任何其他图像)的类型,并且希望保留原始格式,则可以使用Image.RawFormat.Guid属性派生用于创建位图的编解码器并确定正确的ImageCodecInfo将 Guid 与Codec FormatID属性进行比较。

I'm adding an EncoderParameter that set the Image quality to 100% :我正在添加一个将图像质量设置为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

It is done and I've tested it correctly, but to know I have created a new folder named as "TmpFolder" inside the project folder "Debug\TmpFolder\", then try this code:它已经完成并且我已经正确测试了它,但是要知道我已经在项目文件夹“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

Hope this helps you all brothers.希望对各位兄弟有帮助。 ^_^ ^_^

在此处输入图像描述

暂无
暂无

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

相关问题 PowerShell脚本将文件从本地文件复制到共享文件夹的最简单方法是什么? - What is the simplest way to copy a file from local to a shared folder by a PowerShell script? 在Visual Studio中,如何从项目Resources文件夹中加载.png图像? - In Visual Studio how can I load a .png image from the project Resources folder? 将文件从临时文件夹复制到浏览位置 - Copy a file from temp folder to a browsed location C#将最后一个文件从一个文件夹复制到另一个生成的文件夹 - C# copy last file from a folder into another generated folder 将文件从URI格式路径复制到本地路径 - Copy file from URI format path to local path 如何将文件从 UNC-share 复制到本地系统? - How to copy file from UNC-share to local system? 如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录 - How to copy a file from one directory to another directory by creating the folder if that folder does not exist 从Silverlight的文件夹中加载资源“ .resx” - Load Resources “.resx” from folder in Silverlight Nuget Pkg Dll 在.Net 标准 DLL 项目中的构建文件夹中丢失。 dll不是本地复制? 在 Visual Studio 2019 中修复? - Nuget Pkg Dlls Missing from Build Folder in .Net Standard DLL Project. dlls are Not Copy Local? Fix in Visual Studio 2019? 将javascript呈现的svg图像保存为本地磁盘为.png文件 - Save svg image rendered by a javascript to local disk as .png file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM