C# 图像文件上的大量for循环,帧倾斜约7 fps,我需要更高
我目前正在尝试新技术,通过图像上的每个像素,并为每个像素做一些小的处理。这是我的活动代码:C# 图像文件上的大量for循环,帧倾斜约7 fps,我需要更高,c#,image,performance,C#,Image,Performance,我目前正在尝试新技术,通过图像上的每个像素,并为每个像素做一些小的处理。这是我的活动代码: 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.Windo
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.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace ImageProcessingExmp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Timer timer = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
timer.Tick += ChangeColor;
timer.Interval = 1;
timer.Start();
pictureBox1.Width = this.Width;
pictureBox1.Height = this.Height;
pictureBox1.Left = 0;
pictureBox1.Top = 0;
this.WindowState = FormWindowState.Maximized;
}
int timerinc = 0;
public void ChangeColor(object sender, EventArgs e)
{
if (timerinc>=240)
{
timer.Stop();
}
timerinc+=10;
Bitmap bmp = (Bitmap)new Bitmap(this.Width,this.Height);
Benchmark.Start();
///////////////////////////////////////////////////////////////////////////////
// Get the area to be painted
Rectangle areaToPaint = new Rectangle();
BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = data.Stride;
Random rnd = new Random(DateTime.Now.Millisecond);
unsafe
{
byte* ptr = (byte*)data.Scan0;
// Check this is not a null area
// Go through the draw area and set the pixels as they should be
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
// layer.GetBitmap().SetPixel(x, y, m_colour);
byte x2 = Convert.ToByte(x > 255 ? 0 : x);
byte y2 = Convert.ToByte(y > 255 ? 0 : y);
ptr[(x * 3) + y * stride] = x2;
ptr[(x * 3) + y * stride + 1] = y2;
ptr[(x * 3) + y * stride + 2] = 255;
}
}
}
bmp.UnlockBits(data);
/////////////////////////////////////////////////////////////////////////////////
Benchmark.End();
double seconds = Benchmark.GetSeconds();
//MessageBox.Show(seconds.ToString());
richTextBox1.Text += seconds.ToString()+'\n' ;
richTextBox2.Text += DateTime.Now.Second.ToString()+'\n';
pictureBox1.Image= bmp;
String[] lines = richTextBox1.Text.Split('\n');
double avg = 0;
double sum = 0;
double num = 0;
foreach (var line in lines)
{
// MessageBox.Show(line);
double tet = 0;
Double.TryParse(line, out tet);
sum += tet;
num++;
}
avg = sum / num;
label1.Text = "fps = " + (1/avg).ToString();
}
public class Benchmark
{
private static DateTime startDate = DateTime.MinValue;
private static DateTime endDate = DateTime.MinValue;
public static TimeSpan Span { get { return endDate.Subtract(startDate); } }
public static void Start() { startDate = DateTime.Now; }
public static void End() { endDate = DateTime.Now; }
public static double GetSeconds()
{
if (endDate == DateTime.MinValue) return 0.0;
else return Span.TotalSeconds;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统图;
使用系统、绘图、成像;
使用System.Runtime.InteropServices;
命名空间ImageProcessingExmp1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
定时器=新定时器();
私有void Form1\u加载(对象发送方、事件参数e)
{
timer.Tick+=ChangeColor;
时间间隔=1;
timer.Start();
pictureBox1.Width=此.Width;
pictureBox1.Height=此.Height;
pictureBox1.Left=0;
pictureBox1.Top=0;
this.WindowState=FormWindowState.Maximized;
}
int timerinc=0;
public void ChangeColor(对象发送方,事件参数e)
{
如果(timerinc>=240)
{
timer.Stop();
}
timerinc+=10;
位图bmp=(位图)新位图(this.Width,this.Height);
Benchmark.Start();
///////////////////////////////////////////////////////////////////////////////
//获取要绘制的区域
矩形区域油漆=新矩形();
BitmapData data=bmp.LockBits(新矩形(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
int-stride=data.stride;
Random rnd=新随机(DateTime.Now.毫秒);
不安全的
{
字节*ptr=(字节*)data.Scan0;
//检查这不是一个空区域
//穿过绘图区域,将像素设置为应有的值
对于(int y=0;y255?0:x);
字节y2=转换为字节(y>255?0:y);
ptr[(x*3)+y*步幅]=x2;
ptr[(x*3)+y*步幅+1]=y2;
ptr[(x*3)+y*步幅+2]=255;
}
}
}
解锁位(数据);
/////////////////////////////////////////////////////////////////////////////////
Benchmark.End();
双秒=Benchmark.GetSeconds();
//Show(seconds.ToString());
richTextBox1.Text+=seconds.ToString()+'\n';
richTextBox2.Text+=DateTime.Now.Second.ToString();
pictureBox1.Image=bmp;
String[]lines=richTextBox1.Text.Split('\n');
双平均值=0;
双和=0;
双数=0;
foreach(行中的var行)
{
//MessageBox.Show(行);
双tet=0;
双色锥巴色(行,外tet);
总和+=tet;
num++;
}
平均值=总和/数量;
label1.Text=“fps=”+(1/avg.ToString();
}
公共课基准
{
私有静态DateTime startDate=DateTime.MinValue;
私有静态DateTime endDate=DateTime.MinValue;
公共静态时间跨度{get{return endDate.Subtract(startDate);}
公共静态void Start(){startDate=DateTime.Now;}
公共静态void End(){endDate=DateTime.Now;}
公共静态双GetSeconds()
{
如果(endDate==DateTime.MinValue)返回0.0;
否则返回Span.TotalSeconds;
}
}
}
}
我需要把FPS调到90 FPS左右。我该怎么做?我需要使用C#以外的东西吗?目前的fps约为7
编辑1
堆栈溢出编辑掉了我的一些文本。。奇怪的
这是真正的不安全代码
unsafe
{
byte* ptr = (byte*)data.Scan0;
// Check this is not a null area
// Go through the draw area and set the pixels as they should be
for (int y = 0; y< bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
// layer.GetBitmap().SetPixel(x, y, m_colour);
byte x2 = Convert.ToByte(x > 255 ? 0 : x);
byte y2 = Convert.ToByte(y > 255 ? 0 : y);
ptr[(x * 3) + y * stride] = x2;
ptr[(x * 3) + y * stride + 1] = y2;
ptr[(x * 3) + y * stride + 2] = 255;
}
}
}
不安全
{
字节*ptr=(字节*)data.Scan0;
//检查这不是一个空区域
//穿过绘图区域,将像素设置为应有的值
对于(int y=0;y255?0:x);
字节y2=转换为字节(y>255?0:y);
ptr[(x*3)+y*步幅]=x2;
ptr[(x*3)+y*步幅+1]=y2;
ptr[(x*3)+y*步幅+2]=255;
}
}
}
编辑2
哇!它又做了一次。。。我正在用(LESSTHAN)替换小于的符号
不安全
{
字节*ptr=(字节*)data.Scan0;
//检查这不是一个空区域
//穿过绘图区域,将像素设置为应有的值
对于(int y=0;y255?0:x);
字节y2=转换为字节(y>255?0:y);
ptr[(x*3)+y*步幅]=x2;
unsafe
{
byte* ptr = (byte*)data.Scan0;
// Check this is not a null area
// Go through the draw area and set the pixels as they should be
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
// layer.GetBitmap().SetPixel(x, y, m_colour);
byte x2 = Convert.ToByte(x > 255 ? 0 : x);
byte y2 = Convert.ToByte(y > 255 ? 0 : y);
ptr[(x * 3) + y * stride] = x2;
ptr[(x * 3) + y * stride + 1] = y2;
ptr[(x * 3) + y * stride + 2] = 255;
}
}
}
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.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace ImageProcessingExmp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Timer timer = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
timer.Tick += ChangeColor;
timer.Interval = 1;
timer.Start();
pictureBox1.Width = this.Width;
pictureBox1.Height = this.Height;
pictureBox1.Left = 0;
pictureBox1.Top = 0;
this.WindowState = FormWindowState.Maximized;
bmp=(Bitmap)new Bitmap(this.Width, this.Height);
}
int timerinc = 0;
int stride;
Bitmap bmp;
BitmapData data;
public void ChangeColor(object sender, EventArgs e)
{
timerinc+=10;
Benchmark.Start();
///////////////////////////////////////////////////////////////////////////////
data= bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
stride = data.Stride;
unsafe
{
byte* ptr = (byte*)data.Scan0;
// Check this is not a null area
// Go through the draw area and set the pixels as they should be
for (int y = 0; y < bmp.Height; y++)
{
int yTimeStride = y * stride;
byte y2 = (byte)(y > 255 ? 0 : y);
for (int x = 0; x < bmp.Width; x++)
{
int addressToAssign = (x * 3) + yTimeStride;
byte x2 = (byte)(x > 255 ? 0 : x);
ptr[addressToAssign] = x2;
ptr[addressToAssign + 1] = y2;
ptr[addressToAssign + 2] = x2;
}
}
}
bmp.UnlockBits(data);
/////////////////////////////////////////////////////////////////////////////////
Benchmark.End();
double seconds = Benchmark.GetSeconds();
//MessageBox.Show(seconds.ToString());
richTextBox1.Text += seconds.ToString()+'\n' ;
richTextBox2.Text += DateTime.Now.Second.ToString()+'\n';
pictureBox1.Image= bmp;
String[] lines = richTextBox1.Text.Split('\n');
double avg = 0;
double sum = 0;
double num = 0;
foreach (var line in lines)
{
// MessageBox.Show(line);
double tet = 0;
Double.TryParse(line, out tet);
sum += tet;
num++;
}
avg = sum / num;
label1.Text = "fps = " + (1/avg).ToString();
tst1.Add(1/avg*20);
if (timerinc >= 500)
{
timer.Stop();
Bitmap bmp2 = new Bitmap(pictureBox2.Width,pictureBox2.Height);
data = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
stride = data.Stride;
unsafe
{
byte* ptr = (byte*)data.Scan0;
// Check this is not a null area
// Go through the draw area and set the pixels as they should be
for (int x = 0; x < tst1.Count; x++)
{
for (int y = 0; y < (int)tst1[x]; y++)
{
//MessageBox.Show(x.ToString());
if (y<bmp2.Height)
{
ptr[(x * 3) + (int)y * stride] = 255;//b
ptr[(x * 3) + (int)y * stride + 1] =0;//g
ptr[(x * 3) + (int)y * stride + 2] = 255;//r
}
}
}
}
bmp2.UnlockBits(data);
pictureBox2.Image = bmp2;
}
}
public List<double> tst1 = new List<double>();
public class Benchmark
{
private static DateTime startDate = DateTime.MinValue;
private static DateTime endDate = DateTime.MinValue;
public static TimeSpan Span { get { return endDate.Subtract(startDate); } }
public static void Start() { startDate = DateTime.Now; }
public static void End() { endDate = DateTime.Now; }
public static double GetSeconds()
{
if (endDate == DateTime.MinValue) return 0.0;
else return Span.TotalSeconds;
}
}
}
}
for (int y = 0; y < bmp.Height; y++)
{
int yTimeStride = y * stride;
byte y2 = Convert.ToByte(y > 255 ? 0 : y);
for (int x = 0; x < bmp.Width; x++)
{
int addressToAssign = (x * 3) + yTimeStride;
byte x2 = Convert.ToByte(x > 255 ? 0 : x);
ptr[addressToAssign] = x2;
ptr[addressToAssign + 1] = y2;
ptr[addressToAssign + 2] = 255;
}
}