• 大小: 2.74MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-09-29
  • 语言: 其他
  • 标签: C#  

资源简介

此代码实现了DCT压缩图片的整个过程,包含交互的界面,可根据输入的压缩率等进行图片压缩

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ImageCompress
{
    class DCT
    {
        #region Public interface

        // 事件处理
        public delegate void OnImageProcessedHandler(Bitmap ProcessedImage);
        public static event OnImageProcessedHandler OnImageProcessed;

        // 类的唯一公共接口
        public static void compressImage(Bitmap img int coefficient int DistortionRate int compressionRate)
        {
            // 初始化入口
            double[] alpha = create_alpha(coefficient);

            // 得到像素的RGB值
            double[] R; double[] G; double[] B;
            convertToArray(img out R out G out B coefficient);

            // DCT
            R = dct(R coefficient alpha);
            G = dct(G coefficient alpha);
            B = dct(B coefficient alpha);

            // 压缩
            compress(ref R coefficient DistortionRate compressionRate);
            compress(ref G coefficient DistortionRate compressionRate);
            compress(ref B coefficient DistortionRate compressionRate);

            // IDCT
            R = idct(R coefficient alpha);
            G = idct(G coefficient alpha);
            B = idct(B coefficient alpha);

            if (OnImageProcessed != null) OnImageProcessed(convertToImage(R G B img.Size));
        }

        #endregion

        #region DCT & IDCT

        //离散余弦变换
        private static double[] dct(double[] imgArray int coefficient double[] alpha)
        {
            int width = imgArray.GetLength(0);
            int height = imgArray.GetLength(1);

            // 构建矩阵
            double[] C = new double[width height];

            // 遍历块
            for (int x = 0; x < width; x += coefficient)
            {
                for (int y = 0; y < height; y += coefficient)
                {
                    // 遍历块内的像素点
                    double[] f = clipArray(imgArray coefficient x y);

                    for (int u = x; u < coefficient + x; ++u)
                    {
                        for (int v = y; v < coefficient + y; ++v)
                        {
                            // 计算DCT总和
                            double sum = 0;
                            for (int ii = 0; ii < coefficient; ++ii)
                            {
                                for (int jj = 0; jj < coefficient; ++jj)
                                {
                                    sum += f[ii jj] * Math.Cos((2 * ii + 1) * (u - x) * Math.PI / (2 * coefficient)) *
                                                     Math.Cos((2 * jj + 1) * (v - y) * Math.PI / (2 * coefficient));
                                }
                            }
                            C[u v] = alpha[u - x] * alpha[v - y] * sum;
                        }
                    }
              

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-25 21:48  222017321062028 李姗姗 交互实验7-9\
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\
     文件      478208  2018-12-25 21:21  222017321062028 李姗姗 交互实验7-9\ImageCompress.exe
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\
     文件       62976  2018-12-25 21:42  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\.suo
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\Server\
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\Server\sqlite3\
     文件           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\Server\sqlite3\db.lock
     文件      606208  2018-12-25 10:21  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\Server\sqlite3\storage.ide
     文件       32768  2018-12-25 21:38  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\Server\sqlite3\storage.ide-shm
     文件     4140632  2018-12-25 21:38  222017321062028 李姗姗 交互实验7-9\ImageCompress\.vs\ImageCompress\v15\Server\sqlite3\storage.ide-wal
     目录           0  2018-12-25 21:37  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\
     文件        2055  2018-12-25 21:42  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress.sln
     文件         189  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\App.config
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\bin\
     目录           0  2018-12-24 22:53  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\bin\Debug\
     文件      478208  2018-12-25 21:21  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\bin\Debug\ImageCompress.exe
     文件         189  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\bin\Debug\ImageCompress.exe.config
     文件       44544  2018-12-25 21:21  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\bin\Debug\ImageCompress.pdb
     文件       10610  2018-12-25 21:36  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\DCT.cs
     文件        5596  2018-12-25 21:37  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\Form1.cs
     文件       17431  2018-12-25 10:21  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\Form1.Designer.cs
     文件      667658  2018-12-25 10:21  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\Form1.resx
     文件        3648  2018-12-24 22:53  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\ImageCompress.csproj
     目录           0  2018-12-24 20:49  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\obj\
     目录           0  2018-12-25 10:21  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\obj\Debug\
     文件        1443  2018-12-25 00:00  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7220  2018-12-24 22:28  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件          42  2018-12-24 22:28  222017321062028 李姗姗 交互实验7-9\ImageCompress\ImageCompress\obj\Debug\ImageCompress.csproj.CoreCompileInputs.cache
............此处省略20个文件信息

评论

共有 条评论