public static HeightField FromBitmap(Bitmap bitmap) { HeightField hField = new HeightField(bitmap.Width, bitmap.Height); BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unsafe { ARGB_32Bit *pixels = (ARGB_32Bit *)data.Scan0.ToPointer(); int dataIdx = 0; for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { hField.values[dataIdx++] = (pixels->R + pixels->G + pixels->B) / 765f; pixels++; } } } bitmap.UnlockBits(data); return(hField); }
public Bitmap Rasterize() { if (buffer.AALayer != null) { buffer.ApplyAAFilter(); } Bitmap bitmap = new Bitmap(buffer.Width, buffer.Height); BitmapData data = bitmap.LockBits(viewArea, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); unsafe { ARGB_32Bit *pixels = (ARGB_32Bit *)data.Scan0.ToPointer(); int dataIdx = 0; for (int y = 0; y < buffer.Height; y++) { for (int x = 0; x < buffer.Width; x++) { float value = buffer.Data[dataIdx++]; pixels->B = (byte)((int)value >> 16); pixels->G = (byte)((int)value >> 8); pixels->R = (byte)((int)value & 0xFF); pixels->A = (byte)((int)value >> 24); // add bg if alpha < 255 if (pixels->A == 0) { pixels->B = buffer.Bg.B; pixels->G = buffer.Bg.G; pixels->R = buffer.Bg.R; pixels->A = buffer.Bg.A; } else if (pixels->A != 255) { double alpha = (double)pixels->A / 255; double ra = 1 - alpha; pixels->B = (byte)((buffer.Bg.B * ra) + (pixels->B * alpha)); pixels->G = (byte)((buffer.Bg.G * ra) + (pixels->G * alpha)); pixels->R = (byte)((buffer.Bg.R * ra) + (pixels->R * alpha)); pixels->A = buffer.Bg.A; // maybe add alpha also } pixels++; } } } bitmap.UnlockBits(data); Graphics g = Graphics.FromImage(bitmap); g.DrawString(stats, new Font("Tahoma", 6), Brushes.White, 0, 0); int size = buffer.Width / 4; g.DrawImage(texture, buffer.Width - size, buffer.Height - size, size, size); // put alpha back into text //SizeF size = g.MeasureString(stats, new Font("Tahona", 6)); //data = bitmap.LockBits(new Rectangle(0, 0, (int)size.Width, (int)size.Height), // ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); //unsafe //{ // for (int y = 0; y < size.Height; y++) // { // ARGB_32Bit* pixels = (ARGB_32Bit*)data.Scan0.ToPointer() + (y * viewArea.Width); // for (int x = 0; x < size.Width; x++) // { // if (pixels->A == 0/* && (pixels->R != 0 || pixels->G != 0 || pixels->B != 0)*/) // pixels->A = 255; // pixels++; // } // } //} //bitmap.UnlockBits(data); //bitmap.Save("c:/output-HM.png", ImageFormat.Png); return(bitmap); }