zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }

  • 相关阅读:
    expdp和impdp用法详解
    Shiro-JWT SpringBoot前后端分离权限认证的一种思路
    Maven Helper 插件-解决依赖冲突
    maven打包之resource配置
    sparkstreaming direct方式读取kafka(0.10版本)数据, 并手动维护offset
    java向kafka发送消息
    idea maven整合log4j
    java设计模式: 工厂方法模式
    异地购房使用武汉公几斤商dai转公几斤dai款
    java设计模式: 单例设计模式
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1795920.html
Copyright © 2011-2022 走看看