分析InkCanvas保存的图片是否为全部白色

本文介绍了一种方法,用于从InkCanvas组件保存图片,并分析该图片中每个像素的颜色来判断签名区域是否全部为白色。通过创建临时文件并利用WriteableBitmap进行像素级检查,实现了对特定颜色痕迹的有效识别。

1. 从InkCanvas保存图片到Pictures下面

2. 读取保存的图片分析每个像素是否全为白色

(由于擦除的痕迹为白色,可能会有不是FFFFFF的痕迹存在,故RGB均大于200以上的点视为白色)

public async Task<bool> CheckIfSignatureAllWhiteEx(InkStrokeContainer container)
        {
            bool isPixWhite = true;
            try
            {
                int originalPixelWidth = (int)inkCanvas.ActualWidth;
                int originalPixelHeight = (int)inkCanvas.ActualHeight;
                StorageFolder storageFolder = KnownFolders.PicturesLibrary;
                StorageFile imageFile = await storageFolder.CreateFileAsync("test1.png",
                        CreationCollisionOption.ReplaceExisting);
                using (IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await container.SaveAsync(stream);
                }

                IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read);
                WriteableBitmap bitmap = new WriteableBitmap(originalPixelWidth, originalPixelHeight);
                await bitmap.SetSourceAsync(fileStream);

                Color col = new Color();
                DataReader DR = DataReader.FromBuffer(bitmap.PixelBuffer);
                byte[] bytes = new byte[bitmap.PixelBuffer.Capacity];
                DR.ReadBytes(bytes);

                for (int x = 0; x < bitmap.PixelWidth; x++)
                {
                    for (int y = 0; y < bitmap.PixelHeight; y++)
                    {
                        col.B = bytes[(y * bitmap.PixelWidth + x) * 4];
                        col.G = bytes[(y * bitmap.PixelWidth + x) * 4 + 1];
                        col.R = bytes[(y * bitmap.PixelWidth + x) * 4 + 2];
                        col.A = bytes[(y * bitmap.PixelWidth + x) * 4 + 3];

                        if(col.A!=0 && (col.R<200 || col.G < 200 || col.B < 200))
                        {
                            isPixWhite = false;
                            return isPixWhite;
                        }
                    }
                }

                return isPixWhite;
            }
            catch (Exception e)
            {
                
                return false;
            }
        }

 

转载于:https://www.cnblogs.com/kunkka/p/6736885.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值