示例#1
0
        private void Draw(BitmapBuffer writeableBmp)
        {
            if (this.points != null)
            {
                ReloadRandomPoints();
                // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
                using (writeableBmp.GetBitmapContext())
                {
                    writeableBmp.Clear();
                    DrawPoints(writeableBmp);
                    //DrawBeziers(writeableBmp);
                    //  DrawCardinal(writeableBmp);

                    //if (ChkShowPoints.IsChecked.Value)
                    //{
                    //    DrawPoints();
                    //}
                    //if (RBBezier.IsChecked.Value)
                    //{
                    //    DrawBeziers();
                    //}
                    //else if (RBCardinal.IsChecked.Value)
                    //{
                    //    DrawCardinal();
                    //}
                }
            }
        }
示例#2
0
        /// <summary>
        /// Draws random shapes.
        /// </summary>
        private void DrawShapes(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 w2 = w >> 1;
                int h2 = h >> 1;

                // Clear
                writeableBmp.Clear();

                // Fill Shapes
                for (int i = 0; i < shapeCount; i++)
                {
                    // Random polygon
                    int[] p = new int[rand.Next(5, 10) * 2];
                    for (int j = 0; j < p.Length; j += 2)
                    {
                        p[j]     = rand.Next(w);
                        p[j + 1] = rand.Next(h);
                    }
                    writeableBmp.FillPolygon(p, GetRandomColor());
                }

                // Invalidates on exit of Using block
            }
        }
示例#3
0
文件: Form1.cs 项目: BiDuc/PixelFarm
        private void button3_Click(object sender, EventArgs e)
        {
            using (Bitmap src = new Bitmap("d:\\WImageTest\\L01.png"))
                using (LockBmp srcLock = src.Lock())
                    using (Bitmap dest = new Bitmap(400, 500))
                        using (LockBmp dstLock = dest.Lock())
                        {
                            BitmapBuffer dstWb = dstLock.CreateNewBitmapBuffer();
                            BitmapBuffer srcWb = srcLock.CreateNewBitmapBuffer();
                            int          y     = 0;
                            dstWb.Clear(BitmapBufferEx.ColorInt.FromArgb(255, 255, 255, 255));

                            dstWb.Blit(new RectD(10, 10, src.Width, src.Height),
                                       srcWb,
                                       new RectD(0, 0, src.Width, src.Height), BitmapBufferExtensions.BlendMode.None
                                       );
                            dstWb.Blit(new RectD(100, 100, src.Width * 2, src.Height * 2),
                                       srcWb,
                                       new RectD(0, 0, src.Width, src.Height), BitmapBufferExtensions.BlendMode.None
                                       );

                            dstLock.WriteAndUnlock();
                            dest.Save("d:\\WImageTest\\a0004.png");
                        }
        }
示例#4
0
        /// <summary>
        /// Draws circles that decrease in size to build a flower that is animated
        /// </summary>
        private void DrawEllipsesFlower(BitmapBuffer writeableBmp)
        {
            // Init some size vars
            int w = writeableBmp.PixelWidth - 2;
            int h = writeableBmp.PixelHeight - 2;

            // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
            using (writeableBmp.GetBitmapContext())
            {
                // Increment frame counter
                if (++frameCounter >= int.MaxValue || frameCounter < 1)
                {
                    frameCounter = 1;
                }
                double s = Math.Sin(frameCounter * 0.01);
                if (s < 0)
                {
                    s *= -1;
                }

                // Clear
                writeableBmp.Clear();

                // Draw center circle
                int xc = w >> 1;
                int yc = h >> 1;
                // Animate base size with sine
                int r0 = (int)((w + h) * 0.07 * s) + 10;

                BitmapBufferEx.ColorInt color_brown = BitmapBufferEx.ColorInt.FromArgb(
                    255, System.Drawing.Color.Brown.R, System.Drawing.Color.Brown.G, System.Drawing.Color.Brown.B);

                writeableBmp.DrawEllipseCentered(xc, yc, r0, r0, color_brown);

                // Draw outer circles
                int dec    = (int)((w + h) * 0.0045f);
                int r      = (int)((w + h) * 0.025f);
                int offset = r0 + r;
                for (int i = 1; i < 6 && r > 1; i++)
                {
                    for (double f = 1; f < 7; f += 0.7)
                    {
                        // Calc postion based on unit circle
                        int xc2 = (int)(Math.Sin(frameCounter * 0.002 * i + f) * offset + xc);
                        int yc2 = (int)(Math.Cos(frameCounter * 0.002 * i + f) * offset + yc);
                        int col = (int)(0xFFFF0000 | (uint)(0x1A * i) << 8 | (uint)(0x20 * f));
                        writeableBmp.DrawEllipseCentered(xc2, yc2, r, r, col);
                    }
                    // Next ring
                    offset += r;
                    r      -= dec;
                    offset += r;
                }

                // Invalidates on exit of using block
            }
        }
