c# - How to compare a two Cursor objects?
问题描述
I am creating an unit test and I have a Cursor
that should be made from a specific Bitmap
. The Cursor
is created using the following code:
cursor = new Cursor(bmp.GetHicon());
where bmp
is a Bitmap
.
This always maked the test fail (the handle created by bmp.GetHicon()
is always new):
Assert.AreEqual(bmp.GetHicon(), cursor.Handle);
I also thought of using Icon.FromHandle
.
解决方案
事实上,您正在寻找如何比较两个游标。
你有位图,你有一个光标,你想检查光标是否是使用位图创建的。为此,作为一种选择,从位图创建一个游标,然后进行比较,将这两个游标(从位图创建的新游标和现有游标)保存到流中并比较流。
例如:
bool ArEqual(Cursor cur1, Cursor cur2)
{
byte[] bytes1, bytes2;
using (var ico = Icon.FromHandle(cur1.Handle))
using (var fs = new MemoryStream())
{
ico.Save(fs);
bytes1 = fs.ToArray();
}
using (var ico = Icon.FromHandle(cur2.Handle))
using (var fs = new MemoryStream())
{
ico.Save(fs);
bytes2 = fs.ToArray();
}
return bytes1.SequenceEqual(bytes2);
}
推荐阅读
- asp.net-core - Kubernetes 上带有 Pgbouncer 的 Npgsql - 高延迟
- python - Django按计算值排序
- node.js - 在应用程序的客户端/服务器端存储 JWT/授权 [JavaScript]
- matomo - 将分析数据从 Piwik 导入到新服务器上的 Matomo
- python - ipyvuetify:如何限制 TextField 的宽度
- reactjs - 如何从 API 显示数组的数据?
- c++ - 在 C++ 中为用户定义的类型专门化 std::minus 是否合法?
- python - 从 Motionbuilder 到 Maya 的动画传输
- swiftui - SwiftUI 2,s Mapkit MKPointOfInterestFilter
- python - 如何使用 pymysql 将日志上传到 mysql