public override void Run() { try { using (var bmp = new Bitmap(Dimensions.Width, Dimensions.Height)) { int size = System.Math.Min(Dimensions.Width, Dimensions.Height); for (int i = 0; i < size; ++i) bmp.SetPixel(i, i, (Color)((255 - i) << 16)); for (int i = 0; i < size; i += 2) bmp.SetPixel(size - i, i, (Color)(i << 8)); bmp.Flush(); } Thread.Sleep(3000); Pass = true; } catch (Exception e) { UnexpectedException(e); } }
public static void ChangeBitmapColor(Bitmap img, Color anyDifferentColor, Color changeTo) { for (int x = 0; x < img.Width; x++) for (int y = 0; y < img.Height; y++) if (img.GetPixel(x, y) != anyDifferentColor) img.SetPixel(x, y, changeTo); }
/// <summary> /// Converts this ByteMatrix to a black and white bitmap. /// </summary> /// <returns>A black and white bitmap converted from this ByteMatrix.</returns> public Bitmap ToBitmap(BarcodeFormat format, String content) { int width = Width; int height = Height; bool outputContent = !(content == null || content.Length == 0) && (format == BarcodeFormat.CODE_39 || format == BarcodeFormat.CODE_128 || format == BarcodeFormat.EAN_13 || format == BarcodeFormat.EAN_8 || format == BarcodeFormat.CODABAR || format == BarcodeFormat.ITF || format == BarcodeFormat.UPC_A); int emptyArea = outputContent ? 16 : 0; // create the bitmap and lock the bits because we need the stride // which is the width of the image and possible padding bytes var bmp = new Bitmap(width, height); for (int y = 0; y < height - emptyArea; y++) { for (var x = 0; x < width; x++) { var color = this[x, y] ? Color.Black : Color.White; bmp.SetPixel(x, y, color); } } if (outputContent) { //switch (format) //{ // case BarcodeFormat.EAN_8: // if (content.Length < 8) // content = OneDimensionalCodeWriter.CalculateChecksumDigitModulo10(content); // content = content.Insert(4, " "); // break; // case BarcodeFormat.EAN_13: // if (content.Length < 13) // content = OneDimensionalCodeWriter.CalculateChecksumDigitModulo10(content); // content = content.Insert(7, " "); // content = content.Insert(1, " "); // break; //} bmp.DrawText(content, null, Color.Black, width / 2, height - 14); } return bmp; }
public override void Run() { try { using (var bmp = new Bitmap(Dimensions.Width, Dimensions.Height)) { var rand = new Random(); for (int i = 0; i < 100; i++) { bmp.SetPixel(rand.Next(Dimensions.Width), rand.Next(Dimensions.Height), (Color) rand.Next(0xFFFFFF)); bmp.Flush(); } } Pass = true; } catch (Exception e) { UnexpectedException(e); } }
public override void Run() { try { using (var scratch = new Bitmap(64, 32)) { for (int x = 0; x < scratch.Width; x++) { for (int y = 0; y < scratch.Height; y++) { Color color = ColorUtility.ColorFromRGB((byte) (x*255/scratch.Width), (byte) (y*255/(scratch.Height)), (byte) (255 - x*255/scratch.Width)); scratch.SetPixel(x, y, color); } } using (var bmp = new Bitmap(Dimensions.Width, Dimensions.Height)) { var rand = new Random(); for (int i = 0; i < 100; i++) { bmp.DrawImage(rand.Next(Dimensions.Width), rand.Next(Dimensions.Height), scratch, 0, 0, scratch.Width, scratch.Height); bmp.Flush(); } } } Pass = true; } catch (Exception e) { UnexpectedException(e); } }
public override void Run() { int focus = 15; using (var bmp = new Bitmap(Dimensions.Width, Dimensions.Height)) { DateTime barier = DateTime.Now.AddSeconds(5); while(DateTime.Now < barier) { bmp.Clear(); foreach (var star in _stars) { int x = star.X*focus/star.Z + Dimensions.Width/2; int y = Dimensions.Height/2 - star.Y*focus/star.Z; if (x >= 0 && y >= 0 && x <= bmp.Width && y <= bmp.Height) { if (star.Z > 20) bmp.SetPixel(x, y, Color.White); else { bmp.SetPixel(x, y, Color.White); bmp.SetPixel(x - 1, y, Color.White); bmp.SetPixel(x + 1, y, Color.White); bmp.SetPixel(x, y - 1, Color.White); bmp.SetPixel(x, y + 1, Color.White); } } star.Fly(); } bmp.Flush(); Thread.Sleep(5); } } }
/// <summary> /// This method fills an area which surrounded by line(s). Not only polygon but also circle, pentagram, cloud shaped... any figure. Only you need to fill is set Bitmap, Color, certain X and Y to argument. Both X and Y should be exist inner line(s). /// </summary> /// <param name="screen"></param> /// <param name="color"></param> public void FillArea(Bitmap screen, Color color, Point point) { int currentY = 0; int minY = 0; int maxY = 0; //screen.SetPixel(x, y, color); currentY = point.Y - 1; while (currentY >= 0 && screen.GetPixel(point.X, currentY) != color) { screen.SetPixel(point.X, currentY, color); currentY--; } minY = currentY + 1; currentY = point.Y; while (currentY < screen.Height && screen.GetPixel(point.X, currentY) != color) { screen.SetPixel(point.X, currentY, color); currentY++; } maxY = currentY - 1; for (int i = minY; i < maxY; i++) { if (point.X > 0 && screen.GetPixel(point.X - 1, i) != color) { FillArea(screen, color, new Point(point.X - 1, i)); } if (point.X < screen.Width - 1 && screen.GetPixel(point.X + 1, i) != color) { FillArea(screen, color, new Point(point.X + 1, i)); } } }
/// <summary> /// Draws polygon from list of points and fill it with specififed pattern. /// </summary> /// <param name="screen">Bitmap to draw on</param> /// <param name="points">Array of points defining polygon</param> /// <param name="borderColor">Color of poly border</param> /// <param name="borderWidth">Poly border width</param> /// <param name="fillColor">Color to use to fill poly</param> /// <param name="polyFill">Fill patern. Empty will simply draw unfilled poly</param> public void DrawPoly(Bitmap screen, Point[] points, Color borderColor, short borderWidth, Color fillColor, PolyFill polyFill) { if (points.Length < 3) return; // we need at least 3 point for poly if (borderWidth < 1) borderWidth = 1; Color fgColor, bgColor; if (fillColor == Color.Black) { fgColor = Color.Black; bgColor = Color.White; } else { fgColor = Color.White; bgColor = Color.Black; } if (polyFill == PolyFill.POLYFILL_EMPTY) _DrawUnfilledPoly(screen, points, borderColor, borderWidth, new Point(0, 0)); else { Point br = new Point(0, 0), tl = new Point(screen.Width, screen.Height); // find bounding box for (int i = 0; i < points.Length; ++i) { tl.X = (tl.X > points[i].X) ? points[i].X : tl.X; tl.Y = (tl.Y > points[i].Y) ? points[i].Y : tl.Y; br.X = (br.X < points[i].X) ? points[i].X : br.X; br.Y = (br.Y < points[i].Y) ? points[i].Y : br.X; } // adjust binding box to fit thick border. Foê some reason SPOT.Bitmap double the border width (at least on emulator) if (borderWidth > 1) { tl.X = (short)((tl.X > borderWidth) ? tl.X - borderWidth * 2 : 0); tl.Y = (short)((tl.Y > borderWidth) ? tl.Y - borderWidth * 2 : 0); br.X = (short)((br.X + borderWidth < (screen.Width - 1)) ? br.X + borderWidth * 1.5 : 0); br.Y = (short)((br.Y + borderWidth < (screen.Width - 1)) ? br.Y + borderWidth * 1.5 : 0); } // we need separate bitmap to draw poly as one specififed can have some drawing already and we won't be able to detect border properly Bitmap buffer = new Bitmap((br.X - tl.X + 1), (br.Y - tl.Y + 1)); // fill bitmap with color opposite to border color if (borderColor == Color.Black) buffer.DrawRectangle(Color.White, 0, 0, 0, buffer.Width, buffer.Height, 0, 0, Color.White, 0, 0, Color.White, buffer.Width, buffer.Height, 0xFF); else buffer.DrawRectangle(Color.Black, 0, 0, 0, buffer.Width, buffer.Height, 0, 0, Color.Black, 0, 0, Color.Black, buffer.Width, buffer.Height, 0xFF); _DrawUnfilledPoly(buffer, points, borderColor, borderWidth, tl); // scan created bitmap bool isInside = false, onBorder = false; int startX = -1, borderStart = -1; for (int y = 0; y < buffer.Height; ++y) { isInside = false; onBorder = false; startX = -1; borderStart = -1; for (int x = 0; x < buffer.Width; ++x) { if (buffer.GetPixel(x, y) == borderColor) { if (onBorder) { // still on border screen.SetPixel(x + tl.X, y + tl.Y, borderColor); } else { // we have reached border from inside/outside if (isInside) { //isInside = false; if (startX >= 0) { // draw pattern line for (int k = startX; k < x; ++k) { if (polyFill == PolyFill.POLYFILL_SOLID) screen.SetPixel(k + tl.X, y + tl.Y, fgColor); else if (polyFill == PolyFill.POLYFILL_HORIZONTAL) screen.SetPixel(k + tl.X, y + tl.Y, (polyfill_horizontal[k % 3 + 3 * (y % 3)] == 0xFF) ? fgColor : bgColor); else if (polyFill == PolyFill.POLYFILL_HORIZONTAL) screen.SetPixel(k + tl.X, y + tl.Y, (polyfill_horizontal[k % 3 + 3 * (y % 3)] == 0xFF) ? fgColor : bgColor); else if (polyFill == PolyFill.POLYFILL_VERTICAL) screen.SetPixel(k + tl.X, y + tl.Y, (polyfill_vertical[k % 3 + 3 * (y % 3)] == 0xFF) ? fgColor : bgColor); else if (polyFill == PolyFill.POLYFILL_DOTS) screen.SetPixel(k + tl.X, y + tl.Y, (polyfill_dots[k % 3 + 3 * (y % 3)] == 0xFF) ? fgColor : bgColor); else if (polyFill == PolyFill.POLYFILL_GRID) screen.SetPixel(k + tl.X, y + tl.Y, (polyfill_grid[k % 3 + 3 * (y % 3)] == 0xFF) ? fgColor : bgColor); else if (polyFill == PolyFill.POLYFILL_CROSS_LEFT) screen.SetPixel(k + tl.X, y + tl.Y, (polyfill_cross_left[k % 3 + 3 * (y % 3)] == 0xFF) ? fgColor : bgColor); else if (polyFill == PolyFill.POLYFILL_CROSS_RIGHT) screen.SetPixel(k + tl.X, y + tl.Y, (polyfill_cross_right[k % 3 + 3 * (y % 3)] == 0xFF) ? fgColor : bgColor); } startX = -1; } } onBorder = true; borderStart = x; screen.SetPixel(x + tl.X, y + tl.Y, borderColor); } } else { if (onBorder) { // end of border if (!((x - borderStart) > borderWidth * 4)) // if long this is not border, this is poly edge - we are still on the same side isInside = !isInside; onBorder = false; borderStart = -1; if (isInside) startX = x; } } } } buffer.Dispose(); } }
public override void Run() { try { Bitmap scratch = new Bitmap( 64, 32 ); for(int x = 0; x < scratch.Width; x++) { for(int y = 0; y < scratch.Height; y++) { scratch.SetPixel( x, y, Utility.FromRGB( x * 255 / (scratch.Width), y * 255 / (scratch.Height), 255 - x * 255 / (scratch.Width) ) ); } } Bitmap bmp = new Bitmap( Dimensions.Width, Dimensions.Height ); Random rand = new Random(); for(int i = 0; i < 1000; i++) { bmp.DrawImage( rand.Next( Dimensions.Width ), rand.Next( Dimensions.Height ), scratch, 0, 0, scratch.Width, scratch.Height ); bmp.Flush(); } Pass = true; } catch(Exception e) { UnexpectedException( e ); } }
public override void Run() { try { Bitmap bmp = new Bitmap( Dimensions.Width, Dimensions.Height ); Random rand = new Random(); for(int i = 0; i < 1000; i++) { bmp.SetPixel(rand.Next(Dimensions.Width), rand.Next(Dimensions.Height), (Presentation.Media.Color)rand.Next(0xFFFFFF)); bmp.Flush(); } Pass = true; } catch(Exception e) { UnexpectedException( e ); } }
public override void Run() { try { Bitmap bmp = new Bitmap( Dimensions.Width, Dimensions.Height ); int size = System.Math.Min( Dimensions.Width, Dimensions.Height ); for(int i = 0; i < size; ++i) { bmp.SetPixel( i, i, (Microsoft.SPOT.Presentation.Media.Color)((255 - i) << 16) ); } for(int i = 0; i < size; i += 2) { bmp.SetPixel( size - i, i, (Microsoft.SPOT.Presentation.Media.Color)(i << 8) ); } bmp.Flush(); Thread.Sleep( 3000 ); Pass = true; } catch(Exception e) { UnexpectedException( e ); } }
public static void DrawLine(int x1, int y1, int x2, int y2, bool color, Bitmap bmp) { int x_s, x_e; float slope; bool useX; if (x1 == x2) { if (y1 == y2) { DrawPoint(x1, y1, color); return; } useX = false; slope = (float)(x2 - x1) / (y2 - y1); } else { slope = (float)(y2 - y1) / (x2 - x1); useX = System.Math.Abs(slope) < 1.0f; if (!useX) slope = 1.0f / slope; } float y; if (useX) //X is primary { if (x1 < x2) { x_s = x1; x_e = x2; y = y1 + 0.5f; } else { x_s = x2; x_e = x1; y = y2 + 0.5f; } for (int x = x_s; x < x_e; x++) { //DrawPoint(x, (int)y, color); bmp.SetPixel(x, (int)y, color ? Color.Black : Color.White); y += slope; } } else //Y is primary { //slope = 1 / slope; if (y1 < y2) { x_s = y1; x_e = y2; y = x1 + 0.5f; } else { x_s = y2; x_e = y1; y = x2 + 0.5f; } for (int x = x_s; x < x_e; x++) { DrawPoint((int)y, x, color); y += slope; } } }