- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近偶然发现了这个关于在 C# 中将位图格式转换为 24 位 RGB 的问题的答案:Faster copying of images to change their PixelFormat ,其中所选答案说明如下:
Another way is to lock the bits without the alpha channel and then copy the memory to a new bitmap
以下代码作为示例提供(为简洁起见进行了简化):
public static Bitmap RemoveAlphaChannel(Bitmap bitmap) {
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
Bitmap bitmapDest = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb);
BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData dataDest = bitmapDest.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
int bitmapSize = data.Stride * data.Height;
Buffer.MemoryCopy(dataDest.Scan0, data.Scan0, bitmapSize, bitmapSize);
bitmap.UnlockBits(data);
bitmapDest.UnlockBits(dataDest);
return bitmapDest;
}
让我们使用此函数将 32 位 ARGB 图像转换为 24 位 RGB。我有以下问题:
最佳答案
这只是 GDI+ 提供的便利。重要的是,使用与您要应用的特定算法配合良好的格式访问像素通常更加方便和快捷。
32bppPArgb 格式最适合尽快将位图渲染到屏幕上,它与视频帧缓冲区格式兼容,因此无需转换。典型的渲染速度比任何其他像素格式快 10 倍。但是 PArgb 在代码中操作起来非常笨拙。 R、G 和 B channel 值被 alpha 值校正,您必须再次将其除以恢复原始 RGB 值。要求 Argb 格式一下子解决了这个问题。
同样,24bppRgb 格式很尴尬,您必须使用 byte*
来访问像素 channel 。这需要每个像素 3 次内存访问,大大降低了代码速度。请求 32bppArgb 允许使用 int*
,速度更快,并且允许您忽略步幅。
这些转换没有什么特别复杂的。但它们不是是免费的,GDI+ 必须完成分配临时存储和来回转换像素值的工作。我在我的 pokey 笔记本电脑上使用 1000 x 1000 位图和 ImageLockMode.ReadWrite 对其进行了分析:
32bppArgb => 32bppArgb : 0.002 msec
32bppPArgb => 32bppArgb : 5.6 msec
32bppArgb => 24bppRgb : 5.6 msec
32bppPArgb => 24bppRgb : 5.6 msec
我测量了您的 RemoveAlphaChannel() 方法的性能,使用 memcpy() 在同一个 32bppArgb 1000 x 1000 位图上进行复制。对于所需的单个 ImageLockMode.ReadOnly 像素转换,我得到了 2.8 毫秒,对于副本,我得到了 2.8 毫秒。 GDI+ 提供的 Graphics.DrawImage() 方法的速度大约是它的两倍,耗时 9.3 毫秒。
关于c# - 位图的 LockBits() 作为 C# 中的不同格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42735499/
我有一些代码有错误“AccessViolationException 未被用户代码处理:试图读取或写入 protected 内存...” 违规函数的精简版如下: protected override
我有一个方法需要尽可能快,它使用不安全的内存指针,这是我第一次尝试这种类型的编码,所以我知道它可能会更快。 /// /// Copies bitmapdata from one bi
使用 GetPixel 和 SetPixel 很简单,但速度很慢,所以我尝试使用 LockBits。 我很久以前就用过这种方法来比较两张图片: public static Bitmap FastCom
首先,我会指出我将接受 C# 或 VB.NET 解决方案。 我正在尝试重构这段旧代码,以避免使用 GetPixel/SetPixel 方法的坏习惯和性能低下: Public Function Cha
按照 Bob Powell 关于 LockBits 的教程,我将以下代码放入 C# 2010 Visual Studio Express 中: System.Drawing.Imaging.Bitma
我正在尝试使用 Lockbits 写出灰度图像,我当前的代码看起来是 /// /// Save the content of the FrameProc out to a bitmap /// p
我正在使用以下代码来锁定位图的矩形区域 Recangle rect = new rect(X,Y,width,height); BitmapData bitmapData = bitmap.LockB
我在将一些代码从 Bitmap.GetPixel 更改为使用 LockBits 返回的直接像素缓冲区时遇到问题。与 GetPixel 相比,LockBits 返回的数据似乎确实给了我不同的颜色值。 这
我在 Android 上的程序使用的算法使用了很多 setPixel 和 getPixel,因此,它非常慢。在 .NET 上,我可以使用 LockBits 使其更快。 Java 或 Android 上
MSDN 引用:[1] http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx#Y1178 从链接中可以看出,第一个参数将“指定要锁定的位图部分”,
我正在处理由摄像机拍摄的 10 兆像素图像。 目的是在矩阵(二维数组)中注册每个像素的灰度值。 我第一次使用 GetPixel 但花了 25 秒才完成。现在我使用 Lockbits,但仍然需要 10
在部署应用程序之前,我使用 XP 虚拟机和 Vista 虚拟机对应用程序进行冒烟测试。这两个虚拟机都使用 32 位颜色。不确定这是否有什么区别,但我正在使用 VirtualBox。每台机器还分配有 2
我想使用 LockBits 方法更快地比较相似图像,如下所示 using System; using System.Drawing; using System.Drawing.Imaging; usi
我最近经常使用锁定位图,并且不断收到“试图访问无效内存”错误。这主要是因为位图已在内存中移动。有些人使用 GCHandle.Alloc() 在 CLR 中分配内存并固定它。 Bitmap.LockBi
我做了一个实现边缘检测算法的程序,但需要很长时间来处理。我读过有关使用锁位和不安全状态而不是 getpixel 和 setpixel 的信息,但我仍然不明白如何使用它。 这是我的示例代码: priva
使用 MS Visual Studio 2013,具有以下代码: using System; using System.Collections.Generic; using System.Linq;
我正在从一张 1bpp 索引图像剪切并粘贴到一张新图像。 一切正常,直到起始像素是 8 的除数。在下面的代码中,步幅等于相对于矩形宽度的值,直到我达到字节边界。那么步幅等于整个页面的宽度。 var c
我的程序是用 C# 编写的,在低级别上使用位图进行操作。一切正常,但有时(很少见,但稳定)出现“通用 GDI+ 异常”异常,并且很难重现这种情况。 函数 LockBits() 和 UnLockBits
我最近经常使用锁定位图,并且不断收到“试图访问无效内存”错误。这主要是因为位图已在内存中移动。有些人使用 GCHandle.Alloc() 在 CLR 中分配内存并固定它。 Bitmap.LockBi
编辑:非常感谢您的回复。我在这里最需要的是示例代码,用于说明我对嵌套循环中的几行代码所做的工作,因为这在 GetPixel/SetPixel 中是有效的,但也是我无法使用 Lockbits 正确工作的
我是一名优秀的程序员,十分优秀!