/// <summary> /// Load a file into a standard Bitmap object. Will automatically /// detect the format of the image. /// </summary> /// <param name="fileName">Name of the file to load.</param> /// <returns>Bitmap that contains the decoded image, or null if it could /// not be decoded by any of the formats known to this library.</returns> public static Bitmap Load(string fileName) { Bitmap bitmap; var ext = Path.GetExtension(fileName)?.ToLowerInvariant(); if (string.IsNullOrWhiteSpace(ext)) { return(null); } if (ext.EndsWith("tga")) { bitmap = TgaReader.Load(fileName); } else if (ext.EndsWith("cut")) { bitmap = CutReader.Load(fileName); } else if (ext.EndsWith("sgi") || ext.EndsWith("rgb") || ext.EndsWith("bw")) { bitmap = SgiReader.Load(fileName); } else if (ext.EndsWith("xpm")) { bitmap = XpmReader.Load(fileName); } else { bitmap = (Bitmap)Image.FromFile(fileName); } return(bitmap); }
/// <summary> /// Load a file into a standard Bitmap object. Will automatically /// detect the format of the image. /// </summary> /// <param name="fileName">Name of the file to load.</param> /// <returns>Bitmap that contains the decoded image, or null if it could /// not be decoded by any of the formats known to this library.</returns> public static Bitmap Load(string fileName) { Bitmap bmp = null; using (FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { bmp = Load(f); } if (bmp == null) { if (Path.GetExtension(fileName).ToLower().Contains("tga")) { bmp = TgaReader.Load(fileName); } } if (bmp == null) { if (Path.GetExtension(fileName).ToLower().Contains("cut")) { bmp = CutReader.Load(fileName); } } if (bmp == null) { if (Path.GetExtension(fileName).ToLower().Contains("sgi") || Path.GetExtension(fileName).ToLower().Contains("rgb") || Path.GetExtension(fileName).ToLower().Contains("bw")) { bmp = SgiReader.Load(fileName); } } if (bmp == null) { if (Path.GetExtension(fileName).ToLower().Contains("xpm")) { bmp = XpmReader.Load(fileName); } } return(bmp); }
public static Bitmap Load(string fileName) { Bitmap bitmap; using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { bitmap = Load(fileStream); if (bitmap != null) { return(bitmap); } var extension = Path.GetExtension(fileName); var text = (extension != null) ? extension.ToLowerInvariant() : null; if (string.IsNullOrWhiteSpace(text)) { return(null); } if (text.EndsWith("tga")) { bitmap = TgaReader.Load(fileStream); } else if (text.EndsWith("cut")) { bitmap = CutReader.Load(fileStream); } else if (text.EndsWith("sgi") || text.EndsWith("rgb") || text.EndsWith("bw")) { bitmap = SgiReader.Load(fileStream); } else if (text.EndsWith("xpm")) { bitmap = XpmReader.Load(fileStream); } } return(bitmap); }