示例#5
0
        public void Draw(BitmapBuffer writeableBmp)
        {
            // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
            using (writeableBmp.GetBitmapContext())
            {
                writeableBmp.Clear();
                Draw(writeableBmp, this.Root);
#if SILVERLIGHT
                writeableBmp.Invalidate();
#endif
            }
        }
示例#6
0
        private void button6_Click(object sender, EventArgs e)
        {
            BitmapBuffer unmodifiedBmp = LoadBitmapAsReadonly("../../02.jpg");
            BitmapBuffer flipImg       = unmodifiedBmp.RotateFree(20, false);


            using (LockBmp bmplock = destBmp.Lock())
            {
                BitmapBuffer wb = bmplock.CreateNewBitmapBuffer();
                wb.Clear(Colors.White);

                wb.Blit(new RectD(0, 0, flipImg.PixelWidth, flipImg.PixelHeight),
                        flipImg,
                        new RectD(0, 0, flipImg.PixelWidth, flipImg.PixelHeight));

                bmplock.WriteAndUnlock();

                g.Clear(System.Drawing.Color.White);
                g.DrawImage(destBmp, 0, 0);
            }
        }
示例#7
0
        private void button4_Click(object sender, EventArgs e)
        {
            BitmapBuffer unmodifiedBmp = LoadBitmapAsReadonly("../../02.jpg");
            BitmapBuffer rotateBmp     = unmodifiedBmp.Rotate(BitmapBufferExtensions.FastRotateAngle.Rotate270);


            using (LockBmp bmplock = destBmp.Lock())
            {
                BitmapBuffer wb = bmplock.CreateNewBitmapBuffer();
                wb.Clear(Colors.White);

                wb.Blit(new RectD(0, 0, rotateBmp.PixelWidth, rotateBmp.PixelHeight),
                        rotateBmp,
                        new RectD(0, 0, rotateBmp.PixelWidth, rotateBmp.PixelHeight));

                bmplock.WriteAndUnlock();

                g.Clear(System.Drawing.Color.White);
                g.DrawImage(destBmp, 0, 0);
            }
        }
示例#8
0
        private void button2_Click(object sender, EventArgs e)
        {
            BitmapBuffer unmodifiedBmp = LoadBitmapAsReadonly("../../02.jpg");
            BitmapBuffer sticker       = LoadBitmapAsReadonly("../../01.jpg");

            BitmapBuffer overlayResult = Overlay(unmodifiedBmp, sticker, new BitmapBufferEx.PointD(10, 10));

            using (LockBmp bmplock = destBmp.Lock())
            {
                BitmapBuffer wb = bmplock.CreateNewBitmapBuffer();
                wb.Clear(Colors.Black);

                wb.Blit(new RectD(0, 0, overlayResult.PixelWidth, overlayResult.PixelHeight),
                        overlayResult,
                        new RectD(0, 0, overlayResult.PixelWidth, overlayResult.PixelHeight));

                bmplock.WriteAndUnlock();

                g.Clear(System.Drawing.Color.White);
                g.DrawImage(destBmp, 0, 0);
            }
        }
