Exemple #1
0
        /// <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();
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Loads a WORM image and returns it as a bitmap object.
 /// </summary>
 /// <param name="file">The path of the image.</param>
 /// <returns>A <see cref="Bitmap"/> object.</returns>
 public static Bitmap FromFileAsBitmap(string file)
 {
     try
     {
         return(WORM.FromFile(file).Image);
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message + "\r\nIn WORM.FromFileAsBitmap(string)");
     }
 }
Exemple #3
0
 public static void Save(Image image, string path)
 {
     try
     {
         WORM wrm = new WORM(image);
         wrm.Save(path);
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message + "\r\tIn WORM.Save(Image, string)");
     }
 }
Exemple #4
0
 /// <summary>
 /// Load a wrm image.
 /// </summary>
 /// <param name="file">The path of the image.</param>
 /// <returns>A new instance of the WORM class.</returns>
 public static WORM FromFile(string file)
 {
     try
     {
         WORM wrm = new WORM();
         wrm.Load(file);
         return(wrm);
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message + "\r\nIn WORM.FromFile(string)");
     }
 }
Exemple #5
0
        /// <summary>
        /// Save an image as a wrm file. (a custom image format i made)
        /// </summary>
        /// <param name="img">The image to encode.</param>
        /// <param name="filePath">The path to save the image.</param>
        /// <returns>True if the image was saved, else false.</returns>
        public static bool SaveWrm(Image img, string path)
        {
            try
            {
                WORM wrm = new WORM(img);

                wrm.Save(path);
                return(true);
            }
            catch (Exception e)
            {
                e.ShowError();
            }
            return(false);
        }
Exemple #6
0
        public static IMAGE LoadImage(string path, bool showError = false)
        {
            if (string.IsNullOrEmpty(path) || !File.Exists(path))
            {
                return(null);
            }

            ImgFormat fmt = GetImageFormat(path);

            if (fmt == ImgFormat.nil)
            {
                fmt = GetImageFormatFromPath(path);
            }

            try
            {
                switch (fmt)
                {
                case ImgFormat.png:
                    PNG png = new PNG();
                    png.Load(path);
                    return(png);

                case ImgFormat.bmp:
                    BMP bmp = new BMP();
                    bmp.Load(path);
                    return(bmp);

                case ImgFormat.gif:
                    Gif gif = new Gif();
                    gif.Load(path);
                    return(gif);

                case ImgFormat.jpg:
                    JPEG jpeg = new JPEG();
                    jpeg.Load(path);
                    return(jpeg);

                case ImgFormat.tif:
                    TIFF tiff = new TIFF();
                    tiff.Load(path);
                    return(tiff);

                case ImgFormat.webp:
                    Webp webp = new Webp();
                    webp.Load(path);
                    return(webp);

                case ImgFormat.wrm:
                    WORM worm = new WORM();
                    worm.Load(path);
                    return(worm);
                }
            }
            catch (Exception e)
            {
                if (showError)
                {
                    e.ShowError();
                }
            }
            return(null);
        }
Exemple #7
0
        /// <summary>
        /// Loads an image.
        /// </summary>
        /// <param name="path"> The path to the image. </param>
        /// <returns> A bitmap object if the image is loaded, otherwise null. </returns>
        public static Bitmap LoadImageAsBitmap(string path, bool showError = false)
        {
            if (string.IsNullOrEmpty(path) || !File.Exists(path))
            {
                return(null);
            }

            ImgFormat fmt = GetImageFormat(path);

            if (fmt == ImgFormat.nil)
            {
                fmt = GetImageFormatFromPath(path);
            }

            try
            {
                Bitmap result;

                switch (fmt)
                {
                case ImgFormat.png:
                    result     = PNG.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.png;
                    return(result);

                case ImgFormat.bmp:
                    result     = BMP.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.bmp;
                    return(result);

                case ImgFormat.gif:
                    result     = Gif.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.gif;
                    return(result);

                case ImgFormat.jpg:
                    result     = JPEG.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.jpg;
                    return(result);

                case ImgFormat.tif:
                    result     = TIFF.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.tif;
                    return(result);

                case ImgFormat.webp:
                    result     = Webp.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.webp;
                    return(result);

                case ImgFormat.wrm:
                    result     = WORM.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.wrm;
                    return(result);
                }
            }
            catch (Exception e)
            {
                if (showError)
                {
                    e.ShowError();
                }
            }
            return(null);
        }
Exemple #8
0
 private static Size DecodeDWORM(BinaryReader binaryReader)
 {
     return(WORM.GetDimensions(binaryReader, false));
 }