[英]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.Copy
, File.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.