Hello,
I finished my programm and wanted start the big test part.
Today I started it on a Windows 7 DualCore machine and my BackgroundWorker is damm slow now!
I coded it with a crappy old xp pc and everything works fine.
My computer just takes 3 to 4 seconds to render the image
The Widnows 7 PC took about 4-5 Minutes!!!
WTF?
Heres my Code:
My BackgroundWorker creates a virtual Bitmap and if it is finished the Bitmap becomes the PictureBox's Background.
private void paintInBackground_DoWork(object sender, DoWorkEventArgs e)
{
int w = 390 - 1, h = 390 - 1;
bitmap = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
bmpgrafik = Graphics.FromImage(bitmap);
bmpgrafik.CompositingMode = CompositingMode.SourceCopy;
bmpgrafik.CompositingQuality = CompositingQuality.GammaCorrected;
painterflag = false; // Enthält ein Messwert eine Note unter 1 ist etwas schief gelaufen
Color notenfarbe = new Color();
try
{
if (measuredatas != null)
{
for (int k = 0; k < measuredatas[aktTab].Length && invisible_or_blasser == "blasser"; k++)// Bei der Angabe "blasser" werden die punkte mit einen niedrigeren A Wert
// angegeben. Dadurch ist dieser Punkt eben blasser
// Ist "unsichtbar" angegeben dann geht er erst gar nicht in diese schleife und lässt den Punkt aus
{
notenfarbe = choosemarkcolor(measuredatas[aktTab][k].Mark);
notenfarbe = Color.FromArgb(65, notenfarbe);
if (dottype == 0)
bmpgrafik.FillRectangle(new SolidBrush(notenfarbe), (int)measuredatas[aktTab][k].CoordX + w / 2 - dotstrength, (int)measuredatas[aktTab][k].CoordY * -1 + h / 2 - dotstrength, dotstrength * 2 + 1, dotstrength * 3 + 1);
else if (dottype == 1)
bmpgrafik.FillEllipse(new SolidBrush(notenfarbe), (int)measuredatas[aktTab][k].CoordX + w / 2 - dotstrength, (int)measuredatas[aktTab][k].CoordY * -1 + h / 2 - dotstrength, (dotstrength * 2 + 1), (dotstrength * 2 + 1));
}
for (int i = 0; i < measuredatas[aktTab].Length; i++)
{
bool imageflag = false;
if ((measuredatas[aktTab][i].Theta <= 10 && isThetaSelected[paintertabnumber, 0] == true) ||
(measuredatas[aktTab][i].Theta > 10 && measuredatas[aktTab][i].Theta <= 20 && isThetaSelected[paintertabnumber, 1] == true) ||
(measuredatas[aktTab][i].Theta > 20 && measuredatas[aktTab][i].Theta <= 30 && isThetaSelected[paintertabnumber, 2] == true) ||
(measuredatas[aktTab][i].Theta > 30 && measuredatas[aktTab][i].Theta <= 40 && isThetaSelected[paintertabnumber, 3] == true) ||
(measuredatas[aktTab][i].Theta > 40 && measuredatas[aktTab][i].Theta <= 50 && isThetaSelected[paintertabnumber, 4] == true) ||
(measuredatas[aktTab][i].Theta > 50 && measuredatas[aktTab][i].Theta <= 60 && isThetaSelected[paintertabnumber, 5] == true) ||
(measuredatas[aktTab][i].Theta > 60 && measuredatas[aktTab][i].Theta <= 70 && isThetaSelected[paintertabnumber, 6] == true) ||
(measuredatas[aktTab][i].Theta > 70 && measuredatas[aktTab][i].Theta <= 80 && isThetaSelected[paintertabnumber, 7] == true)
)
{
if (// Oben
(isPhiSelected[aktTab, 0] == true && measuredatas[aktTab][i].Phi >= 90 - categories[aktcat].TopPhi / 2 && measuredatas[aktTab][i].Phi <= 90 + categories[aktcat].TopPhi / 2) ||
(isPhiSelected[aktTab, 1] == true && ((measuredatas[aktTab][i].Phi <= 180 - categories[aktcat].MiddlePhi / 2 && measuredatas[aktTab][i].Phi > 90 + categories[aktcat].TopPhi / 2) ||
(measuredatas[aktTab][i].Phi > categories[aktcat].MiddlePhi / 2 && measuredatas[aktTab][i].Phi < 90 - categories[aktcat].TopPhi / 2))) ||
// Mitte
(isPhiSelected[aktTab, 2] == true && ((measuredatas[aktTab][i].Phi >= 360 - categories[aktcat].MiddlePhi / 2 || measuredatas[aktTab][i].Phi <= categories[aktcat].MiddlePhi / 2) ||
(measuredatas[aktTab][i].Phi <= categories[aktcat].MiddlePhi / 2) || (measuredatas[aktTab][i].Phi >= 180 - categories[aktcat].MiddlePhi / 2 && measuredatas[aktTab][i].Phi <= 180 + categories[aktcat].MiddlePhi / 2))) ||
(isPhiSelected[aktTab, 2] == true && measuredatas[aktTab][i].Phi == 360) ||
(isPhiSelected[aktTab, 2] == true && measuredatas[aktTab][i].Phi == 0) ||
// Unten
(isPhiSelected[aktTab, 3] == true && ((measuredatas[aktTab][i].Phi <= 360 - categories[aktcat].MiddlePhi / 2 && measuredatas[aktTab][i].Phi >= 270 + categories[aktcat].BottomPhi / 2) ||
(measuredatas[aktTab][i].Phi < 270 - categories[aktcat].BottomPhi / 2 && measuredatas[aktTab][i].Phi > 180 + categories[aktcat].MiddlePhi / 2))) ||
(isPhiSelected[aktTab, 4] == true && measuredatas[aktTab][i].Phi >= 270 - categories[aktcat].BottomPhi / 2 && measuredatas[aktTab][i].Phi < 270 + categories[aktcat].BottomPhi / 2))
{
notenfarbe = choosemarkcolor(measuredatas[aktTab][i].Mark);
if (dottype == 0 && imageflag == false)
bmpgrafik.FillRectangle(new SolidBrush(notenfarbe), (int)measuredatas[aktTab][i].CoordX + w / 2 - dotstrength, (int)measuredatas[aktTab][i].CoordY * -1 + h / 2 - dotstrength, dotstrength * 2 + 1, dotstrength * 2 + 1);
else if (dottype == 1)
bmpgrafik.FillEllipse(new SolidBrush(notenfarbe), (int)measuredatas[aktTab][i].CoordX + w / 2 - dotstrength, (int)measuredatas[aktTab][i].CoordY * -1 + h / 2 - dotstrength, (dotstrength * 2 + 1), (dotstrength * 2 + 1));
}
}
}
}
if (showgrid == true)
{
for (int z = 0; z < 8; z++)
{
bmpgrafik.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(100, Color.Black)), 1), (imagepreview[aktTab].Width / 16) * z, (imagepreview[aktTab].Height / 16) * z, imagepreview[aktTab].Width - ((imagepreview[aktTab].Width / 8) * z) - 1, imagepreview[aktTab].Height - ((imagepreview[aktTab].Height / 8) * z) - 1);
}
//Vertikal Oben
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 90 - categories[aktcat].TopPhi / 2) + w / 2, MeasureData.polarToY(80, 90 - categories[aktcat].TopPhi / 2) * -1 + h / 2);
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 90 + categories[aktcat].TopPhi / 2) + w / 2, MeasureData.polarToY(80, 90 + categories[aktcat].TopPhi / 2) * -1 + h / 2);
//Horizontal
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 360 - categories[aktcat].MiddlePhi / 2) + w / 2, MeasureData.polarToY(80, 360 - categories[aktcat].MiddlePhi / 2) * -1 + h / 2);
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 0 + categories[aktcat].MiddlePhi / 2) + w / 2, MeasureData.polarToY(80, 0 + categories[aktcat].MiddlePhi / 2) * -1 + h / 2);
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 180 - categories[aktcat].MiddlePhi / 2) + w / 2, MeasureData.polarToY(80, 180 - categories[aktcat].MiddlePhi / 2) * -1 + h / 2);
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 180 + categories[aktcat].MiddlePhi / 2) + w / 2, MeasureData.polarToY(80, 180 + categories[aktcat].MiddlePhi / 2) * -1 + h / 2);
//Vertikal Unten
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 270 - categories[aktcat].BottomPhi / 2) + w / 2, MeasureData.polarToY(80, 270 - categories[aktcat].BottomPhi / 2) * -1 + h / 2);
bmpgrafik.DrawLine(new Pen(Color.FromArgb(100, Color.Black), 1), w / 2, h / 2, MeasureData.polarToX(80, 270 + categories[aktcat].BottomPhi / 2) + w / 2, MeasureData.polarToY(80, 270 + categories[aktcat].BottomPhi / 2) * -1 + h / 2);
}
}
catch { }
}
private void paintInBackground_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Alles erstmal wieder benutzbar machen
tabmeasure.Enabled = true;
try
{
imagepreview[paintertabnumber].Image = bitmap;
}
catch { }
bmpgrafik.Dispose();
try
{
fillsummary(newMeasure[paintertabnumber].Text);
if (markFlag)
{
string fehlermeldung = "";
fehlermeldung += "Die Notenmatrix wurde nicht korrekt ausgefüllt." + Environment.NewLine;
if (painterflag)
fehlermeldung += Environment.NewLine + "Dadurch entstanden Berechnungsfehler der Noten." + Environment.NewLine +
"Bitte korrigieren Sie die Notenmatrix um Berechnungsfehler zu vermeiden.";
else fehlermeldung += "Dadurch könnten Berechnungsfehler entstanden zu sein.";
MessageBox.Show(fehlermeldung, "Warnung", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch
{ }
if (firststart == true && measurePath.Count > 1)
{
paintpicture(0);
firststart = false;
}
}
If having trouble understanding something pls feel free to ask me.
Hope you can help me...
Loading
Sascha BPosted Apr 8, 2011, 5:15 AM
It is in the backgroundworker do work method.
It has nothing special but a few lines could be the problem I think.
Theser are the lines:
bitmap = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
bmpgrafik = Graphics.FromImage(bitmap);
bmpgrafik.CompositingMode = CompositingMode.SourceCopy;
bmpgrafik.CompositingQuality = CompositingQuality.GammaCorrected;
Maybe the Pixelformat isn't good?
The problem has to be there ...
This is my last try. After this I let it go!
Edit: Solved!!! It was PixelFormat's fault!!!
I took Format64bppPArgb and id didn't worked/ worked very slow!
If I use Format32bppArgb it works great!!!
But I dunno why :) Anyway it works!!!
Sascha BPosted Apr 7, 2011, 10:28 AM
Same speed when I do it without the backgroundworker.
Some more ideas?...
Sascha BPosted Apr 7, 2011, 5:51 AM
I'll try it! Just give me time... I'm slow, too :)
I meant that I can't start the backgroudnworker again if it is still loading.
Only if the background isn't working. Hope that clears my weird sentence ...
FroglegPosted Apr 7, 2011, 5:46 AM
Sascha BPosted Apr 7, 2011, 5:42 AM
Didn't I catch that?
I thought with this the backgroundworker can't handle 2 things at the same time.
I also dispose the graphic after every drawing.
Should I dispose the bistmap,too if it was already drawn and it has to be redrawn?
Thank you Frogleg
FroglegPosted Apr 7, 2011, 5:36 AM
That is, check that your not creating the bitmap twice
Sascha BPosted Apr 7, 2011, 2:26 AM
That's what I tested, too. :)
But now in my code (first post) I created the bitmap first (not in the paint method!) and then I set it to the background.
If you do this like me it won't be drawn line by line. So I think it doesn't make any difference.
If I'm wrong please correct me.
FroglegPosted Apr 6, 2011, 7:25 PM
The runtime image is created by drawing single pixels line by line till the bitmap is created
Suthish NairPosted Apr 6, 2011, 12:02 PM
Sascha BPosted Apr 6, 2011, 4:12 AM
I created the bitmap, saved it to harddisc and load it as picturebox's background.
But it was just a bit slower than my current method. But I will try it once more because of this problem.
But a few minutes ago I discovered a very interesting thing on the windows 7 machines.
While it was loading we opened the taskmanager and my programm displays a part of the image that has to be shown at once!
Means (just a guessing): The virtual bitmap creation works well on both OS. But when the bitmap has to be set to the picturebox's background it slows down and this is the part where the programm takes so long to load on windows 7.
I mean these lines
try
{
imagepreview[paintertabnumber].Image = bitmap;
}
catch { }
bmpgrafik.Dispose();
FroglegPosted Apr 6, 2011, 3:59 AM
Go over your code again and see if you can optimize it
You are calculating and drawing 30,000 pixels, one at a time.
If the images are not dynamic - meaning they don't change - maybe you can save them to disk and load them that way
Sascha BPosted Apr 6, 2011, 3:01 AM
Sam HobbsPosted Apr 6, 2011, 2:52 AM
Note that I said "crappy old xp single core" because that is what you called it. I don't know how else to refer to it.
You say that it "has a Geforce FX5500 inside". I assume that is a good graphics board. That is consistent with what I said. So listen to me. Is the graphics in the older system better than the graphics in the newer system? I hope that is clear enough; I don't know how to make it more clear.
You asked for theories of why the graphics would be faster in an old system and my theory is that the older system is actually a newer system. Is the graphics in the old system newer?
Sascha BPosted Apr 6, 2011, 2:30 AM
I don't think that hardware is causing the problem.
I've now tested it on 6 PCs
3 XP PCs = working perfectly
3 Windows 7 PCs = really slow loading
Is it maybe a .net framework problem?
Sam HobbsPosted Apr 5, 2011, 4:52 PM
Sascha BPosted Apr 5, 2011, 9:28 AM
Because of this I'm going to test this on several pcs.
So that I can exclude some things
FroglegPosted Apr 5, 2011, 9:26 AM
Sascha BPosted Apr 5, 2011, 9:08 AM
I try to get some other PCs here. And test it there.
Try to add some info if I have some new ones.
FroglegPosted Apr 5, 2011, 9:06 AM
Go into the debug folder and run the exe from there and see if it is any faster
Sascha BPosted Apr 5, 2011, 9:05 AM
I thought someone knows this problem. Or got some OS/MultiCore trouble.
Thank you anyway
FroglegPosted Apr 5, 2011, 9:01 AM
Sascha BPosted Apr 5, 2011, 8:52 AM
There isn't that much that is done around this. But I'm sure the things that I can't show you have nothing to do with the slow rendering.
What I can additional say are these things:
The paint method is called after I calculate my 28.800 marks and everything.
So every intensive array math is done before the painting starts.
If I start painting I lock my Tabcontrol so nothing can happen during painting.
When I start painting I call the backgroundworker, who does only these steps:
Have you any idea why this is slower on a Windows 7 dua core machin than on a crappy old xp single core one?
Have I used a method that is a known issue on windows 7?
Or is there a problem with the backgroudnworker and dua core?
... Thanks
FroglegPosted Apr 5, 2011, 8:37 AM
Sascha BPosted Apr 5, 2011, 8:21 AM
Anyway...
Thanks for taking the time Frogleg, again :)
I attached an image. Hope it can help you to understand what is drawn.
The image represents 28.800 marks
Every mark (1-5) gets a different color pink-darkblue
So before I drew this image I have done the calculation already. So that can't be th problem.
Edit: Every mark is one dot on the image. And I have several configuration methods to change what has to be drawn. Thats because it looks so confusing. It is confusing me anyway :D
FroglegPosted Apr 5, 2011, 8:10 AM