private void button1_Click(object sender, EventArgs e) { using (Bitmap bmp1 = new Bitmap(400, 500)) using (LockBmp bmplock = bmp1.Lock()) { BitmapBuffer wb = bmplock.CreateNewBitmapBuffer(); //lines int y = 0; wb.DrawLine(0, y, 100, y + 100, BitmapBufferEx.ColorInt.FromArgb(255, 255, 0, 0)); //red wb.DrawLine(0, y + 100, 100, y + 0, BitmapBufferEx.ColorInt.FromArgb(255, 0, 0, 255)); //blue #if DEBUG wb.DrawLineAa(100, y, 200, y + 100, BitmapBufferEx.ColorInt.FromArgb(255, 255, 0, 0)); wb.DrawLineAa(100, y + 100, 200, y + 0, BitmapBufferEx.ColorInt.FromArgb(255, 0, 0, 255)); //blue #endif //---------- y += 150; wb.DrawLineDDA(0, y, 100, y + 100, BitmapBufferEx.ColorInt.FromArgb(255, 255, 0, 0)); //red wb.DrawLineDDA(0, y + 100, 100, y + 0, BitmapBufferEx.ColorInt.FromArgb(255, 0, 0, 255)); //blue wb.DrawEllipse(200, 0, 300, 100, BitmapBufferEx.ColorInt.FromArgb(255, 255, 0, 0)); // bmplock.WriteAndUnlock(); bmp1.Save("d:\\WImageTest\\a0002.png"); } }
private void button1_Click(object sender, EventArgs e) { using (Bitmap bmp1 = new Bitmap(400, 500)) using (var bmplock = bmp1.Lock()) { BitmapBuffer wb = bmplock.GetWritableBitmap(); //lines int y = 0; wb.DrawLine(0, y, 100, y + 100, PixelFarm.DrawingBuffer.ColorInt.FromArgb(255, 255, 0, 0)); //red wb.DrawLine(0, y + 100, 100, y + 0, PixelFarm.DrawingBuffer.ColorInt.FromArgb(255, 0, 0, 255)); //blue wb.DrawLineAa(100, y, 200, y + 100, PixelFarm.DrawingBuffer.ColorInt.FromArgb(255, 255, 0, 0)); wb.DrawLineAa(100, y + 100, 200, y + 0, PixelFarm.DrawingBuffer.ColorInt.FromArgb(255, 0, 0, 255)); //blue //---------- y += 150; wb.DrawLineDDA(0, y, 100, y + 100, PixelFarm.DrawingBuffer.ColorInt.FromArgb(255, 255, 0, 0)); //red wb.DrawLineDDA(0, y + 100, 100, y + 0, PixelFarm.DrawingBuffer.ColorInt.FromArgb(255, 0, 0, 255)); //blue wb.DrawEllipse(200, 0, 300, 100, PixelFarm.DrawingBuffer.ColorInt.FromArgb(255, 255, 0, 0)); // bmplock.WriteAndUnlock(); bmp1.Save("d:\\WImageTest\\a0002.png"); } }
/// <summary> /// Draws the different types of shapes. /// </summary> private void DrawStaticShapes(BitmapBuffer writeableBmp) { // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block using (writeableBmp.GetBitmapContext()) { // Init some size vars int w = writeableBmp.PixelWidth - 2; int h = writeableBmp.PixelHeight - 2; int w3rd = w / 3; int h3rd = h / 3; int w6th = w3rd >> 1; int h6th = h3rd >> 1; // Clear writeableBmp.Clear(); // Draw some points for (int i = 0; i < 200; i++) { writeableBmp.SetPixel(rand.Next(w3rd), rand.Next(h3rd), GetRandomColor()); } // Draw Standard shapes writeableBmp.DrawLine(rand.Next(w3rd, w3rd * 2), rand.Next(h3rd), rand.Next(w3rd, w3rd * 2), rand.Next(h3rd), GetRandomColor()); writeableBmp.DrawTriangle(rand.Next(w3rd * 2, w - w6th), rand.Next(h6th), rand.Next(w3rd * 2, w), rand.Next(h6th, h3rd), rand.Next(w - w6th, w), rand.Next(h3rd), GetRandomColor()); writeableBmp.DrawQuad(rand.Next(0, w6th), rand.Next(h3rd, h3rd + h6th), rand.Next(w6th, w3rd), rand.Next(h3rd, h3rd + h6th), rand.Next(w6th, w3rd), rand.Next(h3rd + h6th, 2 * h3rd), rand.Next(0, w6th), rand.Next(h3rd + h6th, 2 * h3rd), GetRandomColor()); writeableBmp.DrawRectangle(rand.Next(w3rd, w3rd + w6th), rand.Next(h3rd, h3rd + h6th), rand.Next(w3rd + w6th, w3rd * 2), rand.Next(h3rd + h6th, 2 * h3rd), GetRandomColor()); // Random polyline int[] p = new int[rand.Next(7, 10) * 2]; for (int j = 0; j < p.Length; j += 2) { p[j] = rand.Next(w3rd * 2, w); p[j + 1] = rand.Next(h3rd, 2 * h3rd); } writeableBmp.DrawPolyline(p, GetRandomColor()); // Random closed polyline p = new int[rand.Next(6, 9) * 2]; for (int j = 0; j < p.Length - 2; j += 2) { p[j] = rand.Next(w3rd); p[j + 1] = rand.Next(2 * h3rd, h); } p[p.Length - 2] = p[0]; p[p.Length - 1] = p[1]; writeableBmp.DrawPolyline(p, GetRandomColor()); // Ellipses writeableBmp.DrawEllipse(rand.Next(w3rd, w3rd + w6th), rand.Next(h3rd * 2, h - h6th), rand.Next(w3rd + w6th, w3rd * 2), rand.Next(h - h6th, h), GetRandomColor()); writeableBmp.DrawEllipseCentered(w - w6th, h - h6th, w6th >> 1, h6th >> 1, GetRandomColor()); // Draw Grid writeableBmp.DrawLine(0, h3rd, w, h3rd, Colors.Black); writeableBmp.DrawLine(0, 2 * h3rd, w, 2 * h3rd, Colors.Black); writeableBmp.DrawLine(w3rd, 0, w3rd, h, Colors.Black); writeableBmp.DrawLine(2 * w3rd, 0, 2 * w3rd, h, Colors.Black); // Invalidates on exit of using block } }
/// <summary> /// Draws the different types of shapes. /// </summary> private void DrawStaticShapes(BitmapBuffer writeableBmp) { // HideShapeCountText(); // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block using (writeableBmp.GetBitmapContext()) { // Init some size vars int w = writeableBmp.PixelWidth; int h = writeableBmp.PixelHeight; int w3 = w / 3; int h3 = h / 3; int w6 = w3 >> 1; int h6 = h3 >> 1; int w12 = w6 >> 1; int h12 = h6 >> 1; // Clear writeableBmp.Clear(); // Fill closed concave polygon var p = new int[] { w12 >> 1, h12, w6, h3 - (h12 >> 1), w3 - (w12 >> 1), h12, w6 + w12, h12, w6, h6 + h12, w12, h12, w12 >> 1, h12, }; writeableBmp.FillPolygonsEvenOdd(new[] { p }, GetRandomColor()); // Fill closed convex polygon p = new int[] { w3 + w6, h12 >> 1, w3 + w6 + w12, h12, w3 + w6 + w12, h6 + h12, w3 + w6, h6 + h12 + (h12 >> 1), w3 + w12, h6 + h12, w3 + w12, h12, w3 + w6, h12 >> 1, }; writeableBmp.FillPolygon(p, GetRandomColor()); // Fill Triangle + Quad writeableBmp.FillTriangle(2 * w3 + w6, h12 >> 1, 2 * w3 + w6 + w12, h6 + h12, 2 * w3 + w12, h6 + h12, GetRandomColor()); writeableBmp.FillQuad(w6, h3 + (h12 >> 1), w6 + w12, h3 + h6, w6, h3 + h6 + h12 + (h12 >> 1), w12, h3 + h6, GetRandomColor()); // Fill Ellipses writeableBmp.FillEllipse(rand.Next(w3, w3 + w6), rand.Next(h3, h3 + h6), rand.Next(w3 + w6, 2 * w3), rand.Next(h3 + h6, 2 * h3), GetRandomColor()); writeableBmp.FillEllipseCentered(2 * w3 + w6, h3 + h6, w12, h12, GetRandomColor()); // Fill closed Cardinal Spline curve p = new int[] { w12 >> 1, 2 * h3 + h12, w6, h - (h12 >> 1), w3 - (w12 >> 1), 2 * h3 + h12, w6 + w12, 2 * h3 + h12, w6, 2 * h3 + (h12 >> 1), w12, 2 * h3 + h12, }; writeableBmp.FillCurveClosed(p, 0.5f, GetRandomColor()); // Fill closed Beziér curve p = new int[] { w3 + w12, 2 * h3 + h6 + h12, w3 + w6 + (w12 >> 1), 2 * h3, w3 + w6 + w12 + (w12 >> 1), 2 * h3, w3 + w6 + w12, 2 * h3 + h6 + h12, }; writeableBmp.FillBeziers(p, GetRandomColor()); // Fill Rectangle writeableBmp.FillRectangle(rand.Next(2 * w3, 2 * w3 + w6), rand.Next(2 * h3, 2 * h3 + h6), rand.Next(2 * w3 + w6, w), rand.Next(2 * h3 + h6, h), GetRandomColor()); // Fill another rectangle with alpha blending writeableBmp.FillRectangle(rand.Next(2 * w3, 2 * w3 + w6), rand.Next(2 * h3, 2 * h3 + h6), rand.Next(2 * w3 + w6, w), rand.Next(2 * h3 + h6, h), GetRandomColor(), true); BitmapBufferEx.ColorInt black = BitmapBufferEx.ColorInt.FromArgb(255, 0, 0, 0); // Draw Grid writeableBmp.DrawLine(0, h3, w, h3, Colors.Black); writeableBmp.DrawLine(0, 2 * h3, w, 2 * h3, Colors.Black); writeableBmp.DrawLine(w3, 0, w3, h, Colors.Black); writeableBmp.DrawLine(2 * w3, 0, 2 * w3, h, Colors.Black); // Invalidates on exit of Using block } }