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(By Val apicP As System.Drawing. Bitmap)
With SaveFileDialog1
..Title = "select Filename And Format"
..Filter = "Bitmap(*.BMP)| *.BMP|Enhanced
Metafile(*.EMF) |*.EMF|Exchange able(*.EXIF)|*. EXIF|Graphics
Interchange(*.G IF)|*.GIF|Icon( *.ICO)|*.ICO|Ic on(*.ICON)|*.IC ON|Joint
Photographic(*. JPEG)|*.JPEG|Jo int Photographic(*. JPG)|*.JPG|Port able
Network(*.PNG)| *.PNG|Tag Image(*.TIFF)|* .TIFF|Metafile( *.WMF)|*.WMF"
..OverwriteProm pt = True
If .ShowDialog = DialogResult.OK Then
Select Case System.IO.Path. GetExtension(.F ileName).ToUppe r
Case ".BMP"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Bmp)
Case ".EMF"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Emf)
Case ".EXIF"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Exif)
Case ".GIF"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Gif)
Case ".ICO", ".ICON"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Icon)
Case ".JPEG", ".JPG"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Jpeg)
Case ".PNG"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Png)
Case ".WMF"
apicP.Save(.Fil eName, Imaging.ImageFo rmat.Wmf)
Case Else
MsgBox("Can not save file with that extension", MsgBoxStyle.OKO nly Or
MsgBoxStyle.Inf ormation, "File Type Can Not Be Saved")
End Select
End If
End With
End Sub
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
* " active" <ac****@REMOV Ea-znet.com> scripsit: I find Bitmap.Save works for WMF files but Bitmap.FromFile does not.
If I use FromFile on a WMF file that came with VS I get an exception.
If I use it on a WMF file created with Bitmap.Save I don't get an
exception but it appears the Bitmap is blank.
Exception text?
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
As to your question: I get an Invalid Cast exception. Here is the code:
Public Function CopyFileToBitma p() 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 (*.*)|*.*|Bitma p(*.BMP)|*.BMP| Enhanced
Metafile(*.EMF) |*.EMF|Exchange able(*.EXIF)|*. EXIF|Graphics
Interchange(*.G IF)|*.GIF|Icon( *.ICO)|*.ICO|Ic on(*.ICON)|*.IC ON|Joint
Photographic(*. JPEG)|*.JPEG|Jo int Photographic(*. JPG)|*.JPG|Port able
Network(*.PNG)| *.PNG|Tag Image(*.TIFF)|* .TIFF|Metafile( *.WMF)|*.WMF"
If .ShowDialog = DialogResult.OK Then
'Try
Dim bmpTmp As Drawing.Bitmap = Drawing.Bitmap. FromFile(.FileN ame)
Dim bmp As Drawing.Bitmap = bmpTmp.Clone
bmpTmp.Dispose( )
Return bmp
'Catch
' MsgBox("Can not copy that type of file", MsgBoxStyle.OKO nly Or
MsgBoxStyle.Inf ormation, "File Type Can Not Be Converted To Bitmap")
'End Try
End If
End With
End Function
Did a quick watch on Drawing.Bitmap. FromFile(.FileN ame) and got
Value = Drawing.Imaging .MetaFile
Type = Drawing.Image
Got rid of the Catch so I could see the exception