c#(.net)制作压缩缩略图图片

 

public class ImageHelper
{
    public ImageHelper()
 {
 }
   
    /// <summary>
    /// 判断制定路径下是否存在新文件
    /// </summary>
    /// <param name="fullPath">源文件的完整路径</param>
    /// <returns>新文件的完整路径</returns>
    private string ExistsOrCreateFile(string fullPath)
    {
        int num = 0;
        string fileName = System.IO.Path.GetFileNameWithoutExtension(fullPath);
        string extendName = System.IO.Path.GetExtension(fullPath);
        string path = System.IO.Path.GetDirectoryName(fullPath);

        StringBuilder newPath = new StringBuilder();

        while (true)
        {
            newPath.Length = 0;

            newPath.Append(path+"\\");
            newPath.Append(fileName);
            newPath.Append("_small"+num);
            newPath.Append(extendName);

            if (!System.IO.File.Exists(newPath.ToString()))
                break;

            num++;           
        }

        return newPath.ToString();
    }

    /// <summary>
    /// 生成缩略图片
    /// </summary>
    /// <param name="filePath">源图片的完整路径</param>
    /// <param name="_width">缩略图的宽</param>
    /// <param name="_height">高</param>
    /// <returns></returns>
    public string Image(string filePath,int _width, int _height)
    {
        try
        {
           
            using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(filePath))
            {
                //原图宽度和高度   
                int width = sourceImage.Width;
                int height = sourceImage.Height;
                int smallWidth;
                int smallHeight;

                //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高)   
                if (((decimal)width) / height <= ((decimal)_width) / _height)
                {
                    smallWidth = _width;
                    smallHeight = _width * height / width;
                }
                else
                {
                    smallWidth = _height * width / height;
                    smallHeight = _height;
                }

                //判断缩略图在当前文件夹下是否同名称文件存在                 
                //缩略图保存的绝对路径   
                string smallImagePath = ExistsOrCreateFile(filePath);

                //新建一个图板,以最小等比例压缩大小绘制原图   
                using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
                {
                      
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                    {
                       
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.Clear(Color.Black);
                        g.DrawImage(
                        sourceImage,
                        new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
                        new System.Drawing.Rectangle(0, 0, width, height),
                        System.Drawing.GraphicsUnit.Pixel
                        );
                        g.Dispose();
                        bitmap.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }                

                }

                return System.IO.Path.GetFileName(smallImagePath);
            }
        }
        catch
        {
            return "图片格式不正确";
        }
    }
    

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值