正如我的其他答案中所述,解决此问题的更好方法是直接访问位图数据。 BitmapImage继承自BitmapSource。 BitmapSource非常适合这一点,也适用于WPF绑定。
我自己使用BitmapSource来操作直接绑定到WPF(MVVM样式)的图像。基本上我在内存中创建一个区域并将BitmapSource指向它。这允许我直接读/写像素到内存并使BitmapSource无效,以便WPF重新绘制图像。我有一个标准的“位图”对象,我用这个。直接的数据访问使其超快速。 (严重的是,没有任何问题在30fps下修改4张图像中的所有位...并没有以更高的速度尝试,因为它没有被要求。)
样例用法可在my blog上找到。但基本上你这样做:
unsafe {
byte* imgBytePtr = (byte*)myBitmap.ImageData;
Int32* imgInt32Ptr = (Int32*)myBitmap.ImageData;
int height = (int)myBitmap.BitmapSource.Height;
int width = (int)myBitmap.BitmapSource.Width;
int bpp = myBitmap.BytesPerPixel;
// Note: No need to iterate just for copy. A Marshal.Copy() at this point can copy all the bytes into a byte-array if you want.
// But the best would be if your application could do its work directly in the imgBytePtr[]-array.
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
// Get bytes into RGBA values
int bytePos = x * (width * bpp) + (y * bpp);
byte R = imgBytePtr[bytePos + 0];
byte B = imgBytePtr[bytePos + 1];
byte G = imgBytePtr[bytePos + 2];
byte A = imgBytePtr[bytePos + 3];
// Alternatively get Int32 value of color
int intPos = x * width + y;
int intColor = imgIntPtr[intPos];
// Examples of manipulating data
// Remove blue
imgBytePtr[bytePos + 1] = 0;
// Alternative remove blue by bitmask
imgIntPtr[intPos] = imgIntPtr[intPos] & 0xFF00FFFF;
}
}
}
// Now execute Invalidate() and WPF will automagically update bound picture object :)
这使得一个的BitmapSource,如果你需要的BitmapImage相反,你可以看到,如果你可以改变它的工作。
///
/// This object holds a byte array of the picture as well as a BitmapSource for WPF objects to bind to. Simply call .Invalidate() to update GUI.
///
public class Bitmap : IDisposable
{
// some ideas/code borowed from CL NUI sample CLNUIImage.cs
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpFileMappingAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool UnmapViewOfFile(IntPtr hMap);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hHandle);
private IntPtr _section = IntPtr.Zero;
public IntPtr ImageData { get; private set; }
public InteropBitmap BitmapSource { get; private set; }
public int BytesPerPixel = 3;
///
/// Initializes an empty Bitmap
///
/// Image width
/// Image height
/// Image format
public Bitmap(int width, int height, PixelFormat pixelFormat)
{
BytesPerPixel = pixelFormat.BitsPerPixel/8;
uint imageSize = (uint)width * (uint)height * (uint)BytesPerPixel;
// create memory section and map
_section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, imageSize, null);
ImageData = MapViewOfFile(_section, 0xF001F, 0, 0, imageSize);
BitmapSource = Imaging.CreateBitmapSourceFromMemorySection(_section, width, height, pixelFormat, width * BytesPerPixel, 0) as InteropBitmap;
}
///
/// Invalidates the bitmap causing a redraw
///
public void Invalidate()
{
BitmapSource.Invalidate();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
}
// free native resources if there are any.
if (ImageData != IntPtr.Zero)
{
UnmapViewOfFile(ImageData);
ImageData = IntPtr.Zero;
}
if (_section != IntPtr.Zero)
{
CloseHandle(_section);
_section = IntPtr.Zero;
}
}
}
你通常从这种方法返回什么?也许问题不在于这种方法,而是你返回的对象永远不会被抛弃。 – 2011-02-04 11:19:41
向我们展示更多代码。这还不够。 – Euphoric 2011-02-04 11:29:49
通常我返回一个流(byte [])。该功能称为不同位图的多次。 – shepard1 2011-02-04 12:18:43