如何使用LockBits复制Bitmap的rectangluar部分? [英] How do I copy a rectangluar portion of Bitmap using LockBits ?
本文介绍了如何使用LockBits复制Bitmap的rectangluar部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码。
BitmapData bitmapData = bitmap.LockBits( new rect (X,Y,宽度,高度),
ImageLockMode.ReadOnly,bitmap.PixelFormat);
似乎问题是bitmapData.Scan0给了我 IntPtr $ c矩形左上角的$ c>。当我使用memcpy复制时,它会将内存中的连续区域复制到指定的长度。
memcpy(bitmapdest.Scan0,bitmapData。 Scan0, new UIntPtr (( uint ) (rect.Width * rect.Height * 3)));
如果以下是我的位图数据,
abcde< br />
fghi j< br />
klmn o< br />
pqrst< br />
如果需要矩形部分(2,1,3,3)
即该区域
< br />
gh i< br />
lm n< br />
qr s< br />
使用memcpy给我以下区域的位图
< br />
gh i< br />
jk l< ; br />
mn o< br />
我如何仍然使用Lockbits并实现rectangluar区域复制?
解决方案
ÿ为此,您不必使用功能强大的LockBits
。使用System.Drawing.Graphics.DrawImage
方法之一:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs .110%29.aspx [ ^ ]。
仅使用未指定目标<的方法/ i>大小,副本的大小;它将保证您按原样复制图像,而不会重新采样。
以防万一:请不要告诉我这不是你想要的,因为你不会渲染这个图像。这是你想要的,因为你可以在任何媒体上绘制图像副本:屏幕,打印机,另一个图像。要使用此工厂函数获取Graphics
的实例以在位图上绘图: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage( v = vs.110).aspx [ ^ ]。
-SA
I'm using the following code.
BitmapData bitmapData = bitmap.LockBits(new rect(X,Y,width,height),
ImageLockMode.ReadOnly, bitmap.PixelFormat);
What seems to be the issue is bitmapData.Scan0 gives me IntPtr
of the top left corner of the rectangle. When I copy using memcpy, it copies the contiguous region in memory upto the specified length.
memcpy(bitmapdest.Scan0, bitmapData.Scan0, new UIntPtr((uint) (rect.Width*rect.Height*3)));
If following is my bitmap data,
a b c d e <br /> f g h i j<br /> k l m n o<br /> p q r s t <br />
and if the rectangle portion is required (2, 1, 3 ,3)
i.e, the region
<br /> g h i<br /> l m n<br /> q r s<br />
using memcpy gives me bitmap with the following region
<br /> g h i<br /> j k l<br /> m n o<br />
How can I still use Lockbits and achieve rectangluar region copy?
解决方案
You don't have to use powerfulLockBits
for this simple purpose. Use one ofSystem.Drawing.Graphics.DrawImage
methods:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].
Use only the method which don't specify the target size, the size of the copy; it will guarantee that you are copying the image as is, without re-sampling it.
Just in case: please don't tell me that this is not what you want because you are not going to render this image. It is what you want, because you can draw the image copy on any media: screen, printer, another image. To get the instance of theGraphics
to draw on the bitmap using this factory function: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage(v=vs.110).aspx[^].
—SA
这篇关于如何使用LockBits复制Bitmap的rectangluar部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文