C# 如何使用轨迹栏滚动事件在图像之间快速平滑地移动?
这是滚动事件C# 如何使用轨迹栏滚动事件在图像之间快速平滑地移动?,c#,.net,winforms,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) { newImag
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;
}
}
int countver=0;
int counthor=0;
新形象;
私有void trackBar2_滚动(对象发送方,事件参数e)
{
if(files!=null&&files.Length>0)
{
newImage=Image.FromFile(files[files.Length-1]);
float imghorizontal=新建图像。水平分辨率;
float imgvertical=newImage.vertical分辨率;
if(trackBar2.Value
这是调整图像大小的方法
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-maxWidth、int-maxHeight)
{
使用(Image originalImage=Image.FromFile(文件名))
{
//计算新尺寸
int newWidth=原始图像宽度;
int newHeight=原始图像高度;
双纵横比=(双)原始图像.宽度/(双)原始图像.高度;
if(aspectRatio maxWidth)
{
newWidth=maxWidth;
newHeight=(int)Math.Round(newWidth/aspectRatio);
}
else if(aspectRatio>1&&originalImage.Height>maxHeight)
{
newHeight=maxHeight;
newWidth=(int)Math.Round(newHeight*aspectRatio);
}
如果(newWidth>=0&&newHeight>=0)
{
位图newImage=新位图(newWidth、newHeight);
使用(Graphics g=Graphics.FromImage(newImage))
{
//--质量设置会根据您的应用程序进行调整
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQuality双线性;
g、 SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g、 PixelOffsetMode=System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g、 合成质量=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g、 DrawImage(原始图像,0,0,newImage.Width,newImage.Height);
返回新图像;
}
}
返回null;
}
}
问题是,当我向上或向下移动滚动条时,图像处理需要1-3秒。如果我将滚动条上移到另一个值,它将花费更多的时间(另一个值,我的意思是我试图将滑块从程序挂起的开始移动到结束)
这是form1代码,我正在使用backgroundworker更改硬盘上的图像大小。整个想法是让用户预览图像的外观,然后如果用户认为它足够好,他单击一个按钮启动backgroundworker
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
Net系统;
名称空间图像\u批处理\u调整大小
{
公共部分类Form1:Form
{
OpenFileDialog openFileDialog1;
字符串[]文件;
字符串目录路径;
公共表格1()
{
初始化组件();
标签6.可见=假;
标签7.可见=假;
标签8.可见=假;
标签9.可见=假;
标签10.可见=假;
标签12.可见=假;
标签15.可见=假;
openFileDialog1=新建OpenFileDialog();
trackBar2.最小值=0;
trackBar2.最大值=20;
}
私有void changeWorkingDirectoryToolStripMenuItem\u单击(对象发送方,事件参数e)
{
openFileDialog1.Filter=“BMP |*.BMP | GIF |*.GIF | JPG |*.JPG;*.jpeg | PNG |*.PNG | TIFF |*.TIFF |”
+“所有图形类型|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff”;
//openFileDialog1.InitialDirectory=@“c:\”;
openFileDialog1.RestoreDirectory=true;
openFileDialog1.Multiselect=true;
DialogResult=openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
{
files=openFileDialog1.FileNames;
尝试
{
如果(files.Length>0)
{
label6.Text=files.Length.ToString();
label6.可见=真实;
directoryPath=Path.GetDirectoryName(文件[0]);
label12.Text=目录路径;
label12.可见=真;
pictureBox2.Load(文件[0]);
pictureBox1.Load(文件[0]);
trackBar1.最小值=0;
trackBar1.max=files.Length-1;
}
}
捕获(IOException)
{
}
}
}
private void beginOperationToolStripMenuItem\u单击(对象发送方,事件参数e)
{
如果(files.Length>0)
{
backgroundWorker1.RunWorkerAsync();