我使用以下代码锁定位图的矩形区域
Recangle rect = new rect(X,Y,width,height);
BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly,
bitmap.PixelFormat);问题似乎是bitmapData.Scan0给了我矩形左上角的IntPtr。当我使用memcpy时,它将内存中的连续区域复制到指定的长度。
memcpy(bitmapdest.Scan0, bitmapData.Scan0,
new UIntPtr((uint (rect.Width*rect.Height*3)));如果以下是我的位图数据,
a b c d e
f g h i j
k l m n o
p q r s t如果矩形是(2, 1, 3 ,3),即区域
g h i
l m n
q r s使用memcpy给出具有以下区域的位图
g h i
j k l
m n o我能理解为什么它复制连续的记忆区域。底线是我想要使用Lockbits复制一个矩形区域。
编辑:我用了Bitmap.Clone,
using (Bitmap bitmap= (Bitmap)Image.FromFile(@"Data\Edge.bmp"))
{
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
Rectangle cropRect = new Rectangle(new Point(i * croppedWidth, 0),new Size(croppedWidth, _original.Height));
_croppedBitmap= bitmap.Clone(cropRect, bitmap.PixelFormat);
}但是当我翻转Y (小于500ms)时,速度更快了。
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);但是当我没有翻转Y (30秒)时,速度非常慢。
所使用的图像大小为60000x1500。
发布于 2015-05-05 20:44:48
别真的明白你的问题是什么。以下代码将正确的位图区域复制到托管数组中(当然,对于24 255位图,alpha总是255):
int x = 1;
int y = 1;
int w = 2;
int h = 2;
Bitmap bmp = new Bitmap(@"path\to\bitmap.bmp");
Rectangle rect = new Rectangle(x, y, w, h);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
IntPtr ptr = bmpData.Scan0;
int bytes = 4 * w * h;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
bmp.UnlockBits(bmpData);https://stackoverflow.com/questions/29766955
复制相似问题