private static BitmapEncoder GetBitmapEncoder(ImageExtension type) { BitmapEncoder encoder = null; switch (type) { case ImageExtension.PNG: encoder = new PngBitmapEncoder(); break; case ImageExtension.GIF: encoder = new GifBitmapEncoder(); break; case ImageExtension.JPG: encoder = new JpegBitmapEncoder(); break; default: break; } return encoder; }
public static void SaveImageToFile(string absolutFileName, ImageExtension type, BitmapSource image) { try { using (FileStream stream = new FileStream(absolutFileName, FileMode.OpenOrCreate)) { BitmapEncoder encoder = GetBitmapEncoder(type); if (encoder != null) { encoder.Frames.Add(BitmapFrame.Create(image)); encoder.Save(stream); } } } catch (Exception) { } }