void Worker_DoWork(object sender, DoWorkEventArgs e) { try { double[,] data = NoiseFactory.GeneratePlanar((ModuleBase)e.Argument, previewWidth, previewHeight, -1, 1, -1, 1, true, true, 0); Bitmap bm = new Bitmap(previewWidth, previewHeight); for (int x = 0; x < previewWidth; x++) { for (int y = 0; y < previewHeight; y++) { double val = data[x, y]; val = val > 1.0 ? 1.0 : val; val = val < 0 ? 0 : val; // typically, I'd rather do image work with lockbits, but for something so small the // slow setpixels method should work fine. int colorValue = Convert.ToInt32(Math.Round((255.0f * val), 0, MidpointRounding.ToEven)); bm.SetPixel(x, y, System.Drawing.Color.FromArgb(colorValue, colorValue, colorValue)); } } PreviewImageBitmap = bm; } catch (Exception ex) { // can't prepare the preview image. just toss back an empty one for now // it's most likely that the diagram is invalid PreviewImageBitmap = new Bitmap(previewWidth, previewHeight); } }
void bw_DoWork(object sender, DoWorkEventArgs e) { try { double[,] noise = null; //depending on the style selected, create some default noise values if (selectedNoiseStyle.Equals(NoiseStyles.Planar)) { noise = NoiseFactory.GeneratePlanar(module, imageWidth, imageHeight, -1, 1, -1, 1, true, true, 0); } if (selectedNoiseStyle.Equals(NoiseStyles.Cylindrical)) { noise = NoiseFactory.GenerateCylindrical(module, imageWidth, imageHeight, -1, 180, -1, 1, true, 0); } else { noise = NoiseFactory.GenerateSpherical(module, imageWidth, imageHeight, worldSouth, worldNorth, worldWest, worldEast, true, 0); } // Generate the image from the nosie values System.Drawing.Rectangle areaToPaint = new System.Drawing.Rectangle(0, 0, imageWidth, imageHeight); Bitmap bmp = new Bitmap(imageWidth, imageHeight); BitmapData bmData = bmp.LockBits(areaToPaint, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //Generate a second image to overlay "Hillshading" for the world style Bitmap hillshadeBmp = new Bitmap(imageWidth, imageHeight); BitmapData hsbmData = hillshadeBmp.LockBits(areaToPaint, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); int stride = imageWidth * 4; unsafe { byte *ptr = (byte *)bmData.Scan0.ToPointer(); byte *hsPtr = (byte *)hsbmData.Scan0.ToPointer(); for (int y = areaToPaint.Top; y < areaToPaint.Height; y++) { for (int x = areaToPaint.Left; x < areaToPaint.Width; x++) { double heightValue = noise[x, y]; System.Drawing.Color finalColor = System.Drawing.Color.Black; if (selectedColorStyle.Equals(ColourStyles.Greyscale)) { finalColor = GetGradientColor(System.Drawing.Color.Black, System.Drawing.Color.White, (float)heightValue); } else if (selectedColorStyle.Equals(ColourStyles.RedBlue)) { finalColor = GetGradientColor(System.Drawing.Color.Blue, System.Drawing.Color.Red, (float)heightValue); } else { // "World" style if (heightValue <= 0.5) { finalColor = System.Drawing.Color.PowderBlue; } else if (heightValue >= 0.9) { finalColor = GetGradientColor(System.Drawing.Color.LightGray, System.Drawing.Color.Snow, (float)heightValue); } else { finalColor = GetGradientColor(System.Drawing.Color.LightGreen, System.Drawing.Color.ForestGreen, (float)heightValue); } System.Drawing.Color hillshading = Hillshade(noise, imageWidth, imageHeight, x, y, 10, 45, 315, 1); hsPtr[(x * 4) + y * stride] = ((Color)hillshading).B; hsPtr[(x * 4) + y * stride + 1] = ((Color)hillshading).G; hsPtr[(x * 4) + y * stride + 2] = ((Color)hillshading).R; byte trans = 255; if (heightValue > 0.5) { trans = (byte)(((Color)hillshading).A * 0.3); // over land set transparency to 30% } else { trans = (byte)(((Color)hillshading).A * 0.1); // over water transparency to 10% } hsPtr[(x * 4) + y * stride + 3] = trans; } ptr[(x * 4) + y * stride] = finalColor.B; ptr[(x * 4) + y * stride + 1] = finalColor.G; ptr[(x * 4) + y * stride + 2] = finalColor.R; ptr[(x * 4) + y * stride + 3] = finalColor.A; } } } bmp.UnlockBits(bmData); hillshadeBmp.UnlockBits(hsbmData); if (selectedColorStyle.Equals(ColourStyles.World)) { Bitmap mergedResult = new Bitmap(imageWidth, imageHeight); Graphics canvas = Graphics.FromImage(mergedResult); canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); canvas.DrawImage(hillshadeBmp, new Rectangle(0, 0, hillshadeBmp.Width, hillshadeBmp.Height), new Rectangle(0, 0, hillshadeBmp.Width, hillshadeBmp.Height), GraphicsUnit.Pixel); canvas.Save(); previewBmp = mergedResult; } else { previewBmp = bmp; } } catch (Exception ex) { MessageBox.Show("An error has occured while generating the image preview. " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }