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.
I've tried the usual stuff like File.Copy
, File.WriteAllBytes()
but nothing seems to work. I only get error:
bitmap cannot be converted to 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.
My.Resources.[SomeImage]
returns an Image object.
You can use the Image.Save method to save the Image to disc:
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. If the file exists, it will be overwritten without errors.
The Using
statement allows to dispose of the Image created by the ResourceManager factory. 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.
You have hard-coded the Image type ( .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.
I'm adding an EncoderParameter that set the Image quality to 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
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:
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. ^_^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.