zoukankan      html  css  js  c++  java
  • c#工具类之Bitmap缩放帮忙类

    using System.Drawing;
    using System.Drawing.Drawing2D;
    
        /// <summary>
        /// BitmapHelper
        /// </summary>
        public static class BitmapScaleHelper
        {
            /// <summary>
            /// 缩放图片
            /// </summary>
            /// <param name="bitmap">原图片</param>
            /// <param name="width">新图片宽度</param>
            /// <param name="height">新图片高度</param>
            /// <returns>新图片</returns>
            public static Bitmap ScaleToSize(this Bitmap bitmap, int width, int height)
            {
                if (bitmap.Width == width && bitmap.Height == height)
                {
                    return bitmap;
                }
    
                var scaledBitmap = new Bitmap(width, height);
                using (var g = Graphics.FromImage(scaledBitmap))
                {
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(bitmap, 0, 0, width, height);
                }
    
                return scaledBitmap;
            }
    
            /// <summary>
            /// 缩放图片
            /// </summary>
            /// <param name="bitmap">原图片</param>
            /// <param name="size">新图片大小</param>
            /// <returns>新图片</returns>
            public static Bitmap ScaleToSize(this Bitmap bitmap, Size size)
            {
                return bitmap.ScaleToSize(size.Width, size.Height);
            }
    
            /// <summary>
            /// 按比例来缩放
            /// </summary>
            /// <param name="bitmap">原图</param>
            /// <param name="ratio">ratio大于1,则放大;否则缩小</param>
            /// <returns>新图片</returns>
            public static Bitmap ScaleToSize(this Bitmap bitmap, float ratio)
            {
                return bitmap.ScaleToSize((int)(bitmap.Width * ratio), (int)(bitmap.Height * ratio));
            }
    
            /// <summary>
            /// 按给定长度/宽度等比例缩放
            /// </summary>
            /// <param name="bitmap">原图</param>
            /// <param name="width">新图片宽度</param>
            /// <param name="height">新图片高度</param>
            /// <returns>新图片</returns>
            public static Bitmap ScaleProportional(this Bitmap bitmap, int width, int height)
            {
                float proportionalWidth, proportionalHeight;
    
                if (width.Equals(0))
                {
                    proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width;
                    proportionalHeight = height;
                }
                else if (height.Equals(0))
                {
                    proportionalWidth = width;
                    proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height;
                }
                else if (((float)width) / bitmap.Size.Width * bitmap.Size.Height <= height)
                {
                    proportionalWidth = width;
                    proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height;
                }
                else
                {
                    proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width;
                    proportionalHeight = height;
                }
    
                return bitmap.ScaleToSize((int)proportionalWidth, (int)proportionalHeight);
            }
    
            /// <summary>
            /// 按给定长度/宽度缩放,同时可以设置背景色
            /// </summary>
            /// <param name="bitmap">原图</param>
            /// <param name="backgroundColor">背景色</param>
            /// <param name="width">新图片宽度</param>
            /// <param name="height">新图片高度</param>
            /// <returns></returns>
            public static Bitmap ScaleToSize(this Bitmap bitmap, Color backgroundColor, int width, int height)
            {
                var scaledBitmap = new Bitmap(width, height);
                using (var g = Graphics.FromImage(scaledBitmap))
                {
                    g.Clear(backgroundColor);
    
                    var proportionalBitmap = bitmap.ScaleProportional(width, height);
    
                    var imagePosition = new Point((int)((width - proportionalBitmap.Width) / 2m), (int)((height - proportionalBitmap.Height) / 2m));
                    g.DrawImage(proportionalBitmap, imagePosition);
                }
    
                return scaledBitmap;
            }
        }
  • 相关阅读:
    python基础008----Python中类/函数/模块的简单介绍
    linux基础004---用户管理、用户登录注销、系统关机重启
    python基础006----流程控制&推导式&深浅拷贝
    python基础005----字典&集合
    python基础004----列表&元组
    难缠的布隆过滤器,这次终于通透了
    C# 位图BitArray 小试牛刀
    以步步为营的风格解读 Redis分布式锁
    你是不是对MD5算法有误解?
    最适合新手的Redis Cluster搭建过程
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/6669951.html
Copyright © 2011-2022 走看看