在Picturbox中为BMP提供更好的性能 [英] Better Performance For BMP In Picturbox
问题描述
各位大家好!来
$
这是我第一次询问有关C#的事情,我需要在大学开展一个项目。
我必须想象一辆自行车跳过一个坡道,所以我需要一个看起来很流畅的帧速率。我的问题是我只能达到大约10 fps。我正在寻找大约4或5个小时的解决方案,但我没有得到一个我可以使用/理解。我几乎是这个节目的初学者
,所以我希望你们中的某些人可以帮助我。
Hello everybody!
That's the first time i ask something about C# and i need it for a project at the university.
I have to visualize a bike jumping over a ramp, so i need a frame rate which looks fluently. My Problem is that i only reach about 10 fps. I was searching for about 4 or 5 hours for a solution, but i don't get one which i can use/understand. I'm nearly a beginner
to this program, so i hope somebody of you can help me.
节目:
我有两个图片盒,一个用于背景,一个用于自行车。我将背景设置为自行车的父母,并使自行车的背景透明。两个图片盒都具有相同的大小和位置。我的自行车是一个png,可视化工作
罚款。但是如果自行车正在移动,我只会得到低fps而且这真的很烦人。
I have two pictureboxes, one for the background and one for the bike. i set the background as the parent of the bike and made the background of Bike transparent. Both pictureboxes have the same size and position. My Bike is a png and the visualization works fine. But if the bike is moving, i only get low fps and that's really annoying.
private void timer_Tick(object sender, EventArgs e)
{
picBike.Invalidate();
tTimer++;
}
public void picBackground_Paint(object sender, PaintEventArgs e)
{
Graphics z = e.Graphics;
z.SmoothingMode = SmoothingMode.AntiAlias;
float Breite = X; // X-Koordinate in mm
float Höhe = maxY - minY; // Y-Koordinate in mm
Matrix myMatrix = new Matrix(); // Matrix für Koordinatentransformation
myMatrix.Scale(picBackground.ClientSize.Width / Breite, picBackground.ClientSize.Height / Höhe); // Koordinatensystemgröße transformieren
myMatrix.Translate(0, maxY, MatrixOrder.Prepend); // Koordinatenursprung transformieren
z.Transform = myMatrix; // Koordinatensystem transformieren
Pen blackPen = new Pen(Color.Black, 3); // Stift Schwarz
AbsprungwinkelGrad = (double)numAngle.Value; // Rampenabsprungwinkel in Grad
AbsprungwinkelRadiant = AbsprungwinkelGrad * Math.PI / 180; // Rampenabsprungwinkel in Rad
Rampenradius = (double)numRadius.Value / 1000; // Radius in m
XRampengröße = 2 * Rampenradius - Rampenradius * (1 - Math.Sin(AbsprungwinkelRadiant)); // Rampenlänge in m
// Rechtecksfläche für Rampe
Rectangle rect = new Rectangle((int)(XRampenende * 1000) - (int)(XRampengröße * 1000), -(int)(Rampenradius * 1000 * 2), (int)(Rampenradius * 1000 * 2), (int)(Rampenradius * 1000 * 2));
// Ellipsenstart- und Endpunkt
float startAngle = 90;
float sweepAngle = -(float)AbsprungwinkelGrad;
// Ellipsenzeichnung
z.DrawArc(blackPen, rect, startAngle, sweepAngle);
z.DrawLine(blackPen, 0, 0, X, 0);
// Landerampe
double xl = BsX[(int)tEnd];
double yl = BsY[(int)tEnd];
z.DrawLine(blackPen, (float)((xl * 1000 + Bikelänge / 2 * 1000 * Math.Cos(Bikewinkelfalsch[(int)tEnd] * Math.PI / 180)) - 2000), (float)((-yl * 1000) + 2000 * Math.Sin(Bikewinkelfalsch[(int)tEnd] * Math.PI / 180)), (float)(xl * 1000 + yl * 1000 / (-Math.Tan(Bikewinkelfalsch[(int)tEnd] * Math.PI / 180)) + Bikelänge / 2 * 1000 * Math.Cos(Bikewinkelfalsch[(int)tEnd] * Math.PI / 180)), (float)0);
}
private void picBike_Paint(object sender, PaintEventArgs e)
{
Graphics z = e.Graphics;
z.SmoothingMode = SmoothingMode.AntiAlias;
float Breite = X; // X-Koordinate in mm
float Höhe = maxY - minY; // Y-Koordinate in mm
Matrix myMatrix = new Matrix(); // Matrix für Koordinatentransformation
myMatrix.Scale(picBike.ClientSize.Width / Breite, picBike.ClientSize.Height / Höhe); // Koordinatensystemgröße transformieren
myMatrix.Translate(0, maxY, MatrixOrder.Prepend); // Koordinatenursprung transformieren
z.Transform = myMatrix; // Koordinatensystem transformieren
Pen blackPen = new Pen(Color.Black, 3);
// Bikevisualisierung
imgBike = Backflip.Properties.Resources.Projekt_Bike;
Bitmap bmp = new Bitmap((int)(Bikelänge * 1000), (int)(Bikehöhe * 1000));
int newWidth = (int)Math.Sqrt(bmp.Height * bmp.Height + bmp.Width * bmp.Width);
int newHeigth = (int)Math.Sqrt(bmp.Height * bmp.Height + bmp.Width * bmp.Width);
Bikewinkelrichtig = Bikewinkelfalsch[(int)tTimer];
Bikewinkelrichtig %= 360; // Bikedrehung kann nicht mehr über 360 Grad
Bitmap MyreturnBitmap = new Bitmap(newWidth, newHeigth);
Graphics graphics = Graphics.FromImage(MyreturnBitmap);
//graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;//Drawing2D.InterpolationMode.NearestNeighbor
graphics.TranslateTransform(newWidth / 2, newHeigth / 2);
graphics.RotateTransform(-(float)Bikewinkelrichtig);
graphics.TranslateTransform(-(int)(Bikelänge * 1000) / 2, -(int)(Bikehöhe * 1000) / 2);
graphics.DrawImage(imgBike, 0, 0, (int)(Bikelänge * 1000), (int)(Bikehöhe * 1000));
z.TranslateTransform(-(MyreturnBitmap.Width / 2), -(MyreturnBitmap.Height / 2));
z.DrawImage(MyreturnBitmap, (float)BsX[(int)tTimer] * 1000 + bmp.Width / 2, -(float)BsY[(int)tTimer] * 1000 - bmp.Height / 2, MyreturnBitmap.Width, MyreturnBitmap.Height);
z.DrawRectangle(blackPen, (float)BsX[(int)tTimer] * 1000 + (MyreturnBitmap.Width / 2 - imgBike.Width / 2), -(float)Bikehöhe * 1000 - (float)BsY[(int)tTimer] * 1000 + (MyreturnBitmap.Height / 2 - imgBike.Height / 2), 100, 100);
GC.Collect();
}
如果您需要英文变量和评论,请告诉我,我会更改它们。
If you need the variables and comments in English, please tell me and i will change them.
提前谢谢。
推荐答案
感谢您的回复。我
我忘了写,我对这个理论知之甚少。我现在写的程序比我们在讲座中写的那么复杂。
我很高兴,如果你能解释我的GDI +简单和简短的方式,因为我从来没有听说过这个以及如何更改代码以使用Direct2D。
Thank you for your respond.
I forgot to write, that i don't have much knowledge about the theory. The Program i'm writing now is way more complex than the one we did in the lectures.
I would be happy, if you can explain me the GDI+ in a simple and short way, because i never heard of this and how i can change the code to use Direct2D.
这篇关于在Picturbox中为BMP提供更好的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!