管理

图片工具类 - C#小函数类推荐

Posted on 2024-09-21 09:30  lzhdim  阅读(10029)  评论(0编辑  收藏  举报

       此文记录的是图像操作的工具类。

/***

    图片工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

    使用方法例子:
        Bitmap originalImage = new Bitmap("path_to_image");
        Bitmap resizedImage = ResizeImage(originalImage, 100, 100);
        resizedImage.Save("path_to_save_resized_image");

***/

namespace Lzhdim.LPF.Utility
{
    using System.Drawing;

    /// <summary>
    /// 图片工具类
    /// </summary>
    internal class ImageUtil
    {
        /// <summary>
        /// 改变图片大小
        /// </summary>
        /// <param name="original">原始图片</param>
        /// <param name="newWidth">新宽度</param>
        /// <param name="newHeight">新高度</param>
        /// <returns>图片</returns>
        public static Bitmap ResizeImage(Bitmap original, int newWidth, int newHeight)
        {
            // 创建一个新的Bitmap,其尺寸为新的尺寸
            Bitmap resized = new Bitmap(newWidth, newHeight);

            using (Graphics g = Graphics.FromImage(resized))
            {
                // 设置绘画质量
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(original, 0, 0, newWidth, newHeight);
            }

            return resized;
        }
    }
}

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved