欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#生成缩略图的实现方法

程序员文章站 2024-02-11 22:06:16
复制代码 代码如下: private void savethumbnail(bitmap originbitmap, int width, int height,...

复制代码 代码如下:

 private void savethumbnail(bitmap originbitmap, int width, int height, string directory, string filename, string extension)
{
    var physicalpath = directory + filename + extension;

    using (var newimage = new bitmap(width, height))
    {
        using (var graphic = getgraphic(originbitmap, newimage))
        {
            graphic.drawimage(originbitmap, 0, 0, width, height);
            using (var encoderparameters = new encoderparameters(1))
            {
                encoderparameters.param[0] = new encoderparameter(encoder.quality, 100l);
                newimage.save(physicalpath,
                            imagecodecinfo.getimageencoders()
                                .where(x => x.filenameextension.contains(extension.toupperinvariant()))
                                .firstordefault(),
                            encoderparameters);
            }
        }
    }           
}

private graphics getgraphic(image originimage, bitmap newimage)
{
    newimage.setresolution(originimage.horizontalresolution, originimage.verticalresolution);
    var graphic = graphics.fromimage(newimage);
    graphic.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
    graphic.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
    graphic.pixeloffsetmode = system.drawing.drawing2d.pixeloffsetmode.highquality;
    graphic.compositingquality = system.drawing.drawing2d.compositingquality.highquality;
    return graphic;
}