C# 从 System.Drawing.Bitmap 高效获取像素数据
问题说明
我在 HDD 上有几个 (~2GB) 原始 24bpp RGB 文件.现在我想检索它的一部分并将其缩放到所需的大小.
(唯一允许的比例是 1, 1/2, 1/4, 1/8, ..., 1/256)
I have several (~2GB) raw 24bpp RGB files on HDD. Now I want to retrieve a portion of it and scale it to the desired size.
(The only scales allowed are 1, 1/2, 1/4, 1/8, ..., 1/256)
所以我目前正在将感兴趣的矩形中的每一行读取到一个数组中,这给我留下了一个高度正确但宽度错误的位图.
So I'm currently reading every line from the rectangle of interest into an array, which leaves me with a bitmap which has correct height but wrong width.
下一步,我将从新创建的数组中创建一个位图.
这是通过使用指针完成的,因此不涉及数据复制.
接下来我在 Bitmap 上调用 GetThumbnailImage,它会创建一个具有正确尺寸的新位图.
As the next step I'm creating a Bitmap from the newly created array.
This is done with by using a pointer so there is no copying of data involved.
Next I'm calling GetThumbnailImage on the Bitmap, which creates a new bitmap with the correct dimensions.
现在我想返回新创建的位图的原始像素数据(作为字节数组).但是为了实现这一点,我目前正在使用 LockBits 将数据复制到一个新数组中.
Now I want to return the raw pixel data (as a byte array) of the newly created bitmap. But to achieve that I'm currently copying the data using LockBits into a new array.
所以我的问题是:有没有办法在不复制的情况下将像素数据从位图中获取到字节数组中?
类似于:
var bitmapData = scaledBitmap.LockBits(...)
byte[] rawBitmapData = (byte[])bitmapData.Scan0.ToPointer()
scaledBitmap.UnlockBits(bitmapData)
return rawBitmapData
我很清楚这行不通,这只是我想要实现的目标的一个例子.
I'm well aware that this doesn't work, it is just an example to what I basically want to achieve.
正确答案
我认为这是您最好的选择.
I think this is your best bet.
var bitmapData = scaledBitmap.LockBits(...);
var length = bitmapData.Stride * bitmapData.Height;
byte[] bytes = new byte[length];
// Copy bitmap to byte[]
Marshal.Copy(bitmapData.Scan0, bytes, 0, length);
scaledBitmap.UnlockBits(bitmapData);
如果你想传递一个字节[],你必须复制它.
You have to copy it, if you want a pass around a byte[].
您不必删除已分配的字节,只需在完成后处理原始 Bitmap 对象,因为它实现了 IDisposable.
You don't have to delete the bytes that were allocated, you just need to Dispose of the original Bitmap object when done as it implements IDisposable.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangicbah
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20