• C#下的BMP图像压缩类


    这几天研究比较多,其中一个成果就是下面这个图像压缩类。可以把BMP文件压成任意质量的JPEG,在.net framework 2.0下编译通过。有时间的话我会把它写成可以压缩其他格式的类,其实改一下参数就可以了。

    时间原因没有写注释,(不过这个类真够简单了)还是介绍一下吧:

    只有一个没有重载的构造函数,参数是待压缩BMP文件的路径,还有一个长整形的质量参数,在0-100之间取值。

    调用encodeImg()就可以完成压缩工作了,需要一个JPEG文件输出路径的参数。

    如果你要在你的C#工程里面用这个类,一定要把代码保存在ImageEncoder.cs文件里面。当然你也可以把它编译成DLL文件。

    就说这么多了,这个类算是非常简单的,网上有一个代码可以实现相同功能,不过那是一个C#命令行程序。我把它封装了一下,希望能够给大家带来一些方便! :-)

    //Copyright @ xiedi,2006
    //Mail to xiedidan@yeah.net if you have questions or good suggestions.
    //SOME RIGHTS RESERVED. You can use this code freely WITH THIS COMMENT.

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;

    namespace UserLib.ImgEncoder
    {
        public class ImageEncoder
        {
            private String bitmapPath;
            private String jpegPath;
            private Bitmap bitmap;
            private ImageCodecInfo codecInfo;
            private EncoderParameters parameters;
            private EncoderParameter parameter;
            private Encoder encoder;

            public ImageEncoder(String path, long quality)
            {
                this.bitmapPath = path;
                this.bitmap = new Bitmap(this.bitmapPath);
                this.codecInfo = GetEncoderInfo("image/jpeg");
                this.encoder = Encoder.Quality;
                this.parameters = new EncoderParameters(1);
                this.parameter = new EncoderParameter(encoder, quality);
                this.parameters.Param[0] = this.parameter;
            }

            public void encodeImg(String path)
            {
                this.jpegPath = path;
                this.bitmap.Save(this.jpegPath, this.codecInfo, this.parameters);
            }

            private ImageCodecInfo GetEncoderInfo(String mimeType)
            {
                int j;
                ImageCodecInfo[] encoders;
                encoders = ImageCodecInfo.GetImageEncoders();
                for (j = 0; j < encoders.Length; ++j)
                {
                    if (encoders[j].MimeType == mimeType)
                        return encoders[j];
                }
                return null;
            }
        }
    }

  • 相关阅读:
    异常:Batch update returned unexpected row count from update [0]; actual row count: 0;
    python UI/API 环境配置
    selenium xpath定位
    python webdriver 的安装与下载
    python面试题
    fiddler工具使用及手机抓包
    jmeter 常用函数
    Jmeter常用插件——Stepping Thread Group ,JMETER以及关于数据库性能分析
    Jmeter Monitor监控
    python 常用快捷键
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1795920.html
Copyright © 2020-2023  润新知