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

C# 实现图片压缩

程序员文章站 2022-03-26 10:25:29
代码: private static ImageCodecInfo GetImageCodecInfo(ImageFormat imageFormat) { ImageCodecInfo[] imageCodecInfoArr = ImageCodecInfo.GetImageDecoders(); ......

代码:

C# 实现图片压缩
private static imagecodecinfo getimagecodecinfo(imageformat imageformat)
{
    imagecodecinfo[] imagecodecinfoarr = imagecodecinfo.getimagedecoders();
    foreach (imagecodecinfo imagecodecinfo in imagecodecinfoarr)
    {
        if (imagecodecinfo.formatid == imageformat.guid)
        {
            return imagecodecinfo;
        }
    }
    return null;
}
view code

代码:

C# 实现图片压缩
memorystream ms = httputil.httpdownloadfile(url);
bitmap bmp = new bitmap(ms);

encoderparameters encoderparameters = new encoderparameters(1);
encoderparameter encoderparameter = new encoderparameter(system.drawing.imaging.encoder.quality, 75l);
encoderparameters.param[0] = encoderparameter;

memorystream mscompress = new memorystream();
bmp.save(mscompress, getimagecodecinfo(imageformat.jpeg), encoderparameters);
bitmap bmpcompress = new bitmap(mscompress);
bmpcompress.save(path);
bmp.save(path2);

mscompress.close();
ms.close();
view code