private void worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgWorker = sender as BackgroundWorker;
//hps, width, height, Radius, Intensity, stops, onCompleted
object[] args = (object[])e.Argument;
List<PixelPoint> points = (List<PixelPoint>)args[0];
double res = (double)args[4];
int width = (int)Math.Ceiling((int)args[1] * res);
int height = (int)Math.Ceiling((int)args[2] * res);
int radius = (int)Math.Ceiling((int)args[3] * res);
List<ThreadSafeGradientStop> stops = (List<ThreadSafeGradientStop>)args[5];
OnImageSourceCompleted onCompleted = (OnImageSourceCompleted)args[6];
double mapReslution = (double)args[7];
//地理半径
int geoToScreenRadius;
//if (radius % 2 == 0)
//{
// radius++;
//}
//radius = radius * 2 + 1;
ushort[] matrix = CreateDistanceMatrix(radius);
int[] output = new int[width * height];
foreach (PixelPoint p in points)
{
//如果当前点设置地理半径,就用这样的方法
if (p.GeoRadius != 0 )
{
geoToScreenRadius = (int)Math.Round(p.GeoRadius / mapReslution);
//如果显示的地理半径小于1像素则不进行显示
if (!(geoToScreenRadius < 1))
{
ushort[] matrixGeoRadius = CreateDistanceMatrix(geoToScreenRadius);
AddPoint(matrixGeoRadius, geoToScreenRadius, p.X, p.Y, p.Value, output, width);
}
}
else
{
AddPoint(matrix, radius, p.X, p.Y, p.Value, output, width);
}
if (worker.CancellationPending)
{
e.Cancel = true;
e.Result = null;
return;
}
}
matrix = null;
int max = 0;
foreach (int val in output)
{
if (max < val)
{
max = val;
}
}
if (max < 2)
{
max = 2;
}
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) // Height (y)
{
for (int j = 0; j < width; j++) // Width (x)
{
Color c = InterpolateColor(output[i * width + j] / (float)max, stops);
if (c.A == 0)
{
pixels[i * width + j] = 0;
}
else
{
//byte[] brg = new byte[4] { c.B, c.G, c.R, c.A };
//int pixel = BitConverter.ToInt32(brg, 0);//ok
int pixel = (c.A << 24) | (c.R << 16) | (c.G << 8) | c.B;
pixels[i * width + j] = pixel;
}
}
if (bgWorker.CancellationPending)
{
e.Cancel = true;
e.Result = null;
output = null;
pixels = null;
return;
}
//一行报告进度一次
//调用 ReportProgress 方法时将引发ProgressChanged 事件。
bgWorker.ReportProgress((i + 1) * 100 / height);
}
stops.Clear();
output = null;
e.Result = new object[] { pixels, width, height, onCompleted };
}