public static Region ConvertFromTransparentBitmap(UnsafeBitmap imageRegion, Color transparentColor) { // First we get the dimensions of our image GraphicsUnit aPixel = GraphicsUnit.Pixel; RectangleF imageBoundsF = imageRegion.GetBounds(ref aPixel); int imageWidth = Convert.ToInt32(imageBoundsF.Width); int imageHeight = Convert.ToInt32(imageBoundsF.Height); // This will be the path for our Region GraphicsPath regionPath = new GraphicsPath(); // We loop over every line in our image, and every pixel per line for (int intY = 0; intY < imageHeight; intY++) { for (int intX = 0; intX < imageWidth; intX++) { if (imageRegion.GetPixel(intX, intY) != transparentColor) { // We have to see this pixel! regionPath.AddRectangle(new Rectangle(intX, intY, 1, 1)); } } } Region formRegion = new Region(regionPath); regionPath.Dispose(); return(formRegion); } /* ConvertFromTransparentBitmap */
/// <summary> /// This method will generate an image from the provided data. It will /// also zero out any values that fall below the lowBoundary parameter. /// This is a good way to eliminate a lot of noise. /// </summary> /// <param name="data"></param> /// <param name="lowBoundary"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Bitmap generateImage(int[] data, int lowBoundary, int width, int height) { int count = 0; int value = 0; Bitmap result = null; using (UnsafeBitmap bitmap = new UnsafeBitmap(new Bitmap(width, height))) { PixelData pdata; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { value = data[count++]; if (value < lowBoundary) { value = 0; } pdata.red = pdata.green = pdata.blue = (byte)value; bitmap.SetPixel(x, y, pdata); } } bitmap.UnlockBitmap(); result = new Bitmap(bitmap.Bitmap); } return(result); }
public static Point PixelSize(UnsafeBitmap bitmap) { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF bounds = bitmap.GetBounds(ref unit); return(new Point((int)bounds.Width, (int)bounds.Height)); }
public static double[] ExtractGreyValues(UnsafeBitmap bitmap) { Point size = PixelSize(bitmap); double[] data = new double[size.Y * size.X]; int count = 0; for (int y = 0; y < size.Y; y++) { for (int x = 0; x < size.X; x++) { Color c = bitmap.GetPixel(x, y); double dvalue = (0.299 * c.R) + (0.587 * c.G) + (0.114 * c.B); data[count++] = dvalue; } } return(data); }