首页 > 编程语言 >C#图片处理之:图片缩放和剪裁

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

时间:2022-11-14 17:55:55浏览次数:62  
标签:Width img 缩放 C# Bitmap int new Height 图片

一、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# 实现中文转颜色 - 实现根据名字自动生成头像

标签:Width,img,缩放,C#,Bitmap,int,new,Height,图片
From: https://www.cnblogs.com/tianma3798/p/16889800.html

相关文章

  • [Mac]在Mac上关闭Microsoft AutoUpdate弹框提示
    打开terminal终端cd/Library/Application\Support/Microsoft/MAU2.0输入文件地址sudochmod000Microsoft\AutoUpdate.app将此应用程序权限设置为000输入密码......
  • EasyCVR平台国标设备接入,视频播放不稳定是什么原因?
    EasyCVR平台支持视频汇聚管理,能兼容多类型设备接入,可提供的视频功能包括:视频监控、直播录像、云存储、检索回看、智能告警、平台级联等。EasyCVR具备较强的视频能力以及AI......
  • Calico安装
    一、calico简介查看calico支持的kubernetes版本:https://projectcalico.docs.tigera.io/archive/v3.24/getting-started/kubernetes/requirements在线下载calico.yaml配置......
  • Apple Safari 16.1 - macOS 专属浏览器 (独立安装包下载)
    Safari浏览器16.1forMontery,BigSur请访问原文链接:AppleSafari16.1-macOS专属浏览器(独立安装包下载),查看最新版。原创作品,转载请保留出处。作者主页:www.sys......
  • 阿里云 ACK 接入观测云
    简介容器服务Kubernetes版(简称ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。2021年成为国内唯一连续三年入选Gartner公共云容器报......
  • cfssl生成链式自签名证书
    生成大纲总共生成三个证书,一个根证书,一个中间证书签发商,一个服务证书。为方便理解,根证书表示为ca0,中间证书表示为ca1,服务证书表示为server。在本文中,服务证书为生成给harb......
  • Oracle group by over(partition by order by)相关
    快速理解:groupby使用一个(多个)含重复数据的字段进行表数据合算(聚合),结果集展示聚合结果。partitionby同样适用于含重复数据的一个(多个)字段,但是不进行聚合,只是在结果集......
  • CF1750F Majority
    题面传送门看到这个题目觉得非常神奇。首先我们考虑设\(g_i\)表示\(i\)长度的答案,但是显然不好转移。考虑容斥,用总方案数减去不能消成一个的方案数,这里的总方案数要求两......
  • C++软件编码规范推荐--命名规则
    1.背景  命名规则对于代码开发和理解很重要,是作为一名合格的设计师具备的良好代码习惯。2.命名规则2.1匈牙利命名法  基本原则:变量名=属性+类型+对象描述......
  • EasyCVR调用设备录像回放接口,无法播放录像是什么原因?
    EasyCVR视频融合云服务支持多协议、多类型的设备接入,平台可提供视频监控直播、云端录像、云存储、录像检索与回看、智能告警、级联等功能。在录像功能上,EasyCVR可支持云端录......