打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# 检测PNG图片透明区域
将png decode为32位bitmap然后遍历像素

C# code
public Rect GetTransparentBounds(BitmapSource source) {    var width = source.PixelWidth;    var height = source.PixelHeight;    var pixelBytes = new byte[height * width * 4];    source.CopyPixels(pixelBytes, width * 4, 0);    double? ax1 = null, ax2 = null, ay1 = null, ay2 = null;    Rect result = new Rect();    for (int y = 0; y < height; y++) {        for (int x = 0; x < width; x++) {            byte a = pixelBytes[(y * width + x) * 4 + 3];            if (a != 0xff) {                if (ax1.HasValue) {                    ax1 = Math.Min(ax1.Value, x);                    ax2 = Math.Max(ax2.Value, x);                } else {                    ax1 = x;                    ax2 = x;                }                if (ay1.HasValue) {                    ay1 = Math.Min(ay1.Value, y);                    ay2 = Math.Max(ay2.Value, y);                } else {                    ay1 = y;                    ay2 = y;                }            }        };    }    if(ax1.HasValue && ay1.HasValue){        result.X = ax1.Value;        result.Y = ay1.Value;        result.Width = ax2.Value - ax1.Value;        result.Height = ay2.Value - ay1.Value;    }    return result;} 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ZXing 生成或读取二维码(解决中文乱码的问题) - 在键盘上跳舞 - ITeye技术网站
可空类型Nullable到底是什么鬼
int?
【EXCEL】很多人一直想要却得不到的功能,根据姓名自动插入照片
亚像素
游戏开发-碰撞
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服