using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PDF_Master.Helper
{
    public class PictureConverter
    {
        /// <summary>
        /// 图片转PNG(其他图片先转为PNG在转JPG)
        /// picturefile 图片文件名
        /// filePath 保存路径例如//filePath.png
        /// </summary>
        public static bool  SavePng(string picturefile, string filePath)
        {
            Image img = Image.FromFile(picturefile);
            try
            {
                using (var bmp = new Bitmap(img.Width, img.Height))
                {
                    bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                    using (var g = Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.White);
                        g.DrawImageUnscaled(img, 0, 0);
                    }
                    bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 图片转PNG(其他图片先转为PNG在转JPG)
        /// picturefile 图片image
        /// filePath 保存路径例如//filePath.png
        /// </summary>
        public static bool SavePng(Image img, string filePath)
        {
            try
            {
                using (var bmp = new Bitmap(img.Width, img.Height))
                {
                    bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                    using (var g = Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.White);
                        g.DrawImageUnscaled(img, 0, 0);
                    }
                    bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 图片转JPeG
        /// picturefile 图片文件名
        /// filePath 保存路径例如//filePath.jpg
        /// </summary>
        public static bool SaveJpeg(string picturefile, string filePath)
        {
            Image img = Image.FromFile(picturefile);
            try
            {
                using (var bmp = new Bitmap(img.Width, img.Height))
                {
                    bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                    using (var g = Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.White);
                        g.DrawImageUnscaled(img, 0, 0);
                    }
                    //存储各种格式
                    //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
                    //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
                    bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 图片转JPeG
        /// img 图片Image
        /// filePath 保存路径例如//filePath.jpg
        /// </summary>
        public static bool SaveJpeg(Image img, string filePath)
        {
            
            try
            {
                using (var bmp = new Bitmap(img.Width, img.Height))
                {
                    bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                    using (var g = Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.White);
                        g.DrawImageUnscaled(img, 0, 0);
                    }
                    //存储各种格式
                    //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
                    //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
                    bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}