By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

Drawing.Bitmap.FromFile ever work for WMF files

P: n/a
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.

Can anyone share some knowledge on this?
Thanks,
Cal
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* " active" <ac****@REMOVEa-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>
Nov 20 '05 #2

P: n/a
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 [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
* " active" <ac****@REMOVEa-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 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
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.