图片压缩工具

来源:互联网 发布:淘宝店网页设计 编辑:程序博客网 时间:2024/04/28 21:59

本人从事MCU,为了减少图片占用的空间,自己编写了一个工具,由于初学谢谢大家指教;编写环境VS2008;

首先创建一个BMP图片类,里面包括在PICBOX中显示图片(以像素形式),压缩图片,解压图片等函数;

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Collections;
using System.Drawing.Drawing2D;

namespace PP
{
    
class CBitmap
    
{
        
//位图头文件
        private const uint BFTYPE = 0x4D42;    //说明文件的类型.(该值必需是0x4D42,也就是字符'BM'。我们不需要判断OS/2的位图标识,这么做现在来看似乎已经没有什么意义了,而且如果要支持OS/2的位图,程序将变得很繁琐。所以,在此只建议你检察'BM'标识)
        private uint bfSize;    //说明文件的大小,用字节为单位
        private const uint BFRESERVED1 = 0;   //保留,必须设置为0
        private const uint BFRESERVED2 = 0;   //保留,必须设置为0
        private uint bfOffBits; //说明从文件头开始到实际的图象数据之间的字节的偏移量。这个参数是非常有用的,因为位图信息头和调色板的长度会根据不同情况而变化,所以你可以用这个偏移值迅速的从文件中读取到位数据。
        
//位图信息头
        private uint biWidth;   //说明图象的宽度,以象素为单位
        private uint biHeight;  //说明图象的高度,以象素为单位。注:这个值除了用于描述图像的高度之外,它还有另一个用处,就是指明该图像是倒向的位图,还是正向的位图。如果该值是一个正数,说明图像是倒向的,如果该值是一个负数,则说明图像是正向的。大多数的BMP文件都是倒向的位图,也就是时,高度值是一个正数。(注:当高度值是一个负数时(正向图像),图像将不能被压缩(也就是说biCompression成员将不能是BI_RLE8或BI_RLE4)。
        private uint byWidth;
        
private const uint BIPLANES = 1;  //为目标设备说明位面数,其值将总是被设为1
        private uint biBitCount;    //说明比特数/象素,其值为1、2、4、8、16、24
        private uint biCompression = 0//说明图象数据压缩的类型。其值可以是下述值之一: 1.BI_RGB:没有压缩; 2.BI_RLE8:每个象素8比特的RLE压缩编码,压缩格式由2字节组成(重复象素计数和颜色索引); 3.BI_RLE4:每个象素4比特的RLE压缩编码,压缩格式由2字节组成; 4.BI_BITFIELDS:每个象素的比特由指定的掩码决定。
        private uint biSizeImage;   //说明图象的大小,以字节为单位。当用BI_RGB格式时,可设置为0
        private const long BIXPELSPERMETER = 0;   //说明水平分辨率,用象素/米表示
        private const long BIYPELSPERMETER = 0;   //说明垂直分辨率,用象素/米表示
        private uint biClrUsed = 0//说明位图实际使用的彩色表中的颜色索引数(设为0的话,则说明使用所有调色板项)
        private uint biClrImportant = 0;    //说明对图象显示有重要影响的颜色索引的数目,如果是0,表示都重要。
        
//彩色表
        private byte[,] bmiColors;   //说明彩色表RGBQUAD结构的阵列,其中包含索引图像的真实RGB值
        
//图象数据阵列字节
        private byte[,] BYTE;    //图象数据
        
//文件信息
        private string FileName = "NewBitmap";
        
private string FilePath = "";
        
//画点大小
        private int PixelSize = 10;
        
//记录最大篇幅,用于宽和高改变时
        private uint MaxBiHeight = 0;
        
private uint MaxBiWidth = 0;

        
public CBitmap(string FileFullName,byte[] BITMAP_DATA)  //打开图片构造函数
        {
            
//set file mess
            FileName = Path.GetFileNameWithoutExtension(FileFullName);
            FilePath 
= Path.GetDirectoryName(FileFullName);
            
//set bfSize value
            bfSize = Convert.ToUInt32(BITMAP_DATA.Length);
            
//set bfOffBits value
            bfOffBits = SetDWordTypeValue(BITMAP_DATA,0x0A);
            
//set biWidth value
            MaxBiWidth = biWidth = SetDWordTypeValue(BITMAP_DATA, 0x12);
            
//set biHeight value
            MaxBiHeight = biHeight = SetDWordTypeValue(BITMAP_DATA, 0x16);
            
//set biBitCount value
            uint BTemp0 = BITMAP_DATA[0x1C];
            
uint BTemp1 = BITMAP_DATA[0x1D];
            BTemp1 
<<= 8;
            biBitCount 
= BTemp0 + BTemp1;
            
//set byWidth value
            Set_ByWidth_Value();
            
//set biCompression value
            biCompression = SetDWordTypeValue(BITMAP_DATA,0x1E);
            
//set biSizeImage value
            biSizeImage = SetDWordTypeValue(BITMAP_DATA,0x22);
            
//set biClrUsed value
            biClrUsed = SetDWordTypeValue(BITMAP_DATA,0x2E);
            
//set biClrImportant value
            biClrImportant = SetDWordTypeValue(BITMAP_DATA,0x32);
            
//set bmiColors value
            uint biColor = (bfOffBits - 0x36/ 4;
            bmiColors 
= new byte[biColor,4];
            
uint buffIndex = 0x36;
            
for (int iTemp = 0; iTemp < biColor; ++iTemp)
            
{
                bmiColors[iTemp, 
0= BITMAP_DATA[buffIndex];
                bmiColors[iTemp, 
1= BITMAP_DATA[buffIndex + 1];
                bmiColors[iTemp, 
2= BITMAP_DATA[buffIndex + 2];
                bmiColors[iTemp, 
3= BITMAP_DATA[buffIndex + 3];
                buffIndex 
+= 4;
            }

            
//set biSize value
            if (biSizeImage == 0 || biSizeImage != bfSize - bfOffBits)
            
{
                biSizeImage 
= Convert.ToUInt32(bfSize - bfOffBits);
            }

            
//set BYTE value
            BYTE = new byte[biHeight, biSizeImage / biHeight];
            buffIndex 
= bfOffBits;
            
for (long iI = biHeight - 1; iI >= 0--iI)
            
{
                
for (long iJ = 0; iJ < biSizeImage / biHeight; ++iJ)
                
{
                    BYTE[iI, iJ] 
= BITMAP_DATA[bfOffBits];
                    
++bfOffBits;
                }

            }

        }


        
public CBitmap(uint IbiWidth, uint IbiHeight,uint IbiBitCount) //新建图片构造函数(暂时只能创建2色)
        {
            
//set biWidth value
            MaxBiWidth = biWidth = IbiWidth;
            
//set biHeight value
            MaxBiHeight = biHeight = IbiHeight;
            
//set biSizeImage value
            biSizeImage = Convert.ToUInt32(biHeight * (biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1+ (4 - ((biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1)) % 4)) % 4));
            
//set biBitCount value
            biBitCount = IbiBitCount;
            
//set byWidth value
            Set_ByWidth_Value();
            
//set bmiColors value
            
//bmiColors = new uint[2,4];
            bmiColors = new byte[2,4]{{0x00,0x00,0x00,0x00},{0xFF,0xFF,0xFF,0x00}};
            
//set bfOffBits value
            bfOffBits = 0x3E;
            
//set bfSize value
            bfSize = Convert.ToUInt32(bfOffBits + biSizeImage);
            
            BYTE 
= new byte[biHeight, biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1+ (4 - ((biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1)) % 4)) % 4];
            
for (long iI = biHeight - 1; iI >= 0--iI)
            
{
                
for (long iJ = 0; iJ < biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1+ (4 - ((biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1)) % 4)) % 4++iJ)
                
{
                    BYTE[iI, iJ] 
= 0xFF;    //为白色
                }

            }

        }


        
private void Set_ByWidth_Value()
        
{
            
switch (biBitCount)
            
{
                
case 1:
                    byWidth 
= Convert.ToUInt32(biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1));
                    
break;
                
case 2:
                    byWidth 
= Convert.ToUInt32(biWidth / 4 + (biWidth % 4 == 0 ? 0 : 1));
                    
break;
                
case 4:
                    byWidth 
= Convert.ToUInt32(biWidth / 2 + (biWidth % 2 == 0 ? 0 : 1));
                    
break;
                
case 8:
                    byWidth 
= biWidth;
                    
break;
                
case 16:
                    byWidth 
= biWidth * 2;
                    
break;
                
case 24:
                    byWidth 
= biWidth * 3;
                    
break;
                
default:
                    
break;
            }

        }


        
private uint SetDWordTypeValue(byte[] BITMAP_DATA,uint ITemp)
        
{
            
uint BTemp0, BTemp1, BTemp2, BTemp3;
            BTemp0 
= BITMAP_DATA[ITemp];
            BTemp1 
= BITMAP_DATA[ITemp + 1];
            BTemp1 
<<= 8;
            BTemp2 
= BITMAP_DATA[ITemp + 2];
            BTemp2 
<<= 16;
            BTemp3 
= BITMAP_DATA[ITemp + 3];
            BTemp3 
<<= 24;
            
return BTemp0 + BTemp1 + BTemp2 + BTemp3;
        }


        
public string FileNameProp
        
{
            
get
            
{
                
return FileName;
            }

        }


        
public string FilePathProp
        
{
            
get
            
{
                
return FilePath;
            }

        }


        
public int PixelSizeProp
        
{
            
get
            
{
                
return PixelSize;
            }

            
set
            
{
                PixelSize 
= value;
            }

        }


        
public uint WidthProp
        
{
            
get
            
{
                
return biWidth;
            }

        }


        
public uint ByWidthProp
        
{
            
get
            
{
                
return byWidth;
            }

        }


        
public uint HeightProp
        
{
            
get
            
{
                
return biHeight;
            }

        }


        
public Color Get_One_Point_Color_RGB(uint Line_Index,uint Line_Point_Index)
        
{
            Color PointColor_RGB 
= new Color();
            
if (biBitCount == 1)
            
{
                
byte BTemp = BYTE[Line_Index, Line_Point_Index / 8];
                
switch (Line_Point_Index % 8)
                
{
                    
case 0:
                        BTemp 
>>= 7;
                        
break;
                    
case 1:
                        BTemp 
>>= 6;
                        
break;
                    
case 2:
                        BTemp 
>>= 5;
                        
break;
                    
case 3:
                        BTemp 
>>= 4;
                        
break;
                    
case 4:
                        BTemp 
>>= 3;
                        
break;
                    
case 5:
                        BTemp 
>>= 2;
                        
break;
                    
case 6:
                        BTemp 
>>= 1;
                        
break;
                    
default:
                        
break;
                }

                BTemp 
&= 0x01;
                PointColor_RGB 
= Color.FromArgb(bmiColors[BTemp, 2], bmiColors[BTemp, 1], bmiColors[BTemp, 0]);
            }

            
else if (biBitCount == 2)
            
{
                
byte BTemp = BYTE[Line_Index, Line_Point_Index / 4];
                
switch (Line_Point_Index % 4)
                
{
                    
case 0:
                        BTemp 
>>= 6;
                        
break;
                    
case 1:
                        BTemp 
>>= 4;
                        
break;
                    
case 2:
                        BTemp 
>>= 2;
                        
break;
                    
default:
                        
break;
                }

                BTemp 
&= 0x03;
                PointColor_RGB 
= Color.FromArgb(bmiColors[BTemp, 2], bmiColors[BTemp, 1], bmiColors[BTemp, 0]);
            }

            
else if (biBitCount == 4)
            
{
                
byte BTemp = BYTE[Line_Index, Line_Point_Index / 2];
                
if (Line_Point_Index % 2 == 0)
                
{
                    BTemp 
>>= 4
                }

                BTemp 
&= 0x0F;
                PointColor_RGB 
= Color.FromArgb(bmiColors[BTemp, 2], bmiColors[BTemp, 1], bmiColors[BTemp, 0]);
            }

            
else if (biBitCount == 8)
            
{
                PointColor_RGB 
= Color.FromArgb(bmiColors[BYTE[Line_Index, Line_Point_Index], 2], bmiColors[BYTE[Line_Index, Line_Point_Index], 1], bmiColors[BYTE[Line_Index, Line_Point_Index], 0]);
            }

            
else if (biBitCount == 16)
            
{
                Line_Point_Index 
*= 2;
                
int ITemp = BYTE[Line_Index, Line_Point_Index + 1];
                ITemp 
<<= 8;
                ITemp 
+= BYTE[Line_Index, Line_Point_Index];
                PointColor_RGB 
= Color.FromArgb(bmiColors[ITemp, 2], bmiColors[ITemp, 1], bmiColors[ITemp, 0]);
            }

            
else  //biBitCount == 24
            {
                Line_Point_Index 
*= 3;
                PointColor_RGB 
= Color.FromArgb(BYTE[Line_Index, Line_Point_Index + 2], BYTE[Line_Index, Line_Point_Index + 1], BYTE[Line_Index, Line_Point_Index]);
            }

            
return PointColor_RGB;
        }


        
public string Show_Bitmap_HEX_Data()
        
{
            
string Str = "";
            
byte BPar = Get_One_Line_Last_Byte_Par();
            
for (uint ITemp = 0; ITemp < biHeight; ++ITemp)
            
{
                Str 
+= ".DB ";
                
for (uint JTemp = 0; JTemp < byWidth; ++JTemp)
                
{
                    
if (JTemp + 1 == byWidth && biBitCount < 8)
                    
{
                        Str 
+= ((255 - BYTE[ITemp, JTemp]) & BPar).ToString("x3"+ "h";
                    }

                    
else
                    
{
                        Str 
+= (255 - BYTE[ITemp, JTemp]).ToString("x3"+ "h";
                    }

                    
if (JTemp + 1 < byWidth)
                    
{
                        Str 
+= ",";
                    }

                }

                
if (ITemp + 1 < biHeight)
                
{
                    Str 
+= " ";
                }

            }

            
return Str;
        }


        
private byte Get_One_Line_Last_Byte_Par()
        
{
            
byte BPar = 0xFF;
            
if (biBitCount == 1)
            
{
                
switch (biWidth % 8)
                
{
                    
case 1:
                        BPar 
= 0x80;
                        
break;
                    
case 2:
                        BPar 
= 0xC0;
                        
break;
                    
case 3:
                        BPar 
= 0xE0;
                        
break;
                    
case 4:
                        BPar 
= 0xF0;
                        
break;
                    
case 5:
                        BPar 
= 0xF8;
                        
break;
                    
case 6:
                        BPar 
= 0xFC;
                        
break;
                    
case 7:
                        BPar 
= 0xFE;
                        
break;
                    
default:
                        
break;
                }

            }

            
else if (biBitCount == 2)
            
{
                
switch (biWidth % 4)
                
{
                    
case 1:
                        BPar 
= 0xC0;
                        
break;
                    
case 2:
                        BPar 
= 0xF0;
                        
break;
                    
case 3:
                        BPar 
= 0xFC;
                        
break;
                    
default:
                        
break;
                }

            }

            
else if (biWidth % 2 != 0)
            
{
                BPar 
= 0xF0;
            }

            
return BPar;
        }


        
public void Show_Bitmap(PaintEventArgs e,int PB_Size_Width,int PB_Size_Height)
        
{
            Graphics gmap 
= e.Graphics;
            Pen LightGrayPen 
= new Pen(Color.LightGray);
            Pen RedPen 
= new Pen(Color.Red, 2);
            SolidBrush SB 
= new SolidBrush(Color.Black);
            SolidBrush WhiteBrush 
= new SolidBrush(Color.White);
            
try
            
{
                
while (PixelSize != 1 && (PB_Size_Width <= PixelSize * biWidth + 4 || PB_Size_Height <= PixelSize * biHeight + 8))
                
{
                    
--PixelSize;
                }

                
if (PixelSize == 1 && (PB_Size_Width <= PixelSize * biWidth + 4 || PB_Size_Height <= PixelSize * biHeight + 8))
                
{
                    MessageBox.Show(
"图片过大!""Show_Bitmap", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    
return;
                }


                
int draw_coordinate_x = Convert.ToInt32((PB_Size_Width - PixelSize * biWidth) / 2);
                
int draw_coordinate_y = Convert.ToInt32((PB_Size_Height - PixelSize * biHeight) / 2);

                gmap.DrawRectangle(RedPen, draw_coordinate_x 
- 1, draw_coordinate_y - 1, PixelSize * biWidth + 3, PixelSize * biHeight + 3);
                gmap.FillRectangle(WhiteBrush, draw_coordinate_x, draw_coordinate_y, PixelSize 
* biWidth + 1, PixelSize * biHeight + 1);

                
for (uint line_index = 0; line_index < biHeight; ++line_index)
                
{
                    
for (uint line_point_index = 0; line_point_index < biWidth; ++line_point_index)
                    
{
                        
if (Get_One_Point_Color_RGB(line_index, line_point_index) == Color.FromArgb(0xFF0xFF0xFF))
                        
{
                            
if (PixelSize > 3)
                            
{
                                gmap.DrawRectangle(LightGrayPen, draw_coordinate_x, draw_coordinate_y, PixelSize, PixelSize);
                            }

                        }

                        
else
                        
{
                            SB 
= new SolidBrush(Get_One_Point_Color_RGB(line_index, line_point_index));
                            gmap.FillRectangle(SB, 
new Rectangle(draw_coordinate_x, draw_coordinate_y, PixelSize + 1, PixelSize + 1));
                        }


                        draw_coordinate_x 
+= PixelSize;
                    }


                    draw_coordinate_y 
+= PixelSize;
                    draw_coordinate_x 
= Convert.ToInt32((PB_Size_Width - PixelSize * biWidth) / 2);
                }

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Show_Bitmap", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            
finally
            
{
                LightGrayPen.Dispose();
                RedPen.Dispose();
                SB.Dispose();
                WhiteBrush.Dispose();
            }

        }


        
public Boolean Pack_Bitmap(ref string SText)
        
{
            
if (biWidth != 74 || biHeight != 32)
            
{
                MessageBox.Show(
"图片尺寸不对! " + FilePath + "/" + FileName, "Pack_Bitmap", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                
return false;
            }


            
byte[] PackDataBuff = new byte[biHeight * byWidth];
            
byte[] LevelPackResult = new byte[biHeight * byWidth + 3];
            
byte[] ApeakPackResult = new byte[biHeight * byWidth + 3];

            
byte BPar = Get_One_Line_Last_Byte_Par();

            
for (uint UIHeigh = 0; UIHeigh < biHeight; UIHeigh++)
                
for (uint UIWidth = 0; UIWidth < byWidth; UIWidth++)
                
{
                    PackDataBuff[UIHeigh 
* byWidth + UIWidth] = Convert.ToByte(255 - BYTE[UIHeigh, UIWidth]);
                    
if (UIWidth + 1 == byWidth && biBitCount < 8)
                    
{
                        PackDataBuff[UIHeigh 
* byWidth + UIWidth] &= BPar;
                    }

                }

            
            
uint SumLevel = PackDataProg(PackDataBuff, LevelPackResult);
            
if (SumLevel == 0return false;

            
for (int UIWidth = 0; UIWidth < byWidth; UIWidth++)
                
for (int UIHeigh = 0; UIHeigh < biHeight; UIHeigh++)
                
{
                    PackDataBuff[UIWidth 
* biHeight + UIHeigh] = Convert.ToByte(255 - BYTE[UIHeigh, UIWidth]);
                    
if (UIWidth + 1 == byWidth && biBitCount < 8)
                    
{
                        PackDataBuff[UIWidth 
* biHeight + UIHeigh] &= BPar;
                    }

                }


            
uint SumApeak = PackDataProg(PackDataBuff, ApeakPackResult);
            
if (SumApeak == 0return false;

            SText 
= "  .db  " + ((SumApeak > SumLevel ? 128 : 0+ ((SumLevel >= SumApeak ? SumApeak : SumLevel) > 255 ? 1 : 0)).ToString("x3"+ "h,";
            SText 
+= ((SumLevel >= SumApeak ? SumApeak : SumLevel) > 255 ? (SumLevel >= SumApeak ? SumApeak : SumLevel) - 256 : (SumLevel >= SumApeak ? SumApeak : SumLevel)).ToString("x3"+ "h   .db  ";
            
            
for (int ITemp = 0; ITemp < (SumLevel >= SumApeak ? SumApeak : SumLevel); ++ITemp)
            
{
                
if (SumLevel >= SumApeak)
                
{
                    SText 
+= ApeakPackResult[ITemp].ToString("x3"+ "h";
                }

                
else
                
{
                    SText 
+= LevelPackResult[ITemp].ToString("x3"+ "h";
                }


                
if (ITemp + 1 != (SumLevel >= SumApeak ? SumApeak : SumLevel))
                
{
                    
if ((ITemp + 1% 10 != 0)
                    
{
                        SText 
+= ",";
                    }

                    
else
                    
{
                        SText 
+= "   .db  ";
                    }

                }

            }

            
return true;
        }


        
private uint PackDataProg(byte[] PackDataBuff, byte[] ResultDataBuff)
        
{
            
try
            
{
                
uint PackLenSum = 0;
                
uint DataBuffIndex = 0;
                
uint ResultDataBuffIndex = 0;
                
uint DataBuffLen = Convert.ToUInt32(PackDataBuff.Length);
                
uint ResultDataBuffIndexBak = 0;
                
uint SumCount = 0;
                
while (DataBuffIndex < DataBuffLen)
                
{
                    ResultDataBuffIndexBak 
= ResultDataBuffIndex;
                    ResultDataBuff[
++ResultDataBuffIndex] = PackDataBuff[DataBuffIndex];
                    
++ResultDataBuffIndex;
                    
if ((DataBuffIndex + 1< DataBuffLen && (DataBuffIndex + 2< DataBuffLen && PackDataBuff[DataBuffIndex] == PackDataBuff[DataBuffIndex + 1&& PackDataBuff[DataBuffIndex + 1== PackDataBuff[DataBuffIndex + 2])
                    
{
                        SumCount 
= 3;
                        
byte PackDataTemp = PackDataBuff[DataBuffIndex];
                        DataBuffIndex 
+= 3;
                        
while (DataBuffIndex < DataBuffLen && PackDataBuff[DataBuffIndex] == PackDataTemp)
                        
{
                            
++DataBuffIndex;
                            
if (++SumCount >= 127)
                                
break;
                        }

                        ResultDataBuff[ResultDataBuffIndexBak] 
= Convert.ToByte(SumCount + 128);
                    }

                    
else
                    
{
                        SumCount 
= 1;
                        
++DataBuffIndex;
                        
while ((DataBuffIndex < DataBuffLen && (DataBuffIndex + 1< DataBuffLen && PackDataBuff[DataBuffIndex] != PackDataBuff[DataBuffIndex + 1]) || ((DataBuffIndex + 2< DataBuffLen && PackDataBuff[DataBuffIndex + 1!= PackDataBuff[DataBuffIndex + 2]))
                        
{
                            ResultDataBuff[ResultDataBuffIndex] 
= PackDataBuff[DataBuffIndex];
                            
++ResultDataBuffIndex;
                            
++DataBuffIndex;
                            
++PackLenSum;
                            
++SumCount;
                            
if (SumCount >= 127)
                                
break;
                        }

                        ResultDataBuff[ResultDataBuffIndexBak] 
= Convert.ToByte(SumCount);
                    }

                    PackLenSum 
+= 2;
                    
if (ResultDataBuffIndex >= ResultDataBuff.Length - 1)
                    
{
                        
break;
                    }

                }

                
return PackLenSum;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message 
+ " " + FilePath + "/" + FileName, "数据压缩", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                
return 0;
            }

        }


        
public Boolean UPack_Bitmap(string SText)
        
{
            
try
            
{
                
byte[] TBYTE = ASCIIEncoding.Default.GetBytes(SText);
                
uint TBYTE_INDEX = 0;
                
while (Convert.ToChar(TBYTE[TBYTE_INDEX++& 0xDF!= 'H') ;
                
byte UPACK_MODE = HEX_TO_DEC(TBYTE[TBYTE_INDEX - 3], TBYTE[TBYTE_INDEX - 2]);
                
while (Convert.ToChar(TBYTE[TBYTE_INDEX++& 0xDF!= 'H') ;
                
uint UPACK_LEN = Convert.ToUInt32(Convert.ToByte(UPACK_MODE & 0x7F<< 8);
                UPACK_MODE 
&= 0x80;
                UPACK_LEN 
+= HEX_TO_DEC(TBYTE[TBYTE_INDEX - 3], TBYTE[TBYTE_INDEX - 2]);
                
uint UW = 0, UH = 0byte PACK_BYTE = 0; Boolean BREAK_FLAG = false;
                
while (true)
                
{
                    
if (BREAK_FLAG == truebreak;
                    
while (Convert.ToChar(TBYTE[TBYTE_INDEX++& 0xDF!= 'H') ;
                    PACK_BYTE 
= HEX_TO_DEC(TBYTE[TBYTE_INDEX - 3], TBYTE[TBYTE_INDEX - 2]);
                    
if (PACK_BYTE > 128)
                    
{
                        PACK_BYTE 
-= 128;
                        
while (Convert.ToChar(TBYTE[TBYTE_INDEX++& 0xDF!= 'H') ;
                        
while (PACK_BYTE-- != 0)
                        
{
                            BYTE[UH, UW] 
= Convert.ToByte(255 - HEX_TO_DEC(TBYTE[TBYTE_INDEX - 3], TBYTE[TBYTE_INDEX - 2]));
                            
if (BREAK_FLAG = Inc_Height_Width(UPACK_MODE, ref UH, ref UW)) break;
                        }

                    }

                    
else
                    
{
                        
while (PACK_BYTE-- != 0)
                        
{
                            
while (Convert.ToChar(TBYTE[TBYTE_INDEX++& 0xDF!= 'H') ;
                            BYTE[UH, UW] 
= Convert.ToByte(255 - HEX_TO_DEC(TBYTE[TBYTE_INDEX - 3], TBYTE[TBYTE_INDEX - 2]));
                            
if (BREAK_FLAG = Inc_Height_Width(UPACK_MODE, ref UH, ref UW)) break;
                        }

                    }

                }

                
return true;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"图片解压", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                
return false;
            }

        }


        
private Boolean Inc_Height_Width(byte UPACK_MODE,ref uint UH, ref uint UW)
        
{
            
if (UPACK_MODE == 0 && ++UH >= biHeight)
            
{
                    UH 
= 0;
                    
if (++UW >= byWidth) return true;
            }

            
else if (UPACK_MODE == 128 && ++UW >= byWidth)
            
{
                    UW 
= 0;
                    
if (++UH >= biHeight) return true;
            }

            
return false;
        }


        
private byte HEX_TO_DEC(byte BHight, byte BLow)
        
{
            
if (Convert.ToChar(BHight & 0xDF>= 'A')
            
{
                BHight 
= Convert.ToByte(Convert.ToByte(Convert.ToChar(BHight & 0xDF- 'A'+ 10);
            }

            
else
            
{
                BHight 
= Convert.ToByte(BHight - '0');
            }

            BHight 
<<= 4;
            
if (Convert.ToChar(BLow & 0xDF>= 'A')
            
{
                BLow 
= Convert.ToByte(Convert.ToByte(Convert.ToChar(BLow & 0xDF- 'A'+ 10);
            }

            
else
            
{
                BLow 
= Convert.ToByte(BLow - '0');
            }

            
return Convert.ToByte(BHight + BLow);
        }


        
public Boolean Change_Bitmap_Data(string SText)
        
{
            
try
            
{
                
byte BPar = Get_One_Line_Last_Byte_Par();
                BPar 
^= 0xFF;
                
byte[] TBYTE = ASCIIEncoding.Default.GetBytes(SText);
                
uint TBYTE_INDEX = 0;
                
for (uint UH = 0; UH < biHeight; ++UH)
                
{
                    
for (uint UW = 0; UW < byWidth; ++UW)
                    
{
                        
while (Convert.ToChar(TBYTE[TBYTE_INDEX++& 0xDF!= 'H') ;
                        BYTE[UH, UW] 
= Convert.ToByte(255 - HEX_TO_DEC(TBYTE[TBYTE_INDEX - 3], TBYTE[TBYTE_INDEX - 2]));
                        
if (UW + 1 == byWidth && biBitCount < 8)
                        
{
                            BYTE[UH, UW] 
|= BPar;
                        }

                    }

                }

                
return true;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"改变数据", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                
return false;
            }

        }


        
public Boolean Change_Bitmap_Width(uint NewWidth)
        
{
            
try
            
{
                FileName 
= "NewBitmap";
                FilePath 
= "";
                
uint byWidth_bak = byWidth;
                biWidth 
= NewWidth;
                Set_ByWidth_Value();
                
if (NewWidth > MaxBiWidth)
                
{
                    
byte[,] BTEMP = (byte[,])BYTE.Clone(); 
                    BYTE 
= new byte[biHeight, byWidth + (4 - (byWidth % 4)) % 4];

                    
for (uint i = 0; i < biHeight; ++i)
                    
{
                        
for (uint j = 0; j < byWidth_bak; ++j)
                        
{
                            BYTE[i, j] 
= BTEMP[i, j];
                        }

                    }

  
                    
for (uint i = 0; i < biHeight; ++i)
                    
{
                        
for (uint j = byWidth_bak; j < byWidth + (4 - (byWidth % 4)) % 4++j)
                        
{
                            BYTE[i, j] 
= 0xFF;
                        }

                    }

                }

                MaxBiWidth 
= MaxBiWidth < biWidth ? biWidth : MaxBiWidth;
                biSizeImage 
= Convert.ToUInt32(biHeight * byWidth + (4 - (byWidth % 4)) % 4);
                bfSize 
= Convert.ToUInt32(bfOffBits + biSizeImage);
                
return true;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                
return false;
            }

        }


        
public Boolean Change_Bitmap_Height(uint NewHeight)
        
{
            
try
            
{
                FileName 
= "NewBitmap";
                FilePath 
= "";
                
if (NewHeight > MaxBiHeight)
                
{
                    
byte[,] BTEMP = (byte[,])BYTE.Clone();
                    BYTE 
= new byte[NewHeight, byWidth + (4 - (byWidth % 4)) % 4];

                    
for (uint i = 0; i < biHeight; ++i)
                    
{
                        
for (uint j = 0; j < byWidth; ++j)
                        
{
                            BYTE[i, j] 
= BTEMP[i, j];
                        }

                    }


                    
for (uint i = biHeight; i < NewHeight; ++i)
                    
{
                        
for (uint j = 0; j < byWidth; ++j)
                        
{
                            BYTE[i, j] 
= 0xFF;
                        }

                    }

                }

                biHeight 
= NewHeight;
                MaxBiHeight 
= MaxBiHeight < biHeight ? biHeight : MaxBiHeight;
                biSizeImage 
= Convert.ToUInt32(biHeight * (biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1+ (4 - ((biWidth / 8 + (biWidth % 8 == 0 ? 0 : 1)) % 4)) % 4));
                bfSize 
= Convert.ToUInt32(bfOffBits + biSizeImage);
                
return true;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                
return false;
            }

        }

    }

}

接下来创建一个新的窗体,用于创建新的BMP图(只要提供高和宽的设定)

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PP
{
    
public partial class CreateNew : Form
    
{
        
public CreateNew()
        
{
            InitializeComponent();
        }


        
private void textBox1_TextChanged(object sender, EventArgs e)
        
{

        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
try
            
{
                PPF.new_map_width 
= Convert.ToUInt32(tbWidth.Text);
                PPF.new_map_height 
= Convert.ToUInt32(tbHeight.Text);
                
if (PPF.new_map_width == 0 || PPF.new_map_height == 0)
                
{
                    MessageBox.Show(
"长度不能为零!""Create New", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    
return;
                }

                
if (rB2Color.Checked == true) PPF.new_map_color = 1;
                
if (rB4Color.Checked == true) PPF.new_map_color = 2;
                
if (rB16Color.Checked == true) PPF.new_map_color = 4;
                
if (rB256Color.Checked == true) PPF.new_map_color = 8;
                
if (rB16BitColor.Checked == true) PPF.new_map_color = 16;
                
if (rB24BitColor.Checked == true) PPF.new_map_color = 24;
                DialogResult 
= DialogResult.OK;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Create New", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }


        
private void tbWidth_Leave(object sender, EventArgs e)
        
{
            
try
            
{
                
if (tbWidth.Text == ""return;
                PPF.new_map_width 
= Convert.ToUInt32(tbWidth.Text);
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Create New", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tbWidth.ResetText();
            }

        }


        
private void tbHeight_Leave(object sender, EventArgs e)
        
{
            
try
            
{
                
if (tbHeight.Text == ""return;
                PPF.new_map_height 
= Convert.ToUInt32(tbHeight.Text);
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Create New", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tbHeight.ResetText();
            }

        }


        
private void CreateNew_Load(object sender, EventArgs e)
        
{

        }

    }

}

最后创建一个主窗体,用于打开图片(BMP)并显示在PICBOX中,显示每个像素对应的16进制数据,压缩单张图片、多张图片和文件夹里面图片等功能;

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Collections;
using System.Drawing.Drawing2D;

namespace PP
{
    
public partial class PPF : Form
    
{
        
public static uint new_map_width = 0;
        
public static uint new_map_height = 0;
        
public static uint new_map_color = 0;

        
private string RDataText = null;
        
private string RIndexText = null;
        
private uint ErrorCount = 0;

        
private CBitmap CB = null;

        
public PPF()
        
{//构造函数
            InitializeComponent();
        }


        
private void PPF_Load(object sender, EventArgs e)
        
{
            tbHeight.Enabled 
= false;
            tbWidth.Enabled 
= false;
        }


        
private void NewMenu_Click(object sender, EventArgs e)
        
{
            CreateNew cnDlg 
= new CreateNew();
            cnDlg.ShowDialog();
            
if (cnDlg.DialogResult == DialogResult.OK)
            
{
                cnDlg.Dispose();
                CB 
= new CBitmap(new_map_height, new_map_width,new_map_color);
                tbHeight.Text 
= CB.HeightProp.ToString();
                tbWidth.Text 
= CB.WidthProp.ToString();
                
this.Text = "PP - [" + CB.FileNameProp + "]";
                rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
                pictureBox1.Refresh();
                tbWidth.Enabled 
= true;
                tbHeight.Enabled 
= true;
            }

        }


        
private void pictureBox1_Paint(object sender, PaintEventArgs e)
        
{
            
if (CB == nullreturn;
            CB.Show_Bitmap(e, pictureBox1.Size.Width, pictureBox1.Size.Height);
        }


        
private void ZoomOutMenu_Click(object sender, EventArgs e)
        
{
            
if (CB != null)
            
{
                CB.PixelSizeProp 
+= 1;
                pictureBox1.Refresh();
            }

        }


        
private void ZoomInMenu_Click(object sender, EventArgs e)
        
{
            
if (CB != null && CB.PixelSizeProp != 1)
            
{
                CB.PixelSizeProp 
-= 1;
                pictureBox1.Refresh();
            }

        }


        
private void pictureBox1_SizeChanged(object sender, EventArgs e)
        
{
            pictureBox1.Refresh();
        }


        
private void OpenMenu_Click(object sender, EventArgs e)
        
{
            OpenFileDialog OFD 
= new OpenFileDialog();
            OFD.Multiselect 
= false;
            OFD.Title 
= "Open File";
            OFD.Filter 
= "Bitmap (*.bmp)|*.bmp";
            
if (OFD.ShowDialog() == DialogResult.OK)
            
{
                
byte[] BFileData = File.ReadAllBytes(OFD.FileName);
                
if (BFileData[0== 0x42 && BFileData[1== 0x4D)
                
{
                    CB 
= new CBitmap(OFD.FileName,BFileData);
                    tbHeight.Text 
= CB.HeightProp.ToString();
                    tbWidth.Text 
= CB.WidthProp.ToString();
                    
this.Text = "PP - [" + CB.FileNameProp + "]";
                    rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
                    pictureBox1.Refresh();
                    tbWidth.Enabled 
= true;
                    tbHeight.Enabled 
= true;
                }

                
else
                
{
                    MessageBox.Show(
"打开文件类型错误!""Open File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }

            OFD.Dispose();
        }


        
private void tSBDrawPen_Click(object sender, EventArgs e)
        
{
            
        }


        
private void BUPack_Click(object sender, EventArgs e)
        
{
            
if (rtbPackMapData.Text == "")
            
{
                MessageBox.Show(
"没有需要解压的数据!""数据解压", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            CB 
= new CBitmap(74,32,1);
            
if (CB.UPack_Bitmap(rtbPackMapData.Text) == false)
            
{
                MessageBox.Show(
"数据解压失败!""数据解压", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                CB 
= null;
                
return;
            }

            tbHeight.Text 
= CB.HeightProp.ToString();
            tbWidth.Text 
= CB.WidthProp.ToString();
            
this.Text = "PP - [" + CB.FileNameProp + "]";
            rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
            rtbPackMapData.ResetText();
            tabControl1.SelectedIndex 
= 0;
            pictureBox1.Refresh();
        }


        
private void PNMMenu_Click(object sender, EventArgs e)
        
{
            
if (CB == null)
            
{
                MessageBox.Show(
"不存在压缩图片!","压缩图片",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                
return;
            }

            rtbPackMapData.ResetText();
            
string SStr = "";
            
if (CB.Pack_Bitmap(ref SStr) == false)
            
{
                MessageBox.Show(
"压缩图片不成功!""压缩图片", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                
return;
            }

            rtbPackMapData.Text 
= SStr;
            tabControl1.SelectedIndex 
= 1;
        }


        
private void SelectAllMenu_Click(object sender, EventArgs e)
        
{
            
if (tabControl1.SelectedIndex == 0)
            
{
                rtbMapData.SelectAll();
            }

            
else if (tabControl1.SelectedIndex == 1)
            
{
                rtbPackMapData.SelectAll();
            }

        }


        
private void CutMenu_Click(object sender, EventArgs e)
        
{
            
if (tabControl1.SelectedIndex == 0 && rtbMapData.SelectedText != "")
            
{
                rtbMapData.Cut();
            }

            
else if (tabControl1.SelectedIndex == 1 && rtbPackMapData.SelectedText != "")
            
{
                rtbPackMapData.Cut();
            }

        }


        
private void CopyMenu_Click(object sender, EventArgs e)
        
{
            
if (tabControl1.SelectedIndex == 0 && rtbMapData.SelectedText != "")
            
{
                rtbMapData.Copy();
            }

            
else if (tabControl1.SelectedIndex == 1 && rtbPackMapData.SelectedText != "")
            
{
                rtbPackMapData.Copy();
            }

        }


        
private void Pastemenu_Click(object sender, EventArgs e)
        
{
            
if (tabControl1.SelectedIndex == 0)
            
{
                rtbMapData.Paste();
            }

            
else if (tabControl1.SelectedIndex == 1)
            
{
                rtbPackMapData.Paste();
            }

        }


        
private void UndoMenu_Click(object sender, EventArgs e)
        
{
            
if (tabControl1.SelectedIndex == 0 && rtbMapData.CanUndo == true)
            
{
                rtbMapData.Undo();
            }

            
else if (tabControl1.SelectedIndex == 1 && rtbPackMapData.CanUndo == true)
            
{
                rtbPackMapData.Undo();
            }

        }


        
private void RedoMenu_Click(object sender, EventArgs e)
        
{
            
if (tabControl1.SelectedIndex == 0 && rtbMapData.CanRedo == true)
            
{
                rtbMapData.Redo();
            }

            
else if (tabControl1.SelectedIndex == 1 && rtbPackMapData.CanRedo == true)
            
{
                rtbPackMapData.Redo();
            }

        }


        
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        
{
            
if (CB == nullreturn;
            
if (e.X < (pictureBox1.Size.Width - CB.PixelSizeProp * CB.WidthProp) / 2 || e.X > (pictureBox1.Size.Width + CB.PixelSizeProp * CB.WidthProp) / 2return;
            
if (e.Y < (pictureBox1.Size.Height - CB.PixelSizeProp * CB.HeightProp) / 2 || e.Y > (pictureBox1.Size.Height + CB.PixelSizeProp * CB.HeightProp) / 2return;
            tbCousorX.Text 
= ((e.X - (pictureBox1.Size.Width - CB.PixelSizeProp * CB.WidthProp) / 2/ CB.PixelSizeProp).ToString();
            tbcursorY.Text 
= ((e.Y - (pictureBox1.Size.Height - CB.PixelSizeProp * CB.HeightProp) / 2/ CB.PixelSizeProp).ToString();
        }


        
private void rtbMapData_TextChanged(object sender, EventArgs e)
        
{
            
if (rtbMapData.Enabled == falsereturn;
            
byte[] TBYTE = ASCIIEncoding.Default.GetBytes(rtbMapData.Text);
            
uint BLEN = Convert.ToUInt32(TBYTE.Length);
            
uint UHEIGHT = 0, TBYTE_INDEX = 0uint UWIDTH = 0, WIDTH_BAK = 0;
            
while (TBYTE_INDEX < BLEN)
            
{
                
if (TBYTE_INDEX + 1 < BLEN && TBYTE_INDEX + 2 < BLEN && TBYTE[TBYTE_INDEX] == '.' && (TBYTE[TBYTE_INDEX + 1== 'D' || TBYTE[TBYTE_INDEX + 1== 'd'&& (TBYTE[TBYTE_INDEX + 2== 'B' || TBYTE[TBYTE_INDEX + 2== 'b'))
                
{
                    
++UHEIGHT;
                    
if (WIDTH_BAK == 0)
                    
{
                        WIDTH_BAK 
= UWIDTH;
                    }

                    
else if (WIDTH_BAK != UWIDTH)
                    
{
                        MessageBox.Show(
"数据格式错误!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                        
if (CB != null)
                        
{
                            rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
                        }

                        
else
                        
{
                            rtbMapData.ResetText();
                        }

                        
return;
                    }

                    UWIDTH 
= 0;
                }

                
if (TBYTE_INDEX + 1 < BLEN && (TBYTE[TBYTE_INDEX] == 'H' || TBYTE[TBYTE_INDEX] == 'h'&& (TBYTE[TBYTE_INDEX + 1== ',' || TBYTE[TBYTE_INDEX + 1== ' ')) ++UWIDTH;

                
if ((TBYTE[TBYTE_INDEX] == ',' || TBYTE[TBYTE_INDEX] == '.' || (TBYTE[TBYTE_INDEX] >= '0' && TBYTE[TBYTE_INDEX] <= '9'|| (TBYTE[TBYTE_INDEX] >= 'A' && TBYTE[TBYTE_INDEX] <= 'F'|| (TBYTE[TBYTE_INDEX] >= 'a' && TBYTE[TBYTE_INDEX] <= 'f'|| TBYTE[TBYTE_INDEX] == 'H' || TBYTE[TBYTE_INDEX] == 'h' || TBYTE[TBYTE_INDEX] == ' ' || TBYTE[TBYTE_INDEX] == ' '== false)
                
{
                    MessageBox.Show(
"数据格式错误!""ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    
if (CB != null)
                    
{
                        rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
                    }

                    
else
                    
{
                        rtbMapData.ResetText();
                    }

                    
return;
                }

                
++TBYTE_INDEX;
            }

            
if (CB == null || UHEIGHT != CB.HeightProp || WIDTH_BAK != CB.ByWidthProp)
            
{
                CB 
= new CBitmap(8*WIDTH_BAK, UHEIGHT, 1);    //暂时只考虑bitCount =1
                this.Text = "PP - [" + CB.FileNameProp + "]";
                tbHeight.Text 
= CB.HeightProp.ToString();
                tbWidth.Text 
= CB.WidthProp.ToString();
            }

            
if (CB.Change_Bitmap_Data(rtbMapData.Text) == false)
            
{
                CB 
= null;
                
this.Text = "PP";
                rtbMapData.ResetText();
                
return;
            }

            rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
            pictureBox1.Refresh();
            tbWidth.Enabled 
= true;
            tbHeight.Enabled 
= true;
        }


        
private void tbHeight_TextChanged(object sender, EventArgs e)
        
{
            
try
            
{
                
uint HI = Convert.ToUInt32(tbHeight.Text);
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message,
"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                tbHeight.Text 
= CB.HeightProp.ToString();
            }

        }


        
private void tbWidth_TextChanged(object sender, EventArgs e)
        
{
            
try
            
{
                
uint WI = Convert.ToUInt32(tbWidth.Text);
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tbWidth.Text 
= CB.WidthProp.ToString();
            }

        }


        
private void tbWidth_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
if (e.KeyChar == ' ')
            
{
                
uint WI = Convert.ToUInt32(tbWidth.Text);
                
if (WI == CB.WidthProp) return;
                
if (CB.Change_Bitmap_Width(WI) == false)
                
{
                    CB 
= null;
                    
return;
                }

                
this.Text = "PP - [" + CB.FileNameProp + "]";
                rtbMapData.Enabled 
= false;
                rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
                pictureBox1.Refresh();
                rtbMapData.Enabled 
= true;
            }

            
        }


        
private void tbHeight_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
if (e.KeyChar == ' ')
            
{
                
uint HI = Convert.ToUInt32(tbHeight.Text);
                
if (HI == CB.HeightProp) return;
                
if (CB.Change_Bitmap_Height(HI) == false)
                
{
                    CB 
= null;
                    
return;
                }

                
this.Text = "PP - [" + CB.FileNameProp + "]";
                rtbMapData.Enabled 
= false;
                rtbMapData.Text 
= CB.Show_Bitmap_HEX_Data();
                pictureBox1.Refresh();
                rtbMapData.Enabled 
= true;
            }

        }


        
private void PMMenu_Click(object sender, EventArgs e)
        
{
            
try
            
{
                OpenFileDialog OFD 
= new OpenFileDialog();
                OFD.Multiselect 
= true;
                OFD.Title 
= "Open File";
                OFD.Filter 
= "Bitmap (*.bmp)|*.bmp";
                
if (OFD.ShowDialog() == DialogResult.OK)
                
{
                    RDataText 
= "";
                    RIndexText 
= GetNonceFolerName(Path.GetDirectoryName(OFD.FileNames[0])) + "_index: ";
                    ErrorCount 
= 0;
                    
foreach (string SFileFullName in OFD.FileNames)
                    
{
                        
byte[] TBYTE = File.ReadAllBytes(SFileFullName);
                        
if (TBYTE[0== 0x42 && TBYTE[1== 0x4D)
                        
{
                            CBitmap TCB 
= new CBitmap(SFileFullName, TBYTE);
                            
string SText = "";
                            
if (TCB.Pack_Bitmap(ref SText) == true)
                            
{
                                RDataText 
+= GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + ": " + SText + " ";
                                RIndexText 
+= "    .db    #" + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + "_tpp,#" + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + "_tph,#" + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + "_tpl ";
                            }

                            
else
                            
{
                                
++ErrorCount;
                                RDataText 
+= "Error: " + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + " ";
                                RIndexText 
+= "Error: " + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + " ";
                            }

                        }

                    }

                    StreamWriter sw 
= new StreamWriter("PACK_DATA.ASM");
                    sw.Write(RDataText);
                    sw.Close();
                    sw 
= new StreamWriter("PACK_INDEX.ASM");
                    sw.Write(RIndexText);
                    sw.Close();
                    MessageBox.Show(
"压缩" + OFD.FileNames.Length.ToString() + "张图片," + ErrorCount.ToString() + "张出错!""压缩成功!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Error:Pack map", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }


        
private string GetNonceFolerName(string SFileFullName)
        
{
            
int SLEN = SFileFullName.Length;
            
int TSLEN = SLEN - 1;
            
while (SFileFullName[TSLEN--!= '/') ;
            
char[] FolerName = new char[SLEN - TSLEN - 2];
            SFileFullName.CopyTo(TSLEN 
+ 2, FolerName, 0, SLEN - TSLEN - 2);
            
string SFolerName = "";
            
foreach (char CTemp in FolerName)
            
{
                SFolerName 
+= CTemp;
            }

            
return SFolerName;
        }


        
private void PFMenu_Click(object sender, EventArgs e)
        
{
            FolderBrowserDialog FBD 
= new FolderBrowserDialog();
            
if (FBD.ShowDialog() == DialogResult.OK)
            
{
                RDataText 
= "";
                RIndexText 
= "";
                ErrorCount 
= 0;
                Find_Bitmap_In_Folder(
new DirectoryInfo(FBD.SelectedPath), FBD.SelectedPath);
                StreamWriter sw 
= new StreamWriter(FBD.SelectedPath + "/PACK_DATA.ASM");
                sw.Write(RDataText);
                sw.Close();
                sw 
= new StreamWriter(FBD.SelectedPath + "/PACK_INDEX.ASM");
                sw.Write(RIndexText);
                sw.Close();
                MessageBox.Show(
"压缩图片完成有" + ErrorCount.ToString() + "张出错!""压缩完成!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }


        
private void Find_Bitmap_In_Folder(FileSystemInfo fileinfo, string filePath)
        
{
            
try
            
{
                
if (!fileinfo.Exists) return;
                DirectoryInfo dirinfo 
= fileinfo as DirectoryInfo;
                
if (dirinfo == nullreturn//不是目录
                FileSystemInfo[] files = dirinfo.GetFileSystemInfos();
                
for (int i = 0; i < files.Length; i++)    //遍历目录下所有文件、子目录
                {
                    FileInfo file 
= files[i] as FileInfo;
                    
if (file != null// 是文件
                    {
                        
if (file.Extension.ToLower() == ".bmp")   //判断文件的扩展名
                        {
                            
byte[] TBYTE = File.ReadAllBytes(filePath + "/" + file);
                            
if (TBYTE[0== 0x42 && TBYTE[1== 0x4D)
                            
{
                                CBitmap TCB 
= new CBitmap(filePath + "/" + file, TBYTE);
                                
string SText = "";
                                
if (TCB.Pack_Bitmap(ref SText) == true)
                                
{
                                    RDataText 
+= GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + ": " + SText + " ";
                                    RIndexText 
+= "    .db    #" + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + "_tpp,#" + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + "_tph,#" + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + "_tpl ";
                                }

                                
else
                                
{
                                    
++ErrorCount;
                                    RDataText 
+= "Error: " + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + " ";
                                    RIndexText 
+= "Error: " + GetNonceFolerName(TCB.FilePathProp) + "_" + TCB.FileNameProp + " ";
                                }

                            }

                        }

                    }

                    
else        //是目录
                    {
                        RIndexText 
+= files[i].Name + "_index:: ";
                        Find_Bitmap_In_Folder(files[i], files[i].FullName); 
//对子目录进行递归调用
                    }

                }

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Find_Bitmap_In_Folder", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

    }

}

 

 

原创粉丝点击