.NET 中的布尔位图运算?

发布于 2024-08-20 13:57:22 字数 96 浏览 8 评论 0原文

我有两个 8bpp 位图。我想对一个与另一个进行按位与操作,但我在 .NET 中没有看到任何明显的方法来执行此操作。是否可以在不使用非 .NET 方法的情况下执行此操作? 谢谢!

I have two 8bpp bitmaps. I want to do a bitwise AND of one to the other, but I don't see any obvious way to do this in .NET. Is it possible to do this without using non-.NET methods?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅听莫相离 2024-08-27 13:57:22

我认为您正在寻找 Bitmap.LockBits

I think you're looking for Bitmap.LockBits.

不气馁 2024-08-27 13:57:22

您可以尝试将位图转换为字节数组,然后循环遍历字节并将它们组合在一起

编辑:
对循环想法进行时间测试:

示例代码:

DateTime StartTime = DateTime.Now;
Image Image1 = Image.FromFile("C:\\Image1.bmp");
Image Image2 = Image.FromFile("C:\\Image2.bmp");
DateTime AfterLoad = DateTime.Now;
MemoryStream S = new MemoryStream();
Image1.Save(S, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] I1 = S.ToArray();
Image2.Save(S, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] I2 = S.ToArray();
DateTime AfterConvert = DateTime.Now;
DateTime AfterLoop = DateTime.Now;
if (I1.Length == I2.Length)
{
    Byte[] I3 = new Byte[I1.Length];
    for (int i = 0; i < I1.Length; i++)
        I3[i] = Convert.ToByte(I1[i] & I2[i]);
    AfterLoop = DateTime.Now;
    FileStream F = new FileStream("C:\\Users\\jamesb\\desktop\\Image3.bmp", FileMode.OpenOrCreate);
    F.Write(I3, 0, I3.Length);
    F.Close();
}
DateTime Finished = DateTime.Now;
MessageBox.Show("Load Time: " + (AfterLoad - StartTime).TotalMilliseconds.ToString() + " ms" + "\r\n" +
                "Convert Time: " + (AfterConvert - AfterLoad).TotalMilliseconds.ToString() + " ms"+ "\r\n" +
                "Loop Time: " + (AfterLoop - AfterConvert).TotalMilliseconds.ToString() + " ms"+ "\r\n" +
                "Save Time: " + (Finished - AfterLoop).TotalMilliseconds.ToString() + " ms"+ "\r\n" +
                "Total Time: " + (Finished - StartTime).TotalMilliseconds.ToString() + " ms");

结果如下:

Load Time: 30.003 ms
Convert Time: 94.0094 ms
Loop Time: 128.0128 ms
Save Time: 177.0177 ms
Total Time: 429.0429 ms

图像“Image1”和“Image2”为 4000 x 2250(从数码相机转换为 8 位 bmp)

You could try converting the Bitmap to a Byte array and then loop through the bytes anding them together

Edit :
Ran a Time Test on the loop idea:

Example Code:

DateTime StartTime = DateTime.Now;
Image Image1 = Image.FromFile("C:\\Image1.bmp");
Image Image2 = Image.FromFile("C:\\Image2.bmp");
DateTime AfterLoad = DateTime.Now;
MemoryStream S = new MemoryStream();
Image1.Save(S, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] I1 = S.ToArray();
Image2.Save(S, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] I2 = S.ToArray();
DateTime AfterConvert = DateTime.Now;
DateTime AfterLoop = DateTime.Now;
if (I1.Length == I2.Length)
{
    Byte[] I3 = new Byte[I1.Length];
    for (int i = 0; i < I1.Length; i++)
        I3[i] = Convert.ToByte(I1[i] & I2[i]);
    AfterLoop = DateTime.Now;
    FileStream F = new FileStream("C:\\Users\\jamesb\\desktop\\Image3.bmp", FileMode.OpenOrCreate);
    F.Write(I3, 0, I3.Length);
    F.Close();
}
DateTime Finished = DateTime.Now;
MessageBox.Show("Load Time: " + (AfterLoad - StartTime).TotalMilliseconds.ToString() + " ms" + "\r\n" +
                "Convert Time: " + (AfterConvert - AfterLoad).TotalMilliseconds.ToString() + " ms"+ "\r\n" +
                "Loop Time: " + (AfterLoop - AfterConvert).TotalMilliseconds.ToString() + " ms"+ "\r\n" +
                "Save Time: " + (Finished - AfterLoop).TotalMilliseconds.ToString() + " ms"+ "\r\n" +
                "Total Time: " + (Finished - StartTime).TotalMilliseconds.ToString() + " ms");

with the following results :

Load Time: 30.003 ms
Convert Time: 94.0094 ms
Loop Time: 128.0128 ms
Save Time: 177.0177 ms
Total Time: 429.0429 ms

The images "Image1" and "Image2" were 4000 x 2250 (From a digital camera converted to 8 bit bmp)

烟火散人牵绊 2024-08-27 13:57:22

如果性能不重要,请使用 Bitmap.GetPixel 和 Bitmap.SetPixel

If performance is not important, use Bitmap.GetPixel and Bitmap.SetPixel

作业与我同在 2024-08-27 13:57:22

您可以使用“BitBlt”函数,您可以在其中 AND 源和目标 (SRCAND),pinvoke 签名为 此处

这里有一篇关于 Codeproject 的文章,它使用 BitBlt 包装器此处

希望这有帮助,
此致,
汤姆.

You can use the 'BitBlt' function, in which you can AND the source and destination (SRCAND), the pinvoke signature is here.

Here is an article on Codeproject that uses a BitBlt wrapper here.

Hope this helps,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文