convert enum to imaging.imageformat

  • Thread starter Thread starter David A. Osborn
  • Start date Start date
D

David A. Osborn

I currently have a class that has an enumeration of graphics extensions that
it supports ie:

Public Enum GraphicFormats
JPG
JPEG
BMP
TIFF
End Enum


When a user selects a file it checks against this enum to verify it has an
okay extension and if I ever want add more files I can just modify this
enum. At some point though I need to translate a variable of the type
GraphicsFormat to type Imaging.ImageFormat in order to pass to a function.
I.E. BMP to Imaging.ImageFormat.BMP and JPG or JPEG to
Imaging.ImageFormat.JPEG.

I thought Maybe I could do the following when the user loads the vaiable:

Public Property FileFormat() As GraphicFormats
Get
If _FileFormat Is Imaging.ImageFormat.Jpeg Then
Return GraphicFormats.JPEG
ElseIf _FileFormat Is Imaging.ImageFormat.Bmp Then
Return GraphicFormats.BMP
ElseIf _FileFormat Is Imaging.ImageFormat.Tiff Then
Return GraphicFormats.TIFF
End If
End Get
Set(ByVal Value As GraphicFormats)
If Value = GraphicFormats.JPEG Or Value = GraphicFormats.JPG
Then
_FileFormat = Imaging.ImageFormat.Jpeg
ElseIf Value = GraphicFormats.BMP Then
_FileFormat = Imaging.ImageFormat.Bmp
ElseIf Value = GraphicFormats.TIFF Then
_FileFormat = Imaging.ImageFormat.Tiff
End If
End Set
End Property

This way the object would really store in _FileFormat of type
Imaging.ImageFormat, but when the user access the variable the get a type of
my enumeration. But this doesn't work because if I access _FileFormat in my
class it doesn't have the value, but just an object of type
Imaging.ImageFormat, and if I access Me.FileFormat in my class I get a type
of my enum instead.

Also this method kills my ease of adding new extensions to my class. Maybe
I am going about this completely the wrong way. Any suggestions?
 
David,

I have this as almost yours in a program like beneath. You can change that
filterindex of course for the extention and set than in the case more
extentions and test on that. However, this is for me a typical sample of
using a select case (you can set more condition seperated by a comma in a
select case)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmselectcase.asp

\\\\
Private Sub whatever
)
)
fdlSave.Filter = "Bitmap (*.bmp)|*.bmp|GIF (*.gif)|*.gif|" & _
"JPEG (*.jpg)|*.jpg;*.jpeg|TIF (*.tif)|*.tif|PNG (*.png)|*.png"
fdlSave.FilterIndex = 3
If fdlSave.ShowDialog() = DialogResult.OK Then
Dim ici As ImageCodecInfo = GetEncoderInfo(fdlSave.FilterIndex)
newImage.Save(fdlSave.FileName, ici, eps)
End If
End Sub
Private Function Getimageformat(ByVal Filterindex As Integer) As
Imaging.ImageFormat
Select Case Filterindex
Case 1
Return Imaging.ImageFormat.Bmp
Case 2
Return Imaging.ImageFormat.Gif
Case 3
Return Imaging.ImageFormat.Jpeg
Case 4
Return Imaging.ImageFormat.Tiff
Case 5
Return Imaging.ImageFormat.Png
End Select
End Function
///

I hope this helps?

Cor
 
Thanks, but not exactly what I'm looking for. I'm using a TreeView control
and not a file dialog box. I'm actually hoping to figure out a way to list
my extensions in the enumeration so that it is the only place I will update
to add new extensions. Is there are way to "map" each enumeration to a type
if Imaging.ImageFormat?
 
David,

Any reason that it "must" be an Enum.
In my opinion is setting in the Tag of the treeview an indexnumber much
easier.

Just my opinion.

Cor
 
Back
Top