在图像上绘制边框 [英] Drawing a border on an image

查看:84
本文介绍了在图像上绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试制作一个遮罩生成器,以生成图像的多边形。这是我的代码。

I'm currently trying to make a mask generator to generate polygons for images. Here's my code.

Bitmap bmp = new Bitmap(file);
int w = bmp.Width;
int h = bmp.Height;
List<Point> vertices = new List<Point>();

for (int y=0; y<h; y++)
{
    bool rowbegin = false;
    for (int x=0; x<w; x++)
    {
        Color c = bmp.GetPixel(x, y);
        if (!rowbegin)
        {
            // Check for a non alpha color
            if (c.A != Color.Transparent.A)
            {
                rowbegin = true;
                // This is the first point in the row
                vertices.Add(new Point(x, y));
            }
        } else {
            // Check for an alpha color
            if (c.A == Color.Transparent.A)
            {
                // The previous pixel is the last point in the row
                vertices.Add(new Point(x-1, y));
                rowbegin = false;
            }
        }
    }
}

// Convert to an array of points
Point[] polygon = vertices.ToArray();

Graphics g = Graphics.FromImage(bmp);

g.DrawPolygon(Pens.LawnGreen, polygon);
g.Dispose();

但是我得到的输出是错误的。

But I'm getting wrong output.

必填。

我还希望字符之间的空白为空。也是为什么DrawPolygon填充它?

What I want is also the gaps between the characters to be empty. Also why DrawPolygon is filling it?

谢谢。

推荐答案

您尝试这样的操作:希望我的代码对您有所帮助:

Do you trying something like this:i hope my code help you:

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(@"C:\Users\Ali\Desktop\1.png");
            int w = bmp.Width;
            int h = bmp.Height;
            Lst_Data lastpointcolor = new Lst_Data() ;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    Color c = bmp.GetPixel(x, y);
                    if (c.A != Color.Transparent.A)
                    {
                        if (lastpointcolor.color.A == Color.Transparent.A)
                        {
                            bmp.SetPixel(lastpointcolor.point.X, lastpointcolor.point.Y, Color.Red);
                        }
                    }
                    lastpointcolor = new Lst_Data() { point = new Point(x, y), color = bmp.GetPixel(x, y) };
                }
            }

            for (int y = h-1; y > 0; y--)
            {
                for (int x = w-1; x > 0; x--)
                {
                    Color c = bmp.GetPixel(x, y);
                    if (c.A != Color.Transparent.A)
                    {
                        if (lastpointcolor.color.A == Color.Transparent.A)
                        {
                            bmp.SetPixel(lastpointcolor.point.X, lastpointcolor.point.Y, Color.Red);
                        }
                    }
                    lastpointcolor = new Lst_Data() { point = new Point(x, y), color = bmp.GetPixel(x, y) };
                }
            }
            pictureBox1.Image = bmp;
        }
    }
    public struct Lst_Data
    {
        public Point point;
        public Color color;
    }
}

已编辑:

还有一个我为您准备的想法:D

And Another idea i have for you:D

为具有相同尺寸的原始图像设置一个蒙版,然后执行此操作:

Make a mask for original image with the same size and do this:

    Bitmap orginal = new Bitmap(@"C:\Users\Ali\Desktop\orginal.png");
    Bitmap mask = new Bitmap(@"C:\Users\Ali\Desktop\mask.png");
    for (int i = 0; i < orginal.Width; i++)
    {
        for (int j = 0; j < orginal.Height; j++)
        {
            if (orginal.GetPixel(i, j).A == Color.Transparent.A)
            {
                mask.SetPixel(i, j, Color.Transparent);
            }
        } 
    }
    Bitmap bitmap = new Bitmap(mask, mask.Height, mask.Height);
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.DrawImage(mask, 0, 0);
        g.DrawImage(orginal,0,0);
    }
    pictureBox1.Image = bitmap;

结果:

这篇关于在图像上绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