convert enum to imaging.imageformat

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?
 
C

Cor Ligthert [MVP]

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
 
D

David A. Osborn

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?
 
C

Cor Ligthert [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Better way to get a bitmap file's format 8
Bitmap Saved as GIF loses image quality 6
Enum Extentions 7
Global Enum question 1
Why [Enum]? 3
Enum Properties Revisited 3
Inherit ENUM 2
Strange Enum Properties 2

Top