using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

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;
            }
        }

        public static byte[] GetImageByteFromPath(string ImagePath, string fileExt, out double width, out double height)
        {
            try
            {
                #region 跟图章一样的转换方法。
                if (ImagePath != null && ImagePath.Length > 0 && File.Exists(ImagePath))
                {
                    FileStream fileData = File.OpenRead(ImagePath);
                    BitmapFrame frame = null;
                    width = 0;
                    height = 0;

                    if (fileExt.Contains(".jpg") || fileExt.Contains(".jpeg"))
                    {
                        JpegBitmapDecoder decoder = (JpegBitmapDecoder)JpegBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        frame = decoder.Frames[0];
                        width = frame.Width;
                        height = frame.Height;
                    }
                    else if (fileExt.Contains(".png"))
                    {
                        PngBitmapDecoder decoder = (PngBitmapDecoder)PngBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        frame = decoder.Frames[0];
                        width = frame.Width;
                        height = frame.Height;
                    }
                    else if (fileExt.Contains(".bmp"))
                    {
                        BmpBitmapDecoder decoder = (BmpBitmapDecoder)BmpBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        frame = decoder.Frames[0];
                        width = frame.Width;
                        height = frame.Height;
                    }
                    else//默认采用png的解码方式
                    {
                        BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        frame = decoder.Frames[0];
                        width = frame.Width;
                        height = frame.Height;
                    }

                    if (frame != null)
                    {
                        var ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
                        frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
                        return ImageArray;
                    }
                    return null;
                }
                width = 0;
                height = 0;
                return null;
                #endregion
            }
            catch (Exception ex)
            {
                width = 0;
                height = 0;
                return null;
            }
        }

    }
}