Esempio n. 1
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            String folderSrc  = this.textBox_src.Text;
            String folderDest = this.textBox_dest.Text;

            if (folderSrc == null || !Directory.Exists(folderSrc))
            {
                MessageBox.Show("源图路径设置错误!");
                return;
            }
            if (folderDest == null || !Directory.Exists(folderDest))
            {
                MessageBox.Show("目标路径设置错误!");
                return;
            }
            currentTransform = this.comboBox1.SelectedIndex;
            String formatSrc  = "";
            String formatDest = "";

            if (currentTransform < 0)
            {
                MessageBox.Show("选择你需要转换的格式。");
                return;
            }
            else
            {
                switch (currentTransform)
                {
                case TRANSFORM_PNG_GIF:    //png->gif
                    formatSrc  = "png";
                    formatDest = "gif";
                    break;

                case TRANSFORM_GIF_PNG:    //gif->png
                    formatSrc  = "gif";
                    formatDest = "png";
                    break;

                case TRANSFORM_PNG_24_PNG_8A:    //png24->png-8+alpha
                    formatSrc  = "png";
                    formatDest = "png";
                    break;
                }
            }

            //开始转换
            this.richTextBox.AppendText("=============开始转换=============\n");
            String[] files = Directory.GetFiles(folderSrc + "\\", "*." + formatSrc);
            this.richTextBox.AppendText("共发现" + files.Length + "个被转换文件" + "\n");
            ImageFormat imgFormat = null;
            String      fileAlpha = null;
            String      fileDest  = null;

            for (int i = 0; i < files.Length; i++)
            {
                Image img      = IOUtil.createImage(files[i]);
                Image imgAlpha = null;
                fileDest  = files[i].Replace(folderSrc, folderDest);
                fileDest  = fileDest.Substring(0, fileDest.LastIndexOf('.'));
                fileAlpha = fileDest + "_alpha." + formatDest;
                fileDest  = fileDest + "." + formatDest;
                String fileName = fileDest.Substring(fileDest.LastIndexOf("\\") + 1, fileDest.Length - fileDest.LastIndexOf("\\") - 1);
                switch (currentTransform)
                {
                case TRANSFORM_PNG_GIF:    //png->gif
                    imgFormat = ImageFormat.Gif;
                    img       = IndexImageProcessor.getIndexColorImage(img, files[i]);
                    break;

                case TRANSFORM_GIF_PNG:    //gif->png
                    imgFormat = ImageFormat.Png;
                    img       = IndexImageProcessor.getIndexColorImage(img, files[i]);
                    break;

                case TRANSFORM_PNG_24_PNG_8A:    //png24->png-8+alpha
                    imgFormat = ImageFormat.Png;
                    Image[] imgs = PNG24Processor.getPng_8(img);
                    img      = imgs[0];
                    imgAlpha = imgs[1];
                    break;
                }
                if (img != null)
                {
                    img.Save(fileDest, imgFormat);//ImageCodecInfo encoder, EncoderParameters encoderParams
                    if (imgAlpha != null)
                    {
                        imgAlpha.Save(fileAlpha, imgFormat);
                    }
                    richTextBox.AppendText("转换成功->" + fileName + "\n");
                }
                else
                {
                    richTextBox.AppendText("【转换失败】->" + fileName + "\n");
                }
                this.Refresh();
            }
            this.richTextBox.AppendText("=============转换完毕=============\n");
        }
Esempio n. 2
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            String folderSrc  = this.textBox_src.Text;
            String folderDest = this.textBox_dest.Text;

            if (folderSrc == null || !Directory.Exists(folderSrc))
            {
                MessageBox.Show("源图路径设置错误!");
                return;
            }
            if (folderDest == null || !Directory.Exists(folderDest))
            {
                MessageBox.Show("目标路径设置错误!");
                return;
            }
            int transPercentBegin = Convert.ToInt32(numericUpDown_min.Value);
            int transCount        = Convert.ToInt32(numericUpDown_count.Value);

            //开始转换
            this.richTextBox.AppendText("=============开始转换=============\n");
            String[] files = Directory.GetFiles(folderSrc + "\\", "*.png");
            this.richTextBox.AppendText("共发现" + files.Length + "个被转换文件" + "\n");
            String fileDest = null;

            for (int i = 0; i < files.Length; i++)
            {
                Image  img      = IOUtil.createImage(files[i]);
                String fileName = files[i].Substring(files[i].LastIndexOf("\\") + 1, files[i].Length - files[i].LastIndexOf("\\") - 1);
                fileName = fileName.Substring(0, fileName.LastIndexOf("."));
                ArrayList printPercent = new ArrayList();
                //遍历每个缩放级别
                for (int j = 0; j < transCount; j++)
                {
                    fileDest = folderDest + "\\" + fileName + "_z" + (j) + ".png";
                    int percent = transPercentBegin + (transCount - j) * (100 - transPercentBegin) / transCount;
                    percent = transPercentBegin * transCount * 100 / ((j + 1) * (100 - transPercentBegin) + transPercentBegin * transCount);
                    int wN = percent * img.Width / 100;
                    int hN = percent * img.Height / 100;
                    printPercent.Add(percent);
                    Image zoomImg = new Bitmap(wN, hN);
                    //缩放图片
                    Graphics g = Graphics.FromImage(zoomImg);
                    GraphicsUtil.drawClip(g, img, 0, 0, 0, 0, img.Width, img.Height, 0, percent / 100.0f);
                    g.Dispose();
                    //转换成PNG
                    Image pngImg = IndexImageProcessor.getIndexColorImage(zoomImg, files[i]);
                    if (pngImg != null)
                    {
                        pngImg.Save(fileDest, ImageFormat.Png);//ImageCodecInfo encoder, EncoderParameters encoderParams
                        richTextBox.AppendText("转换成功->" + fileDest + "\n");
                    }
                    else
                    {
                        richTextBox.AppendText("【转换失败】->" + fileDest + "\n");
                    }
                    pngImg.Dispose();
                    zoomImg.Dispose();
                    this.Refresh();
                }
                richTextBox.AppendText("public static int percents[]=new int[]{");
                richTextBox.AppendText("100,");
                for (int j = 0; j < printPercent.Count; j++)
                {
                    richTextBox.AppendText("" + printPercent[j]);
                    if (j != printPercent.Count - 1)
                    {
                        richTextBox.AppendText(",");
                    }
                }
                richTextBox.AppendText("};\n");
            }
            this.richTextBox.AppendText("=============转换完毕=============\n");
        }