-
概述(Overview)
在网上搜了很多,都不太满意,找ChatGPT一问,效率慢,内存占用大,但是可以用:
1.图片是什么形状,控件就是什么形状;
2.穿透多层控件实现路径外的全透明/镂空。
(I searched a lot on the Internet, but I was not satisfied, and I asked ChatGPT, which was slow and had a large memory, but it could be used: 1. What is the shape of the picture, what shape is the control; 2. Penetrate multi-layer controls to achieve full transparency/hollowing out of the path.)
-
获取控件的Region(Code)
-
/// <summary> /// 根据图片计算Region路径(低效率——更高效率请使用指针/内存法处理,引用之处有具体的代码,可自行点击跳转查看) /// </summary> /// <param name="image">图像资源</param> public Region ImageToRegion(Image image) { if (image == null) { return null; } GraphicsPath path = new GraphicsPath(); Bitmap bitmap = new Bitmap(image); // 扫描图片的每个像素,将不透明部分添加到GraphicsPath中 for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { if (bitmap.GetPixel(x, y).A > 0) // 如果像素不透明 { // 将像素坐标添加到GraphicsPath中 path.AddRectangle(new Rectangle(x, y, 1, 1)); } } } bitmap.Dispose(); return new Region(path); }
-
处理控件的Region(Code)
public SharpControl() { InitializeComponent(); //图片是什么形状,控件就是什么形状 this.Region = ImageToRegion(this.BackgroundImage); }
-
引用(Reference)