Example #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            MemoryBitmap mbp    = new MemoryBitmap(new Bitmap(pictureBox1.Image));
            MemoryBitmap output = new MemoryBitmap(new Bitmap(pictureBox1.Image));

            int[] xModel = new int[] { 1, 1, 1, 0, 0, 0, -1, -1, -1 };
            int[] yModel = new int[] { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
            mbp.Gray();

            for (int i = 0; i < mbp.Height; i++)
            {
                for (int j = 0; j < mbp.Width; j++)
                {
                    int index = 0;
                    int Gx = 0, Gy = 0;
                    for (int m = -1; m < 2; m++)
                    {
                        for (int n = -1; n < 2; n++)
                        {
                            int vx = j + n >= mbp.Width ? j * 2 - mbp.Width : j + n;
                            int vy = i + m >= mbp.Height ? i * 2 - mbp.Height : i + m;


                            if (vx < 0)
                            {
                                vx *= -1;
                            }
                            if (vy < 0)
                            {
                                vy *= -1;
                            }

                            int val = mbp.GetPixel(vx, vy).R;
                            Gx += val * xModel[index];
                            Gy += val * yModel[index];
                            index++;
                        }
                    }
                    Gx = Math.Abs(Gx);
                    Gy = Math.Abs(Gy);
                    int p = Math.Max(Gx, Gy);
                    output.SetPixel(j, i, p, p, p);
                }
            }
            output.SaveMemory();
            pictureBox2.Image = output.bmp;
        }
Example #2
0
        /// <summary>
        /// Draws the control in normal or pushed states
        /// </summary>
        /// <param name="e">Paitn event</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            var attributes = new ImageAttributes();

            using (var g = Graphics.FromImage(MemoryBitmap))
            {
                if (Pushed)
                {
                    using (var pen = new Pen(Theme.ThemeBase))
                        GraphicsExtensions.DrawThemedGradientRectangle(g, pen, ClientRectangle, Dpi.ScaleSize(new Size(8, 8)));
                }
                else
                {
                    g.Clear(Parent.BackColor);
                }

                var textSize = g.MeasureString(Text, Font);
                var textArea = new RectangleF((ClientSize.Width - textSize.Width) / 2,
                                              (ClientSize.Height - textSize.Height),
                                              textSize.Width,
                                              textSize.Height);

                if (Image != null)
                {
                    var imageWidth  = Dpi.Scale(Image.Width);
                    var imageHeight = Dpi.Scale(Image.Height);
                    var imageArea   = new Rectangle((ClientSize.Width - imageWidth) / 2,
                                                    (ClientSize.Height - imageHeight) / 2,
                                                    imageWidth,
                                                    imageHeight);

                    var key = Image.GetPixel(0, 0);
                    attributes.SetColorKey(key, key);

                    g.DrawImage(Image,
                                StretchImage ? ClientRectangle : imageArea,
                                0, 0, Image.Width, Image.Height,
                                GraphicsUnit.Pixel,
                                attributes);
                }

                using (var brush = new SolidBrush(ForeColor))
                    g.DrawString(Text, Font, brush, textArea);

                if (Pushed)
                {
                    var key = MemoryBitmap.GetPixel(0, 0);
                    attributes.SetColorKey(key, key);
                }
                else
                {
                    attributes.ClearColorKey();
                }

                e.Graphics.DrawImage(MemoryBitmap,
                                     ClientRectangle,
                                     0, 0, MemoryBitmap.Width, MemoryBitmap.Height,
                                     GraphicsUnit.Pixel,
                                     attributes);
            }
        }