C#创建缩略图的操作类源码

将代码过程中经常用的一些代码片段收藏起来,如下的代码段是关于C#创建缩略图的操作类的代码,应该对小伙伴有一些用。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

namespace HtmlSnap
{
public static class ImageHelper
{

    public static Image GetThumbnailImage(Image image, int width, int height)
    {
        if (image == null || width < 1 || height < 1)
            return null;

        Image bitmap = new System.Drawing.Bitmap(width, height);

        using (Graphics g = System.Drawing.Graphics.FromImage(bitmap))
        {

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

            g.Clear(Color.Transparent);

            g.DrawImage(image, new Rectangle(0, 0, width, height),
                new Rectangle(0, 0, image.Width, image.Height),
                GraphicsUnit.Pixel);

            return bitmap;
        }
    }

    public static Image GetThumbnailImageKeepRatio(Image image, int width, int height)
    {
        Size imageSize = GetImageSize(image, width, height);
        return GetThumbnailImage(image, imageSize.Width, imageSize.Height);
    }

    public static Size GetImageSize(Image picture, int percent)
    {
        if (picture == null || percent < 1)
            return Size.Empty;


        return GetImageSize(picture, width, height);
    }

    public static Size GetImageSize(Image picture, int width, int height)
    {
        if (picture == null || width < 1 || height < 1)
            return Size.Empty;

        Size imageSize;

        imageSize = new Size(width, height);

        double heightRatio = (double)picture.Height / picture.Width;
        double widthRatio = (double)picture.Width / picture.Height;

        int desiredHeight = imageSize.Height;
        int desiredWidth = imageSize.Width;


        imageSize.Height = desiredHeight;
        if (widthRatio > 0)

        if (imageSize.Width > desiredWidth)
        {
            imageSize.Width = desiredWidth;
        }

        return imageSize;
    }


    public static ImageCodecInfo GetCodecInfo(string mimeType)
    {
        ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
        foreach (ImageCodecInfo ici in CodecInfo)
        {
            if (ici.MimeType == mimeType) return ici;
        }
        return null;
    }

    public static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
    {
        ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();

        foreach (ImageCodecInfo icf in encoders)
        {
            if (icf.FormatID == format.Guid)
            {
                return icf;
            }
        }

        return null;
    }

    public static void SaveImage(Image image, string savePath, ImageFormat format)
    {
        SaveImage(image, savePath, GetImageCodecInfo(format));
    }

    private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
    {
        EncoderParameters parms = new EncoderParameters(1);
        EncoderParameter parm = new EncoderParameter(Encoder.Quality, ((long)95));
        parms.Param[0] = parm;

        image.Save(savePath, ici, parms);
        parms.Dispose();
    }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值