C#图片处理之:图片缩放和剪裁

一、C#图片处理之:图片缩放和剪裁 封装

此代码是针对.net core 的

使用如下:

        public static void ImgOne()
        {
            // Image

            //  Bitmap

            ImageOperate.Cut(@"E:\MyDll\QLCommon\test\1.jpg", @"E:\MyDll\QLCommon\test\11.jpg",100, 100);
            Console.WriteLine("操作成功");
        }

结果如下:

 

 

二、代码封装如下:

 /// <summary>
    /// 图片处理使用封装
    /// </summary>
    public class ImageOperate
    {
        /// <summary>
        /// 图片剪切,固定格式jpg
        /// 最大化缩放
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <exception cref="Exception"></exception>
        public static void Cut(string source, string target, int width, int height)
        {
            //
            if (File.Exists(source) == false)
                throw new Exception("图片文件不存在");
            Image img = Bitmap.FromFile(source);
            //图片缩放
            int realWidth = 0,
                realHeight = 0,
                realLeft = 0,
                realTop = 0;
            if (img.Width > img.Height)
            {
                realHeight = height;
                realWidth = Convert.ToInt32(height * 1.0 / img.Height * img.Width);
                realLeft = (img.Width - realWidth) / 2;
                realTop = 0;
            }
            else
            {
                realWidth = width;
                realHeight = Convert.ToInt32(width * 1.0 / img.Width * img.Height);
                realLeft = 0;
                realTop = (img.Height - realHeight) / 2;
            }
            Bitmap oldMap = ResizeBitmap(new Bitmap(img), realWidth, realHeight);
            int left = Convert.ToInt32((oldMap.Width - width) / 2.0);
            int top = Convert.ToInt32((oldMap.Height - height) / 2.0);
            //剪切
            Bitmap map = CutBitmap(oldMap, left, top, width, height);

            //保存到磁盘
            map.Save(target, ImageFormat.Jpeg);
        }
        /// <summary>
        /// 等比例缩放图片
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="newW"></param>
        /// <param name="newH"></param>
        /// <returns></returns>
        public static Bitmap ResizeBitmap(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH),
                    new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 剪裁 -- 用GDI+
        /// </summary>
        /// <param name="b">原始Bitmap</param>
        /// <param name="StartX">开始坐标X</param>
        /// <param name="StartY">开始坐标Y</param>
        /// <param name="iWidth">宽度</param>
        /// <param name="iHeight">高度</param>
        /// <returns>剪裁后的Bitmap</returns>
        public static Bitmap CutBitmap(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }

            int w = b.Width;
            int h = b.Height;

            if (StartX >= w || StartY >= h)
            {
                return null;
            }

            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }

            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }

            try
            {
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight),
                    new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                g.Dispose();
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
    }

 

更多:

GDI+ 位图介绍

.Net Core GDI+ 使用基础

C# 实现中文转颜色 - 实现根据名字自动生成头像

posted @ 2022-11-14 17:48  天马3798  阅读(1076)  评论(0编辑  收藏  举报