DrawHelper

2022-11-30  本文已影响0人  o简单生活o

简单的写字,合成图,缩放,图片映射,图片变圆角

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Framework.API.Utility
{
    public static class DrawHelper
    {
        #region 绘图方法
        public static Stream DrawImage(this Stream SourceImageStream, Stream DestImageStream, int DX, int DY, int Width, int Height)
        {
            Image SourceImage = Image.FromStream(SourceImageStream);
            Image DestImage = Image.FromStream(DestImageStream);

            Graphics g = Graphics.FromImage(SourceImage);

            g.DrawImage(DestImage, DX, DY, Width, Height);

            Stream stream = new MemoryStream();
            SourceImage.Save(stream, ImageFormat.Png);

            stream.Position = 0;
            return stream;
        }

        public static Stream DrawImage(this Stream SourceImageStream, byte[] DestImageByte, int DX, int DY, int Width, int Height)
        {
            var DestImageStream = DestImageByte.ToStreamFromBytes();
            return DrawImage(SourceImageStream, DestImageStream, DX, DY, Width, Height);
        }

        public static Stream DrawImage(this byte[] SourceImageByte, byte[] DestImageByte, int DX, int DY, int Width, int Height)
        {
            var DestImageStream = DestImageByte.ToStreamFromBytes();
            var SourceImageStream = SourceImageByte.ToStreamFromBytes();

            return DrawImage(SourceImageStream, DestImageStream, DX, DY, Width, Height);
        }

        public static Stream DrawImage(this byte[] SourceImageByte, Stream DestImageStream, int DX, int DY, int Width, int Height)
        {
            var SourceImageStream = SourceImageByte.ToStreamFromBytes();

            return DrawImage(SourceImageStream, DestImageStream, DX, DY, Width, Height);
        }

        public static Stream DrawText(this Stream SourceImageStream, string Text, int DX, int DY, int TextSize)
        {
            Font font = new Font("微软雅黑", TextSize, FontStyle.Regular);
            Brush brush = new SolidBrush(Color.Black);//填充的颜色

            return DrawText(SourceImageStream, Text, DX, DY, font, brush);
        }

        public static Stream DrawText(this Stream SourceImageStream, string Text, int DX, int DY, int TextSize, Color color)
        {
            Font font = new Font("微软雅黑", TextSize, FontStyle.Regular);
            Brush brush = new SolidBrush(color);//填充的颜色

            return DrawText(SourceImageStream, Text, DX, DY, font, brush);
        }

        public static Stream DrawText(this Stream SourceImageStream, string Text, int DX, int DY, Font f, Brush b)
        {
            Image SourceImage = Image.FromStream(SourceImageStream);

            Graphics g = Graphics.FromImage(SourceImage);

            g.SmoothingMode = SmoothingMode.AntiAlias;

            g.DrawString(Text, f, b, DX, DY);

            Stream stream = new MemoryStream();
            SourceImage.Save(stream, ImageFormat.Png);

            stream.Position = 0;
            return stream;
        }
        #endregion

        #region 保存方法
        public static void SaveToFile(this Stream SourceImageStream, string FileName)
        {
            Image SourceImage = Image.FromStream(SourceImageStream);
            SourceImage.Save(FileName, ImageFormat.Png);
            SourceImageStream.Close();
        }
        public static void SaveToFile(this Image SourceImage, string FileName)
        {
            SourceImage.Save(FileName, ImageFormat.Png);
        }
        #endregion

        #region 转换方法
        public static byte[] ToBytesFromStream(this Stream SourceSream)
        {
            SourceSream.Position = 0;

            byte[] bytes = new byte[SourceSream.Length];

            SourceSream.Read(bytes, 0, bytes.Length);
            SourceSream.Seek(0, SeekOrigin.Begin);

            return bytes;
        }

        public static Stream ToStreamFromBytes(this byte[] SourceBytes)
        {
            MemoryStream ms = new MemoryStream(SourceBytes);
            return ms;
        }

        public static Stream ToStreamFromImage(this Image SourceImage)
        {
            MemoryStream ms = new MemoryStream();
            SourceImage.Save(ms, ImageFormat.Png);
            return ms;
        }

        public static Stream ToStreamFromFile(this string FileName)
        {
            var fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            var bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Dispose();
            fs.Close();

            var ms = new MemoryStream(bytes);
            ms.Position = 0;
            return ms;
        }

        public static Image ToImageFromStream(this Stream SourceSream)
        {
            Image i = Image.FromStream(SourceSream);
            return i;
        }
        #endregion

        #region 高级方法
        public static Image SetCornerRadius(this Image SourceImage
            , int RadiusWidth = 5
            , bool LeftTopCorner = false
            , bool RightTopCorner = false
            , bool RightBottomCorner = false
            , bool LeftBottomCorner = false)
        {
            Bitmap b = (Bitmap)(SourceImage.Clone());
            Graphics g = Graphics.FromImage(b);

            int Width = b.Width;
            int Height = b.Height;

            int MinDest = Width > Height ? Height : Width;

            if (RadiusWidth > MinDest / 2)
            {
                RadiusWidth = MinDest / 2;
            }

            if (LeftTopCorner)
            {
                for (int x = 0; x < RadiusWidth; x++)
                {
                    for (int y = 0; y < RadiusWidth; y++)
                    {
                        if (Math.Pow(x - RadiusWidth, 2) + Math.Pow(y - RadiusWidth, 2) > Math.Pow(RadiusWidth, 2))
                        {
                            b.SetPixel(x, y, Color.Transparent);
                        }
                    }
                }
            }

            if (RightTopCorner)
            {
                for (int x = Width - RadiusWidth; x < Width; x++)
                {
                    for (int y = 0; y < RadiusWidth; y++)
                    {
                        if (Math.Pow(x - (Width - RadiusWidth), 2) + Math.Pow(y - RadiusWidth, 2) > Math.Pow(RadiusWidth, 2))
                        {
                            b.SetPixel(x, y, Color.Transparent);
                        }
                    }
                }
            }


            if (RightBottomCorner)
            {
                for (int x = Width - RadiusWidth; x < Width; x++)
                {
                    for (int y = Height - RadiusWidth; y < Height; y++)
                    {
                        if (Math.Pow(x - (Width - RadiusWidth), 2) + Math.Pow(y - (Height - RadiusWidth), 2) > Math.Pow(RadiusWidth, 2))
                        {
                            b.SetPixel(x, y, Color.Transparent);
                        }
                    }
                }
            }

            if (LeftBottomCorner)
            {
                for (int x = 0; x < RadiusWidth; x++)
                {
                    for (int y = Height - RadiusWidth; y < Height; y++)
                    {
                        if (Math.Pow(x - RadiusWidth, 2) + Math.Pow(y - (Height - RadiusWidth), 2) > Math.Pow(RadiusWidth, 2))
                        {
                            b.SetPixel(x, y, Color.Transparent);
                        }
                    }
                }
            }

            return b;
        }

        public static Image Resize(this Image image, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(b);
            g.Clear(Color.Transparent);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(image, 0, 0, width, height);
            g.Dispose();
            return b;
        }

        /// <summary>
        /// 映射图片
        /// </summary>
        /// <param name="ImageSource">原图</param>
        /// <param name="MaskImage">映射的图</param>
        /// <param name="ClearColor">映射的图上要去掉的颜色</param>
        /// <param name="DX">原图开始的X位置</param>
        /// <param name="DY">原图开始的Y位置</param>
        /// <param name="Width">映射图的宽度</param>
        /// <param name="Height">映射图的高度</param>
        /// <returns></returns>
        public static Image Map(this Image ImageSource, Image MaskImage
            , Color ClearColor, int DX, int DY, int Width, int Height)
        {
            MaskImage = MaskImage.Resize(Width, Height);

            Bitmap b = (Bitmap)(ImageSource.Clone());
            Bitmap mask = (Bitmap)(MaskImage.Clone());


            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    if (mask.GetPixel(x, y).ToArgb() == ClearColor.ToArgb())
                    {
                        b.SetPixel(DX + x, DY + y, Color.Transparent);
                    }
                }
            }

            return b;
        }

        #endregion
    }
}
上一篇 下一篇

猜你喜欢

热点阅读