如何使用trackBar滚动事件快速平滑地在图像之间移动?

时间:2016-03-16 18:28:33

标签: c# .net winforms

这是滚动事件

int countver = 0;
        int counthor = 0;
        Image newImage;
        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            if (files != null && files.Length > 0)
            {
                newImage = Image.FromFile(files[files.Length - 1]);
                float imghorizontal = newImage.HorizontalResolution;
                float imgvertical = newImage.VerticalResolution;
                if (trackBar2.Value < trackBar2.Maximum)
                {
                    countver += 100;
                    counthor += 100;
                }
                if (trackBar2.Value == trackBar2.Maximum)
                {
                    countver += 100;
                    counthor += 100;
                }
                pictureBox1.Image = ResizeImage(files[files.Length - 1], (int)imghorizontal + counthor, (int)imgvertical + countver);
                label15.Text = (int)imghorizontal + counthor.ToString() + "," + (int)imgvertical + countver.ToString();
                label15.Visible = true;
            }
        }

这是调整图像大小的方法

private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
        {
            using (Image originalImage = Image.FromFile(filename))
            {
                //Caluate new Size
                int newWidth = originalImage.Width;
                int newHeight = originalImage.Height;
                double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
                if (aspectRatio <= 1 && originalImage.Width > maxWidth)
                {
                    newWidth = maxWidth;
                    newHeight = (int)Math.Round(newWidth / aspectRatio);
                }
                else if (aspectRatio > 1 && originalImage.Height > maxHeight)
                {
                    newHeight = maxHeight;
                    newWidth = (int)Math.Round(newHeight * aspectRatio);
                }
                if (newWidth >= 0 && newHeight >= 0)
                {
                    Bitmap newImage = new Bitmap(newWidth, newHeight);
                    using (Graphics g = Graphics.FromImage(newImage))
                    {
                        //--Quality Settings Adjust to fit your application
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
                        return newImage;
                    }
                }
                return null;
            }
        }

问题是当我向上或向下移动卷轴时,需要1-3秒才能完成图像处理。如果我将滚动更快地移动到另一个值,它会采取更多(另一个值,我的意思是我试图将滑块从开始移动到结束程序挂起)。

这是form1代码,我使用后台工作程序来更改硬盘上的图像大小。整个想法是让用户预览图像的样子,然后如果他确定它足够好,他点击一个启动背景工作者的按钮。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace Images_Batch_Resize
{
    public partial class Form1 : Form
    {
        OpenFileDialog openFileDialog1;
        string[] files;
        string directoryPath;

        public Form1()
        {
            InitializeComponent();

            label6.Visible = false;
            label7.Visible = false;
            label8.Visible = false;
            label9.Visible = false;
            label10.Visible = false;
            label12.Visible = false;
            label15.Visible = false;

            openFileDialog1 = new OpenFileDialog();
            trackBar2.Minimum = 0;
            trackBar2.Maximum = 20;
        }

        private void changeWorkingDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
       + "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff";
            //openFileDialog1.InitialDirectory = @"c:\";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;

            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                files = openFileDialog1.FileNames;

                try
                {
                    if (files.Length > 0)
                    {
                        label6.Text = files.Length.ToString();
                        label6.Visible = true;
                        directoryPath = Path.GetDirectoryName(files[0]);
                        label12.Text = directoryPath;
                        label12.Visible = true;
                        pictureBox2.Load(files[0]);
                        pictureBox1.Load(files[0]);
                        trackBar1.Minimum = 0;
                        trackBar1.Maximum = files.Length - 1;
                    }
                }
                catch (IOException)
                {
                }
            }
        }

        private void beginOperationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (files.Length > 0)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
        {
            using (Image originalImage = Image.FromFile(filename))
            {
                //Caluate new Size
                int newWidth = originalImage.Width;
                int newHeight = originalImage.Height;
                double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
                if (aspectRatio <= 1 && originalImage.Width > maxWidth)
                {
                    newWidth = maxWidth;
                    newHeight = (int)Math.Round(newWidth / aspectRatio);
                }
                else if (aspectRatio > 1 && originalImage.Height > maxHeight)
                {
                    newHeight = maxHeight;
                    newWidth = (int)Math.Round(newHeight * aspectRatio);
                }
                if (newWidth >= 0 && newHeight >= 0)
                {
                    Bitmap newImage = new Bitmap(newWidth, newHeight);
                    using (Graphics g = Graphics.FromImage(newImage))
                    {
                        //--Quality Settings Adjust to fit your application
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
                        return newImage;
                    }
                }
                return null;
            }
        }

        int countver = 0;
        int counthor = 0;
        Image newImage;
        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            if (files != null && files.Length > 0)
            {
                newImage = Image.FromFile(files[files.Length - 1]);
                float imghorizontal = newImage.HorizontalResolution;
                float imgvertical = newImage.VerticalResolution;
                if (trackBar2.Value < trackBar2.Maximum)
                {
                    countver += 100;
                    counthor += 100;
                }
                if (trackBar2.Value == trackBar2.Maximum)
                {
                    countver += 100;
                    counthor += 100;
                }
                pictureBox1.Image = ResizeImage(files[files.Length - 1], (int)imghorizontal + counthor, (int)imgvertical + countver);
                label15.Text = (int)imghorizontal + counthor.ToString() + "," + (int)imgvertical + countver.ToString();
                label15.Visible = true;
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            pictureBox1.Load(files[trackBar1.Value]);
            pictureBox2.Load(files[trackBar1.Value]);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                BackgroundWorker worker = sender as BackgroundWorker;
                int counter = 0;
                int percentage = 0;
                foreach (string file in files)
                {                   
                    Bitmap bmp1 = new Bitmap(ResizeImage(file, 300, 300));
                    string resizedfilename = Path.Combine(directoryPath, Path.GetFileNameWithoutExtension(file) + "_resized.jpg");
                    bmp1.Save(resizedfilename);
                    bmp1.Dispose();
                    counter++;
                    percentage = counter * 100 / files.Length;
                    worker.ReportProgress(percentage);
                }
            }
            catch(Exception err)
            {
                string ttt = err.ToString();
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        int oldValue = 0;
        private void trackBar2_ValueChanged(object sender, EventArgs e)
        {
            if ((sender as TrackBar).Value >= oldValue)
            {

            }
            else
            {

            }
            oldValue = (sender as TrackBar).Value;
        }
    }
}

0 个答案:

没有答案