/// <summary> /// Attempts to determine the ImageFormat of the source image. First attempts to parse the path, if a string is present in original.Tag. (or if 'original' is a string) /// Falls back to using original.RawFormat. Returns null if both 'original' is null. /// RawFormat has a bad reputation, so this may return unexpected values, like MemoryBitmap or something in some situations. /// </summary> /// <param name="original">The source image that was loaded from a stream, or a string path</param> /// <returns></returns> public static ImageFormat GetOriginalFormat(object original) { if (original == null) { return(null); } //Try to parse the original file extension first. string path = original as string; if (path == null && original is Image) { path = ((Image)original).Tag as string; } if (path == null && original is Image && ((Image)original).Tag is BitmapTag) { path = ((BitmapTag)((Image)original).Tag).Path; } //We have a path? Parse it! if (path != null) { ImageFormat f = ImageEncoder.GetImageFormatFromPhysicalPath(path); if (f != null) { return(f); //From the path } } //Ok, I guess if there (a) wasn't a path, or (b), it didn't have a recognizable extension if (original is Image) { return(((Image)original).RawFormat); } return(null); }
/// <summary> /// Сохраняет изображение в файл или поток /// </summary> /// <param name="image">Изображение для сохранения</param> /// <param name="destination">Путь к файлу или поток, куда необходимо записать изображение</param> /// <param name="imageFormat">Формат изображения. Может быть jpeg, gif, png</param> /// <param name="jpegQuality">Качество изображения для jpeg в диапазоне 10-100. Игнорируется при других форматах. Оптимальным считается 90</param> public static void SaveImage(Image image, object destination, ImageFormat imageFormat, int jpegQuality) { if (image == null) { throw new ArgumentNullException("bitmap"); } // Определяем imageFormat изображения ImageFormat format = imageFormat; if (format == null) { if (destination is string) { try { format = ImageEncoder.GetImageFormatFromPhysicalPath(destination as string); } catch { format = image.RawFormat; } } else { format = image.RawFormat; } } if (jpegQuality < 10 || jpegQuality > 100) { throw new ArgumentException("Качество изображения может быть в диапазоне 10 - 100"); } if (destination is string) { string fileName = (string)destination; string dirName = Path.GetDirectoryName(fileName); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } bool finishedWrite = false; try { System.IO.FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write); using (fileStream) { BuildBitmapToStream(image, fileStream, imageFormat, jpegQuality); fileStream.Flush(); finishedWrite = true; } } finally { //Don't leave half-written files around. if (!finishedWrite) { try { if (File.Exists(fileName)) { File.Delete(fileName); } } catch { } } } } else if (destination is Stream) { BuildBitmapToStream(image, (Stream)destination, imageFormat, jpegQuality); } else { throw new ArgumentException("Destination may be a string or Stream.", "Dest"); } }