Answer to your question below but first: I lied below. The reason I don't
get an exception from Files Bitmap wrote is because Bitmap writes PNG file
when asked to write PNG, WMF, EMF or ICON.
Here is the code. I put breakpoints at each of the Saves so I know if I ask
it to write a WMF for example it executes the correct statement.
It appears to write BMP, JPEG and GIF files correctly.
Private Sub SaveBitmapAs(ByVal apicP As System.Drawing.Bitmap)
With SaveFileDialog1
..Title = "select Filename And Format"
..Filter = "Bitmap(*.BMP)|*.BMP|Enhanced
Metafile(*.EMF)|*.EMF|Exchangeable(*.EXIF)|*.EXIF|Graphics
Interchange(*.GIF)|*.GIF|Icon(*.ICO)|*.ICO|Icon(*.ICON)|*.ICON|Joint
Photographic(*.JPEG)|*.JPEG|Joint Photographic(*.JPG)|*.JPG|Portable
Network(*.PNG)|*.PNG|Tag Image(*.TIFF)|*.TIFF|Metafile(*.WMF)|*.WMF"
..OverwritePrompt = True
If .ShowDialog = DialogResult.OK Then
Select Case System.IO.Path.GetExtension(.FileName).ToUpper
Case ".BMP"
apicP.Save(.FileName, Imaging.ImageFormat.Bmp)
Case ".EMF"
apicP.Save(.FileName, Imaging.ImageFormat.Emf)
Case ".EXIF"
apicP.Save(.FileName, Imaging.ImageFormat.Exif)
Case ".GIF"
apicP.Save(.FileName, Imaging.ImageFormat.Gif)
Case ".ICO", ".ICON"
apicP.Save(.FileName, Imaging.ImageFormat.Icon)
Case ".JPEG", ".JPG"
apicP.Save(.FileName, Imaging.ImageFormat.Jpeg)
Case ".PNG"
apicP.Save(.FileName, Imaging.ImageFormat.Png)
Case ".WMF"
apicP.Save(.FileName, Imaging.ImageFormat.Wmf)
Case Else
MsgBox("Can not save file with that extension", MsgBoxStyle.OKOnly Or
MsgBoxStyle.Information, "File Type Can Not Be Saved")
End Select
End If
End With
End Sub
Herfried K. Wagner said:
As to your question: I get an Invalid Cast exception. Here is the code:
Public Function CopyFileToBitmap() As Drawing.Bitmap
'Caller needs to dispose bitmap returned
'The file remains locked until the Image object is disposed so I clone it
With OpenFileDialog1
..Title = "select Filename To Copy"
..Filter = "All (*.*)|*.*|Bitmap(*.BMP)|*.BMP|Enhanced
Metafile(*.EMF)|*.EMF|Exchangeable(*.EXIF)|*.EXIF|Graphics
Interchange(*.GIF)|*.GIF|Icon(*.ICO)|*.ICO|Icon(*.ICON)|*.ICON|Joint
Photographic(*.JPEG)|*.JPEG|Joint Photographic(*.JPG)|*.JPG|Portable
Network(*.PNG)|*.PNG|Tag Image(*.TIFF)|*.TIFF|Metafile(*.WMF)|*.WMF"
If .ShowDialog = DialogResult.OK Then
'Try
Dim bmpTmp As Drawing.Bitmap = Drawing.Bitmap.FromFile(.FileName)
Dim bmp As Drawing.Bitmap = bmpTmp.Clone
bmpTmp.Dispose()
Return bmp
'Catch
' MsgBox("Can not copy that type of file", MsgBoxStyle.OKOnly Or
MsgBoxStyle.Information, "File Type Can Not Be Converted To Bitmap")
'End Try
End If
End With
End Function
Did a quick watch on Drawing.Bitmap.FromFile(.FileName) and got
Value = Drawing.Imaging.MetaFile
Type = Drawing.Image
Got rid of the Catch so I could see the exception