'/// '/// Provides static (Shared) methods to find the'/// ImageFormat for a filename or MIME type.'/// Public NotInheritable Class ImageFormatUtils Private Shared codecs() As ImageCodecInfo Private Shared formats As Hashtable Shared Sub New() '// Get a combined list of all codecs Dim encoders() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders() If encoders Is Nothing Then ReDim encoders(0) Dim decoders() As ImageCodecInfo = ImageCodecInfo.GetImageDecoders() If decoders Is Nothing Then ReDim decoders(0) Dim codec As ImageCodecInfo Dim htCodecs As new Hashtable(encoders.Length + decoders.Length) Dim i As Integer For i = 0 To encoders.Length - 1 codec = encoders(i) htCodecs.Add(codec.Clsid, codec) Next For i = 0 To decoders.Length - 1 codec = decoders(i) If Not htCodecs.ContainsKey(codec.Clsid) Then htCodecs.Add(codec.Clsid, codec) End If Next ReDim codecs(htCodecs.Count - 1) htCodecs.Values.CopyTo(codecs, 0) '// Enumerate the pre-defined formats Try Dim tImageFormat As Type = GetType(ImageFormat) Dim props() As PropertyInfo = tImageFormat.GetProperties _ (BindingFlags.Public Or BindingFlags.Static) Dim prop As PropertyInfo Dim format As ImageFormat formats = new Hashtable(props.Length) For i = 0 To props.Length - 1 prop = props(i) If prop.PropertyType Is tImageFormat Then format = DirectCast(prop.GetValue(Nothing, Nothing), _ ImageFormat) formats.Add(format.Guid, format) End If Next Catch End Try End Sub '/// '/// Returns the ImageFormat instance for the '/// specified format ID. Uses reflection to '/// test the static instances of the class '/// for the correct ID, before using the '/// constructor. '/// '/// '/// The Guid which contains the format ID. '/// '/// '/// The corresponding ImageFormat object. '/// Public Shared Function FromID(ByVal formatID As Guid) As ImageFormat If formats Is Nothing OrElse Not formats.ContainsKey(formatID) Then Return New ImageFormat(formatID) Else Return DirectCast(formats(formatID), ImageFormat) End If End Function '/// '/// Returns the ImageCodecInfo which corresponds '/// to the specified MIME type. '/// '/// '/// The MIME type, e.g. "image/jpeg" '/// '/// '/// The ImageCodecInfo for the MIME type. '/// Public Shared Function CodecFromMime(ByVal mimeType As String) As _ ImageCodecInfo If mimeType Is Nothing OrElse 0 = mimeType.Length Then Throw New ArgumentNullException("mimeType") End If Dim i As Integer For i = 0 To codecs.Length - 1 If 0 = String.Compare(codecs(i).MimeType, mimeType, true) Then Return codecs(i) End If Next Return Nothing End Function '/// '/// Returns the ImageFormat which corresponds '/// to the specified MIME type. '/// '/// '/// The MIME type, e.g. "image/jpeg" '/// '/// '/// The ImageFormat for the MIME type. '/// Public Shared Function FromMime(ByVal mimeType As String) As ImageFormat If mimeType Is Nothing OrElse 0 = mimeType.Length Then Throw New ArgumentNullException("mimeType") End If Dim info As ImageCodecInfo = CodecFromMime(mimeType) If info Is Nothing Then Return Nothing Else Return FromID(info.FormatID) End If End Function '/// '/// Returns an ImageCodecInfo which corresponds '/// to the extension of the supplied filename. '/// '/// '/// The filename or extension of the image. '/// '/// '/// The ImageCodecInfo for the filename. '/// Public Shared Function CodecFromExtension(ByVal filename As String) As _ ImageCodecInfo If filename Is Nothing OrElse 0 = filename.Length Then Throw New ArgumentNullException("filename") End If Dim index As Integer = filename.LastIndexOf("."c) If -1 = index Then Return Nothing Dim ext As String = "*." & filename.Substring(index + 1) Dim i As Integer For i = 0 To codecs.Length - 1 Dim extensions() As String = codecs(i).FilenameExtension.Split(";"c) Dim j As Integer For j = 0 To extensions.Length - 1 If 0 = String.Compare(extensions(j), ext, true) Then Return codecs(i) End If Next Next Return Nothing End Function '/// '/// Returns an ImageFormat which corresponds '/// to the extension of the supplied filename. '/// '/// '/// The filename or extension of the image. '/// '/// '/// The ImageFormat for the filename. '/// Public Shared Function FromExtension(ByVal filename As String) As _ ImageFormat If filename Is Nothing OrElse 0 = filename.Length Then Throw New ArgumentNullException("filename") End If Dim info As ImageCodecInfo = CodecFromExtension(filename) If info Is Nothing Then Return Nothing Else Return FromID(info.FormatID) End If End Function '/// '/// No need to create an instance of this class. '/// Private Sub New() End SubEnd Class' *******************' * SAMPLE USAGE' *******************Option ExplicitOption StrictImports SystemImports System.CollectionsImports System.ReflectionImports System.Drawing.ImagingClass App Shared Sub Main() Try Dim file As String Dim index As Integer Dim codec As ImageCodecInfo do Console.Write("Enter a file name or MIME type: ") file = Console.ReadLine() If Not (file Is Nothing) AndAlso 0 < file.Length Then index = file.IndexOf("/"c) If -1 = index Then codec = ImageFormatUtils.CodecFromExtension(file) Else codec = ImageFormatUtils.CodecFromMime(file) End If If codec Is Nothing Then Console.WriteLine("Unknown format.") Else WriteCodec(codec) End If Console.WriteLine() End If Loop While Not(file Is Nothing) AndAlso 0 < file.Length Catch ex As Exception Console.WriteLine(ex) End Try End Sub Shared Sub WriteCodec(ByVal codec As ImageCodecInfo) If codec Is Nothing Then Throw New ArgumentNullException("codec") Console.WriteLine("Format: {0}", ImageFormatUtils.FromID(codec.FormatID) _ ) Console.WriteLine("Name: {0}", codec.CodecName) Console.WriteLine("DLL: {0}", codec.DllName) Console.WriteLine("Version: {0}", codec.Version) Console.WriteLine("Extension: {0}", codec.FilenameExtension) Console.WriteLine("MIME: {0}", codec.MimeType) End SubEnd Class


Revolutionizing Search: A Glimpse Into Google’s Generative Experience
Google is revolutionizing the search experience as we know it with its latest generative experience. No longer will you be bound by the limitations of traditional keyword searching. Now, you