Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
AnswerRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
NotPolitcallyCorrect24-Nov-16 7:31
NotPolitcallyCorrect24-Nov-16 7:31 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Rıza Berkay Ayçelebi24-Nov-16 7:52
Rıza Berkay Ayçelebi24-Nov-16 7:52 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
NotPolitcallyCorrect24-Nov-16 7:57
NotPolitcallyCorrect24-Nov-16 7:57 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Rıza Berkay Ayçelebi24-Nov-16 8:01
Rıza Berkay Ayçelebi24-Nov-16 8:01 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
NotPolitcallyCorrect24-Nov-16 8:06
NotPolitcallyCorrect24-Nov-16 8:06 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Rıza Berkay Ayçelebi24-Nov-16 8:12
Rıza Berkay Ayçelebi24-Nov-16 8:12 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Dave Kreskowiak24-Nov-16 8:15
mveDave Kreskowiak24-Nov-16 8:15 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Rıza Berkay Ayçelebi24-Nov-16 8:25
Rıza Berkay Ayçelebi24-Nov-16 8:25 
Here is Choose file for txt files
C#
 private void CompressDosyaAc_button_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog1.Filter = "Huffman Files (*.huf)|*.huf";
                openFileDialog1.Filter = "Text Files (*.txt)|*.txt";

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    int[] freq = new int[128];
                    StreamReader sr = new StreamReader(openFileDialog1.FileName);

                    iCount = 0;

                    while (!sr.EndOfStream)  // dosya sonuna kadar karakterleri oku
                    {
                        int ch = sr.Read();

                        if (ch >= 0 && ch <= 127)
                            freq[ch]++;
                        else
                        {
                            MessageBox.Show("Dosya ASCII içerikli değil!", "HATA", //ascii kodulu veriyle karşılaşılmaması durumunda hata penceresi çıkar
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }

                        iCount++;
                    }

                    sr.Close();
                    charFreq = new List<CharFreq>(128);

                    for (int i = 0; i < 128; i++)
                    {
                        if (freq[i] != 0)
                        {
                            CharFreq cf = new CharFreq();

                            cf.ch = (char)i;
                            cf.freq = freq[i];
                            charFreq.Add(cf);
                        }
                    }

                    Compress();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "HATA",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

       


        
    }
}
Here is compress function ?
<pre lang="c#">       private void 

   Compress()   //sıkıştırma fonksiyonu
        {
            int bitLength = 0, block = 0, bpBlock = 0;
            HuffmanTree ht = new HuffmanTree();
            BinaryTreeNode<CharFreq> root = ht.Build(charFreq, charFreq.Count);

            leafNodes = 0;
            textBox1.Text += ("The Huffman ağacı geçişi\r\n\r\n");
            InorderTraversal(root); //huffman ağacı oluşturuluyor

            if (leafNodes != charFreq.Count)
            {
                MessageBox.Show("Sıkıştırma ağacı sıkıştırma işleminde hata", "HATA!",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            textBox1.Text += ("Toplam girilen karakter sayısı = " + iCount.ToString() + "\r\n");

            textBox1.Text += ("Toplam yaprak kısmındaki karakter sayısı= " + leafNodes.ToString() + "\r\n");
                

            if (leafNodes < 2) //yaprak düğümü 2 den az ise
            {
                bitLength = 1; // veriler 1 bitle temsil edilecek
                block = 8;
                bpBlock = 1;
            }
            else if (leafNodes < 4)//yaprak düğümü 4 den az ise
            {
                bitLength = 2; // veriler 2 bitle temsil edilecek
                block = 4;
                bpBlock = 1;
            }
            else if (leafNodes < 8)//yaprak düğümü 8 den az ise
            {
                bitLength = 3;// veriler 3 bitle temsil edilecek
                block = 8;
                bpBlock = 3;
            }
            else if (leafNodes < 16)//yaprak düğümü 16 dan az ise
            {
                bitLength = 4;// veriler 4 bitle temsil edilecek
                block = 2;
                bpBlock = 1;
            }
            else if (leafNodes < 32)//yaprak düğümü 32 den az ise
            {
                bitLength = 5;// veriler 5 bitle temsil edilecek
                block = 8;
                bpBlock = 5;
            }
            else if (leafNodes < 64)//yaprak düğümü 64 den az ise
            {
                bitLength = 6;// veriler 6 bitle temsil edilecek
                block = 4;
                bpBlock = 3;
            }
            else if (leafNodes < 128)//yaprak düğümü 128 den az ise
            {
                bitLength = 7;// veriler 7 bitle temsil edilecek
                block = 8;
                bpBlock = 8;
            }

            int number = (leafNodes * bitLength) / block;
            int numberBits = bitLength * iCount;
            int numberBlocks = numberBits / (8 * bpBlock);
            int remainder = numberBits % (8 * bpBlock);

            header = new Header();
            header.inputFileLength = iCount;
            header.outputDataLength = numberBlocks * bpBlock + remainder / bitLength;
            header.tableLength = leafNodes;
            header.bitLength = bitLength;
            header.chars = new byte[leafNodes];

            textBox1.Text += ("Toplam veri miktarı = " + header.outputDataLength.ToString() + "\r\n");

            textBox1.Text += ("Toplam bit uzunluğu = " + bitLength.ToString() + "\r\n");
                

            int headerLength = 4 * sizeof(int) + leafNodes * sizeof(byte);
            int outputFileLength = headerLength + header.outputDataLength;          //sıkıştırılmış dosyanın boyutu
            double percent = 100.0 - 100.0 * ((double)outputFileLength) / iCount;    //sıkıştırılmış dosyanın ne oranda sıkıştırıldığı analiz ediliyor

            textBox1.Text += ("Sıkıştırılmış dosyanın boyutu = " + outputFileLength.ToString() + "\r\n");

            textBox1.Text += ("Sıkıştırma Oranı: = %" + percent.ToString("F2"));
            aquaGauge1.Value = Convert.ToInt16(percent);
            aquaGauge1.ThresholdPercent = Convert.ToInt16(percent);
                

            if (percent <= 0.0) // sıkıştırma oranı 0 veya altında olursa, yani sıkıştırma işleminden sonra dosya boyutu küçüleceği yerde boyutu büyürse...
            {
                MessageBox.Show("Sıkıştırma oranı 0 veya 0'ın altında oldu,yani sıkıştırma başarız", "HATA!",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            for (int i = 0; i < charFreq.Count; i++)
                header.chars[i] = (byte)charFreq[i].ch;

            try
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK) // oluşturulan dosyanın içine veriler aktarılıp kaydediliyor
                {
                    FileStream fs = new FileStream(saveFileDialog1.FileName,
                        FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);
                    StreamReader sr = new StreamReader(openFileDialog1.FileName);
                    byte[] ibuff = new byte[block];
                    byte[] codes = new byte[block];

                    bw.Write(header.inputFileLength);
                    bw.Write(header.tableLength);
                    bw.Write(header.bitLength);
                    bw.Write(header.outputDataLength);
                    bw.Write(header.chars, 0, header.tableLength);

                    for (int k = 0; k < header.inputFileLength / block; k++)
                    {
                        for (int i = 0; i < block; i++)
                        {
                            ibuff[i] = (byte)sr.Read();
                            codes[i] = (byte)255;

                            for (int j = 0; j < header.tableLength; j++)
                            {
                                if (ibuff[i] == header.chars[j])
                                {
                                    codes[i] = (byte)j;
                                    break;
                                }
                            }

                            if (codes[i] == (byte)255)
                            {
                                MessageBox.Show("İşlem başarısız,farklı bir yöntem deneyin", "HATA",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }

                        byte[] obuff = new byte[bpBlock];

                        for (int i = 0; i < block; i++)
                        {
                            int c = (int)codes[i];

                            for (int j = 0; j < bitLength; j++)
                            {
                                int bit = (c >> (bitLength - j - 1)) & 1;

                                SetBit(i * bitLength + j, bit, obuff);
                            }
                        }

                        bw.Write(obuff, 0, bpBlock);
                    }

                    if (header.inputFileLength % block != 0)
                    {
                        for (int i = 0; i < header.inputFileLength % block; i++)
                        {
                            byte ch = (byte)sr.Read();

                            bw.Write(ch);
                        }
                    }

                    bw.Flush();
                    sr.Close();
                    bw.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "HATA",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Dave Kreskowiak24-Nov-16 8:30
mveDave Kreskowiak24-Nov-16 8:30 
AnswerRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Richard MacCutchan24-Nov-16 7:41
mveRichard MacCutchan24-Nov-16 7:41 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Rıza Berkay Ayçelebi24-Nov-16 7:50
Rıza Berkay Ayçelebi24-Nov-16 7:50 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Richard MacCutchan24-Nov-16 8:00
mveRichard MacCutchan24-Nov-16 8:00 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Rıza Berkay Ayçelebi24-Nov-16 8:02
Rıza Berkay Ayçelebi24-Nov-16 8:02 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Dave Kreskowiak24-Nov-16 8:12
mveDave Kreskowiak24-Nov-16 8:12 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
Dave Kreskowiak24-Nov-16 8:06
mveDave Kreskowiak24-Nov-16 8:06 
GeneralRe: Are there someone for helping me.(Huffman İmage Compression with c#) Pin
OriginalGriff24-Nov-16 8:22
mveOriginalGriff24-Nov-16 8:22 
QuestionDecimal MEID to hexadecimal Pin
mjawadkhatri23-Nov-16 12:22
mjawadkhatri23-Nov-16 12:22 
AnswerRe: Decimal MEID to hexadecimal Pin
OriginalGriff23-Nov-16 20:52
mveOriginalGriff23-Nov-16 20:52 
GeneralRe: Decimal MEID to hexadecimal Pin
mjawadkhatri26-Nov-16 18:07
mjawadkhatri26-Nov-16 18:07 
GeneralRe: Decimal MEID to hexadecimal Pin
OriginalGriff26-Nov-16 21:26
mveOriginalGriff26-Nov-16 21:26 
AnswerRe: Decimal MEID to hexadecimal Pin
mjawadkhatri26-Nov-16 18:06
mjawadkhatri26-Nov-16 18:06 
QuestionTaking C# to a whole new level of Evil Pin
Foothill23-Nov-16 9:16
professionalFoothill23-Nov-16 9:16 
AnswerRe: Taking C# to a whole new level of Evil Pin
Richard Deeming23-Nov-16 12:04
mveRichard Deeming23-Nov-16 12:04 
QuestionHow to create alert in c# Pin
Member 308047023-Nov-16 1:20
Member 308047023-Nov-16 1:20 
SuggestionRe: How to create alert in c# Pin
Richard MacCutchan23-Nov-16 1:24
mveRichard MacCutchan23-Nov-16 1:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.