从透明表单上的图像中删除轮廓 [英] Remove outline from image on transparent form
问题描述
我正在尝试将窗口形式转换为透明形式,并使其仅显示一个对象.但是它在我的对象周围仍然有一条线(笔划),并不是我想要的那样完美.如何取出线(行程)?(附有图片进行比较.)
I'm trying to transfer window form into transparent, and make it show just an object. But it still has a line (stroke) around my object, it's not really perfect as I wanted. How can I take out the line (stroke)? (Attached a picture to compare.)
这是我的代码:
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Width = this.pictureBox1.Width;
this.Height = this.pictureBox1.Height;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Black;
this.TransparencyKey = this.pictureBox1.BackColor;
}
推荐答案
您的图像具有半透明像素. TransparencyKey
将仅使一种颜色透明.因此,边界线像素将显示图像颜色和 Parent
控件或表单的颜色的混合.
Your image has semi-transparent pixels. TransparencyKey
will only make one color transparent. So the borderline pixels will show a mix of the image color and the color of the Parent
control or form..
这是通过使所有半透明像素完全透明来消除它们的功能:
Here is a function that eliminates all semi-transparent pixels by making them fully transparent:
using System.Runtime.InteropServices;
..
public static void UnSemi(Bitmap bmp)
{
Size s = bmp.Size;
PixelFormat fmt = bmp.PixelFormat;
Rectangle rect = new Rectangle(Point.Empty, s);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, fmt);
int size1 = bmpData.Stride * bmpData.Height;
byte[] data = new byte[size1];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size1);
for (int y = 0; y < s.Height; y++)
{
for (int x = 0; x < s.Width; x++)
{
int index = y * bmpData.Stride + x * 4;
// alpha, threshold = 255
data[index + 3] = (data[index + 3] < 255) ? (byte)0 : (byte)255;
}
}
System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
bmp.UnlockBits(bmpData);
}
请注意,这也意味着反锯齿的外观会变得有些粗糙.
Note that this also means that the nice anti-aliased look will get somewhat rough instead..
还请注意,该例程采用32位ARGB像素格式,就像 PNG
通常那样.
Also note that the routine assumes a 32 bit ARGB pixel-format, as PNGs
will usually have.
最后请注意,由于图像的很多为 Black
,因此您应该选择其他颜色.紫红色
在野外很少见,但在龙的世界中可能并非如此,您想要选择一些随机的颜色.
Finally note that since the image has a lot of Black
you should pick a different color. Fuchsia
is rather rare in the wild, but maybe not in the world of dragons and you want to pick some random color..
也:您要设置 pictureBox1.BackColor = Color.Transparent
..
最后:有时在函数签名中添加一个 threshold
参数以设置一个级别,从该级别可以完全打开或关闭alpha.
Finally: Sometimes it makes sense to add a threshold
parameter to the function signature to set a level from which to turn alpha all on or off..
这是一个用法示例:
this.BackColor = Color.FromArgb(1,2,3,4);
this.TransparencyKey = this.BackColor;
UnSemi((Bitmap)this.pictureBox1.Image);
这篇关于从透明表单上的图像中删除轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!