/// <summary> /// Saves an image. /// </summary> /// <param name="img"> The image to save. </param> /// <param name="path"> The path to save the image. </param> /// <param name="collectGarbage"> A bool indicating if GC.Collect should be called after saving. </param> /// <returns> true if the image was saved successfully, else false </returns> public static bool SaveImage(Image img, string path, bool collectGarbage = true) { if (img == null || string.IsNullOrEmpty(path)) { return(false); } PathHelper.CreateDirectoryFromFilePath(path); try { switch (GetImageFormatFromPath(path)) { default: case ImgFormat.png: PNG.Save(img, path); return(true); case ImgFormat.jpg: JPEG.Save(img, path, InternalSettings.Jpeg_Quality); return(true); case ImgFormat.bmp: BMP.Save(img, path); return(true); case ImgFormat.gif: Gif.Save(img, path); return(true); case ImgFormat.tif: TIFF.Save(img, path); return(true); case ImgFormat.wrm: WORM.Save(img, path); return(true); case ImgFormat.webp: Webp.Save(img, path, InternalSettings.WebpQuality_Default); return(true); } } catch (Exception e) { e.ShowError(); return(false); } finally { if (collectGarbage) { GC.Collect(); } } }
/// <summary> /// Saves an image to disk. /// <para>Can throw just about any exception and should be used in a try catch</para> /// </summary> /// <param name="path">The path to save.</param> /// <param name="encodeGif">Should the Gif be encoded before saving.</param> /// <exception cref="Exception"></exception> public void Save(string path, bool encodeGif) { if (encodeGif) { Save(path); } PathHelper.CreateDirectoryFromFilePath(path); this.Image.Save(path, System.Drawing.Imaging.ImageFormat.Gif); }
/// <summary> /// Save an image using the png format. /// </summary> /// <param name="image">The image to save.</param> /// <param name="path">The path to save the image.</param> public static void Save(Image image, string path) { try { PathHelper.CreateDirectoryFromFilePath(path); image.Save(path, System.Drawing.Imaging.ImageFormat.Png); } catch (Exception ex) { throw new Exception(ex.Message + "\r\nIn PNG.Save(Image, string)"); } }
public static void Save(Image image, string path) { try { PathHelper.CreateDirectoryFromFilePath(path); Gif g = new Gif(image); g.Save(path); } catch (Exception ex) { throw new Exception(ex.Message + "\r\nIn Gif.Save(Image, string)"); } }
/// <summary> /// Save an image using the bitmap format. /// </summary> /// <param name="image">The image to save.</param> /// <param name="path">The path to save the image.</param> public static void Save(Image image, string path, long quality = 75L) { try { PathHelper.CreateDirectoryFromFilePath(path); JPEG t = new JPEG(image); t.Quality = quality; t.Save(path); } catch (Exception ex) { throw new Exception(ex.Message + "\r\nIn JPEG.Save(Image, string)"); } }
public override void Save(string path) { if (this.Image == null) { throw new System.ArgumentException("Gif.Save(string, bool)\n\tThe image cannot be null"); } if (string.IsNullOrEmpty(path)) { throw new System.ArgumentException("Gif.Save(string, bool)\n\tThe path cannot be null or empty"); } PathHelper.CreateDirectoryFromFilePath(path); using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { this.Save(fs); } }