示例#9
0
        void UpdateRenderFrame()
        {
            // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
            // NOTE: This is not strictly necessary for the SL version as this is a WPF feature, however we include it here for completeness and to show
            // a similar API to WPF
            //render!
            using (LockBmp bmplock = destBmp.Lock())
            {
                BitmapBuffer wb = bmplock.CreateNewBitmapBuffer();
                emitter.TargetBitmap   = wb;
                emitter.ParticleBitmap = particleBmp;

                wb.Clear(Colors.Black);


                double elapsed = (DateTime.Now - lastUpdate).TotalSeconds;
                lastUpdate = DateTime.Now;
                emitter.Update(elapsed);
                //			bmp.Blit(new Point(100, 150), circleBmp, new Rect(0, 0, 200, 200), Colors.Red, BlendMode.Additive);
                //			bmp.Blit(new Point(160, 55), circleBmp, new Rect(0, 0, 200, 200), Color.FromArgb(255, 0, 255, 0), BlendMode.Additive);
                //			bmp.Blit(new Point(220, 150), circleBmp, new Rect(0, 0, 200, 200), Colors.Blue, BlendMode.Additive);

                //double timeNow = _stopwatch.ElapsedMilliseconds;
                //double elapsedMilliseconds = timeNow - _lastTime;
                //_lowestFrameTime = Math.Min(_lowestFrameTime, elapsedMilliseconds);
                //// FpsCounter.Text = string.Format("FPS: {0:0.0} / Max: {1:0.0}", 1000.0 / elapsedMilliseconds, 1000.0 / _lowestFrameTime);
                //_lastTime = timeNow;



                //
                bmplock.WriteAndUnlock();
                //

                g.Clear(System.Drawing.Color.White);
                g.DrawImage(destBmp, 0, 0);
            }
        }
示例#10
0
        /// <summary>
        /// Draws random ellipses
        /// </summary>
        private void DrawEllipses(BitmapBuffer writeableBmp)
        {
            // Init some size vars
            int w  = writeableBmp.PixelWidth - 2;
            int h  = writeableBmp.PixelHeight - 2;
            int wh = w >> 1;
            int hh = h >> 1;

            // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
            using (writeableBmp.GetBitmapContext())
            {
                // Clear
                writeableBmp.Clear();

                // Draw Ellipses
                for (int i = 0; i < shapeCount; i++)
                {
                    writeableBmp.DrawEllipse(rand.Next(wh), rand.Next(hh), rand.Next(wh, w), rand.Next(hh, h),
                                             GetRandomColor());
                }

                // Invalidates on exit of using block
            }
        }
示例#11
0
        /// <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
            }
        }
示例#12
0
        /// <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
            }
        }
示例#13
0
        private void DrawFillDemo(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 w2 = w >> 1;
                int h2 = h >> 1;
                int w4 = w2 >> 1;
                int h4 = h2 >> 1;
                int w8 = w4 >> 1;
                int h8 = h4 >> 1;

                // Clear
                writeableBmp.Clear();

                // Add circles
                const float startTimeFixed  = 1;
                const float endTimeFixed    = startTimeFixed + timeStep;
                const float startTimeRandom = 3;
                const float endTimeCurve    = 9.7f;
                const int   intervalRandom  = 2;
                const int   maxCircles      = 30;

                // Spread fixed position and color circles
                if (time > startTimeFixed && time < endTimeFixed)
                {
                    unchecked
                    {
                        circles.Add(new Circle {
                            X = w8, Y = h8, Radius = 10f, Velocity = 1, Color = (int)0xFFC88717
                        });
                        circles.Add(new Circle
                        {
                            X = w8, Y = h - h8, Radius = 10f, Velocity = 1, Color = (int)0xFFFB522B
                        });
                        circles.Add(new Circle
                        {
                            X = w - w8, Y = h8, Radius = 10f, Velocity = 1, Color = (int)0xFFDB6126
                        });
                        circles.Add(new Circle
                        {
                            X = w - w8, Y = h - h8, Radius = 10f, Velocity = 1, Color = (int)0xFFFFCE25
                        });
                    }
                }

                // Spread random position and color circles
                if (time > startTimeRandom && (int)time % intervalRandom == 0)
                {
                    unchecked
                    {
                        circles.Add(new Circle
                        {
                            X        = rand.Next(w),
                            Y        = rand.Next(h),
                            Radius   = 1f,
                            Velocity = rand.Next(1, 5),
                            Color    = rand.Next((int)0xFFFF0000, (int)0xFFFFFFFF),
                        });
                    }
                }

                // Render and update circles
                foreach (var circle in circles)
                {
                    var r = (int)circle.Radius;
                    writeableBmp.FillEllipseCentered(circle.X, circle.Y, r, r, circle.Color);
                    circle.Update();
                }

                if (circles.Count > maxCircles)
                {
                    circles.RemoveAt(0);
                }

                // Fill closed Cardinal Spline curve
                if (time < endTimeCurve)
                {
                    var p = new int[]
                    {
                        w4, h2,
                        w2, h2 + h4,
                        w2 + w4, h2,
                        w2, h4,
                    };
                    writeableBmp.FillCurveClosed(p, (float)Math.Sin(time) * 7, Colors.Black);
                }

                // Update time
                time += timeStep;

                // Invalidates on exit of Using block
            }
        }