简体   繁体   中英

How to add outlines to transparent images?

I am trying to create a feature where I could apply a stroke effect (similar to how Photoshop does it) to my images.

I have scoured the inte.net, but have not been able to find a proper solution. How would one approach create such a feature?

I want to use the alpha channel as the source for the outline. Something like this:

在此处输入图像描述

I know there are solutions that use dilate method. But to be honest, I don't really know much about that.

Is there some algorithm that can be used in creating a such outline? Would appreciate any help

A way to add outlines to transparent images is to use the Image. Clone method to create a copy of the original image and then use the Graphics.DrawImage method to draw the outline.

using (var bitmap = (Bitmap)originalImage.Clone())
using (var graphics = Graphics.FromImage(bitmap))
{
    var pen = new Pen(Color.Black, 2);
    graphics.DrawImage(originalImage, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imageAttributes);
}

The above will create an outline of width 2 and the color black.

Note : the above code assumes that you have already loaded an image into a Bitmap object called "originalImage". Also, the Graphics. The DrawImage method will keep the transparency of the image.

This is one way. HTH Pablo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM