我已经从这里给出的答案中编写了代码
我的示例代码如下
void Process(int i)
{
input = (Bitmap)Bitmap.FromFile(@"filepath.bmp");
Bitmap temp = new Bitmap(input.Width, input.Height,
PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(temp);
g.Clear(Color.Red);
g.DrawImage(input, Point.Empty);
temp.Save(stream, ImageFormat.Bmp);
//I need this stream thats further processing
}
void timer_Ex()
{
int i = 11;
for (; ; )
{
if (i == 335) break;
else
{
st.Start();
Process(i);
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds.ToString());
//This time is more than the time in which thread sleeps
st.Reset();
Thread.Sleep(70);
i++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我试图将图像从rgb32转换为rgb24.但是处理所需的时间比线程休眠的时间要长.它只是一个示例代码.所以请帮我解决这个问题"如何优化进程(int i)在20毫秒或低于100毫秒内执行?"
能够显着改善您的代码的事情: -
您的"输入"图像始终是相同的图像.但是每次调用Process()时都会加载它.使其成为会员,并仅加载一次.
每次调用Process()时都会分配"temp"位图,同样不需要.使其成为成员,并分配一次.
您不会丢弃任何Graphics对象.当你完成位图时应该处理位图,就像Graphics一样.
这不是一个表演的事情,但你的for循环非常奇怪,而且难以理解.为什么要尝试重新构建内置的语言结构?出什么问题了
for (int i=11; i != 335 ; i++)
{
st.Start();
Process(i);
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds.ToString());
//This time is more than the time in which thread sleeps
st.Reset();
Thread.Sleep(70); }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
192 次 |
| 最近记录: |