这是我的代码,任何有兴趣的人。可能有一个更优化的解决方案,但这是可行的。我正在创建一个白色的整个页面,并在我通过它时复制新页面中的选定区域。感谢 Bob Powell 的 SetIndexedPixel 例程。
protected int GetIndexedPixel(int x, int y, BitmapData bmd)
{
var index = y * bmd.Stride + (x >> 3);
var p = Marshal.ReadByte(bmd.Scan0, index);
var mask = (byte)(0x80 >> (x & 0x7));
return p &= mask;
}
protected void SetIndexedPixel(int x, int y, BitmapData bmd, bool pixel)
{
int index = y * bmd.Stride + (x >> 3);
byte p = Marshal.ReadByte(bmd.Scan0, index);
byte mask = (byte)(0x80 >> (x & 0x7));
if (pixel)
p &= (byte)(mask ^ 0xff);
else
p |= mask;
Marshal.WriteByte(bmd.Scan0, index, p);
}
public DocAppImage CutToNew(int left, int top, int width, int height, int pageWidth, int pageHeight)
{
var destBmp = new Bitmap(pageWidth, pageHeight, BitmapImage.PixelFormat);
var pageRect = new Rectangle(0, 0, pageWidth, pageHeight);
var pageData = destBmp.LockBits(pageRect, ImageLockMode.WriteOnly, BitmapImage.PixelFormat);
var croppedRect = new Rectangle(left, top, width, height);
var croppedSource = BitmapImage.LockBits(croppedRect, ImageLockMode.ReadWrite, BitmapImage.PixelFormat);
for (var y = 0; y < pageHeight; y++)
for (var x = 0; x < pageWidth; x++)
{
if (y >= top && y <= top + height && x >= left && x <= width + left)
{
SetIndexedPixel(x, y, pageData,
GetIndexedPixel(x - left, y - top, croppedSource) == 0 ? true : false);
SetIndexedPixel(x - left, y - top, croppedSource, false); //Blank area in original
}
else
SetIndexedPixel(x, y, pageData, false); //Fill the remainder of the page with white.
}
destBmp.UnlockBits(pageData);
var retVal = new DocAppImage { BitmapImage = destBmp };
destBmp.Dispose();
BitmapImage.UnlockBits(croppedSource);
SaveBitmapToFileImage(BitmapImage);
return retVal;
}