Click here to Skip to main content
15,923,015 members
Home / Discussions / C#
   

C#

 
GeneralCommunication with com1 Pin
Viedy7-May-03 1:14
Viedy7-May-03 1:14 
GeneralRe: Communication with com1 Pin
Viedy7-May-03 3:39
Viedy7-May-03 3:39 
GeneralRe: Communication with com1 Pin
Torsten Mauz7-May-03 3:52
Torsten Mauz7-May-03 3:52 
GeneralRe: Communication with com1 Pin
Carlos Antollini7-May-03 4:48
Carlos Antollini7-May-03 4:48 
QuestionHow Do i Create DSN in C# Pin
Faisal7867-May-03 0:34
Faisal7867-May-03 0:34 
AnswerRe: How Do i Create DSN in C# Pin
Marcin7-May-03 12:09
Marcin7-May-03 12:09 
GeneralImageFormat with SaveFileDialog Pin
Roald Bankras7-May-03 0:32
Roald Bankras7-May-03 0:32 
GeneralRe: ImageFormat with SaveFileDialog Pin
Richard Deeming8-May-03 4:45
mveRichard Deeming8-May-03 4:45 
The cleanest way to do this is to use the ImageCodecInfo class, and some reflection on the ImageFormat class. The code isn't simple, but you won't need to update it if new formats are added. Just call ImageUtils.GetImageFormatForFile, pass in the selected file name, and the function will return the correct ImageFormat object.
C#
using System;
using System.IO;
using System.Reflection;
using System.Drawing.Imaging;
 
public class ImageUtils
{
    /// <summary>
    /// Returns an ImageFormat which corresponds
    /// to the extension of the supplied filename.
    /// </summary>
    /// <param name="filename">
    /// The filename or extension of the image.
    /// </param>
    /// <returns>
    /// The ImageFormat for the filename.
    /// </returns>
    public static ImageFormat GetImageFormatForFile(string filename)
    {
        if (null == filename || 0 == filename.Length)
            throw new ArgumentNullException("filename");
    
        // Find the extension of the filename:
        int index = filename.IndexOf('.');
        string ext = (-1 == index) ? filename : filename.Substring(index + 1);
        ext = "*." + ext;
        
        // Find the ImageCodecInfo which matches the extension:
        ImageCodecInfo info = null;
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
        for(int i=0; i<codecs.Length && null == info; i++)
        {
            string[] extensions = codecs[i].FilenameExtension.Split(';');
            for(int j=0; j<extensions.Length && null == info; j++)
            {
                if (0 == string.Compare(extensions[j], ext, true))
                    info = codecs[i];
            }
        }
        
        // If we can't find the codec, there is no matching format
        if (null == info)
            return null;
        else
            return GetImageFormat(info.FormatID);
    }
    
    /// <summary>
    /// Returns the ImageFormat which corresponds
    /// to the specified MIME type.
    /// </summary>
    /// <param name="mimeType">
    /// The MIME type, e.g. "image/jpeg"
    /// </param>
    /// <returns>
    /// The ImageFormat for the MIME type.
    /// </returns>
    public static ImageFormat GetImageFormatForMime(string mimeType)
    {
        // Find the ImageCodecInfo which matches the MIME type:
        ImageCodecInfo info = null;
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
        for(int i=0; i<codecs.Length && null == info; i++)
        {
            if (0 == string.Compare(codecs[i].MimeType, mimeType, true))
                info = codecs[i];
        }
        
        // If we can't find the codec, there is no matching format
        if (null == info)
            return null;
        else
            return GetImageFormat(info.FormatID);
    }
    
    /// <summary>
    /// 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.
    /// </summary>
    /// <param name="formatID">
    /// The Guid which contains the format ID.
    /// </param>
    /// <returns>
    /// The corresponding ImageFormat object.
    /// </returns>
    public static ImageFormat GetImageFormat(Guid formatID)
    {
        // Find the matching image format
        // The image formats are stored as static properties
        // of the ImageFormat class.
        Type tImageFormat = typeof(ImageFormat);
        PropertyInfo[] props = tImageFormat.GetProperties(
            BindingFlags.Public | BindingFlags.Static);
        
        for(int i=0; i<props.Length; i++)
        {
            PropertyInfo prop = props[i];
            if (tImageFormat == prop.PropertyType)
            {
                // This is an ImageFormat property
                ImageFormat fmt = (ImageFormat)prop.GetValue(null, null);
                if (fmt.Guid == formatID)
                    return fmt;
            }
        }
        
        // None of the static ImageFormat properties matched,
        // so use the FormatID to create a new format:
        return new ImageFormat(formatID);
    }
}



"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
GeneralCOMException "Requested resource in use" Pin
GriffonRL7-May-03 0:14
GriffonRL7-May-03 0:14 
GeneralRe: COMException "Requested resource in use" Pin
Stephane Rodriguez.7-May-03 22:07
Stephane Rodriguez.7-May-03 22:07 
GeneralRe: COMException "Requested resource in use" Pin
GriffonRL12-May-03 0:42
GriffonRL12-May-03 0:42 
GeneralDataComponents in Visual C# Pin
flyingv6-May-03 23:41
flyingv6-May-03 23:41 
GeneralRe: DataComponents in Visual C# Pin
Andres Manggini7-May-03 20:09
Andres Manggini7-May-03 20:09 
GeneralRe: DataComponents in Visual C# Pin
Anonymous9-May-03 2:01
Anonymous9-May-03 2:01 
GeneralRe: DataComponents in Visual C# Pin
Andres Manggini9-May-03 13:00
Andres Manggini9-May-03 13:00 
QuestionHow to write WebControl Pin
KETUINHA6-May-03 21:21
KETUINHA6-May-03 21:21 
QuestionHow to extract links from a webpage using mshtml Pin
benzite6-May-03 20:20
benzite6-May-03 20:20 
AnswerRe: How to extract links from a webpage using mshtml Pin
Stephane Rodriguez.6-May-03 22:19
Stephane Rodriguez.6-May-03 22:19 
GeneralRe: How to extract links from a webpage using mshtml Pin
benzite7-May-03 17:30
benzite7-May-03 17:30 
GeneralRe: How to extract links from a webpage using mshtml Pin
Stephane Rodriguez.7-May-03 20:37
Stephane Rodriguez.7-May-03 20:37 
GeneralRe: How to extract links from a webpage using mshtml Pin
benzite7-May-03 21:41
benzite7-May-03 21:41 
GeneralRe: How to extract links from a webpage using mshtml Pin
Stephane Rodriguez.7-May-03 21:58
Stephane Rodriguez.7-May-03 21:58 
GeneralRe: How to extract links from a webpage using mshtml Pin
benzite7-May-03 22:25
benzite7-May-03 22:25 
AnswerRe: How to extract links from a webpage using mshtml Pin
Stephane Rodriguez.6-May-03 22:36
Stephane Rodriguez.6-May-03 22:36 
General<mailto> in C# Pin
eggie56-May-03 15:02
eggie56-May-03 15:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.