2018年6月27日 星期三

[C#] Image、Bitmap與Base64字串互轉

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;

namespace My.Helper
{
    public class ImageHelper
    {
        /// 
        /// Bytes轉成Bitmap
        /// 
        /// 
        /// 
        public static Bitmap BytesToBitmap(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return null;
            }

            Bitmap bitmap = new Bitmap(new MemoryStream(bytes));
            return bitmap;
        }

        /// 
        /// Bytes轉成Image
        /// 
        /// 
        /// 
        public static Image BytesToImage(byte[] bytes)
        {
            return BytesToBitmap(bytes);
        }

        /// 
        /// Base64轉成Bitmap
        /// 
        /// 
        /// 
        public static Bitmap Base64ToBitmap(string base64)
        {
            if (string.IsNullOrEmpty(base64))
            {
                return null;
            }

            Bitmap bitmap = BytesToBitmap(Convert.FromBase64String(base64));
            return bitmap;
        }

        /// 
        /// Base64轉成Image
        /// 
        /// 
        /// 
        public static Image Base64ToImage(string base64)
        {
            return Base64ToBitmap(base64);
        }

        /// 
        /// Image轉成Base64
        /// 
        /// 
        /// 
        public static string ImageToBase64(Image image)
        {
            if (image == null)
            {
                return null;
            }

            using (MemoryStream stream = new MemoryStream())
            {
                image.Save(stream, image.RawFormat);
                return Convert.ToBase64String(stream.ToArray());
            }
        }

        /// 
        /// Bitmap轉成Base64
        /// 
        /// 
        /// 
        public static string BitmapToBase64(Bitmap bitmap)
        {
            return ImageToBase64(bitmap);
        }

        /// 
        /// Image轉成Bytes
        /// 
        /// 
        /// 
        public static byte[] ImageToBytes(Image image)
        {
            if (image == null)
            {
                return null;
            }

            using (MemoryStream stream = new MemoryStream())
            {
                image.Save(stream, image.RawFormat);
                return stream.ToArray();
            }
        }

        /// 
        /// Bitmap轉成Bytes
        /// 
        /// 
        /// 
        public static byte[] BitmapToBytes(Bitmap bitmap)
        {
            return ImageToBytes(bitmap);
        }

        /// 
        /// 取得圖片格式
        /// 
        /// 
        /// 
        public static string GetImageFormat(Image img)
        {
            if (img.RawFormat.Equals(ImageFormat.Jpeg))
            {
                return "jpeg";
            }
            else if (img.RawFormat.Equals(ImageFormat.Bmp))
            {
                return "bmp";
            }
            else if (img.RawFormat.Equals(ImageFormat.Png))
            {
                return "png";
            }
            else if (img.RawFormat.Equals(ImageFormat.Emf))
            {
                return "emf";
            }
            else if (img.RawFormat.Equals(ImageFormat.Exif))
            {
                return "exif";
            }
            else if (img.RawFormat.Equals(ImageFormat.Gif))
            {
                return "gif";
            }
            else if (img.RawFormat.Equals(ImageFormat.Icon))
            {
                return "icon";
            }
            else if (img.RawFormat.Equals(ImageFormat.MemoryBmp))
            {
                return "memorybmp";
            }
            else if (img.RawFormat.Equals(ImageFormat.Tiff))
            {
                return "tiff";
            }
            else if (img.RawFormat.Equals(ImageFormat.Wmf))
            {
                return "wmf";
            }
            else
            {
                return "unknow";
            }
        }

        /// 
        /// 圖片加浮水印
        /// 
        /// 
        /// 
        /// 
        public static Image Watermark(Image image, string MarkFile)
        {
            Image watermarkImage = Image.FromFile(MarkFile);

            Graphics graphics = Graphics.FromImage(image);
            ColorMatrix matrix = new ColorMatrix();
            //設定圖片色彩(透明度)
            matrix.Matrix33 = 0.5F;
            ImageAttributes imgattributes = new ImageAttributes();
            imgattributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            //從圖的中間位置加上浮上印
            graphics.DrawImage(watermarkImage, new Rectangle((image.Width / 2) - (image.Width / 2),
                                                             (image.Height / 2) - (watermarkImage.Height / 2),
                                                              image.Width,
                                                              image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imgattributes);

            return image;
        }
    }
}

沒有留言:

張貼留言