/// <summary> /// 指针扫描bmp by skep99 /// </summary> unsafe void dealbmp() { //lock GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = new Rectangle((int)boundsF.X, (int)boundsF.Y, (int)boundsF.Width, (int)boundsF.Height); width = (int)boundsF.Width * sizeof(PixelData); if (width % 4 != 0) { width = 4 * (width / 4 + 1); } bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); pBase = (Byte*)bitmapData.Scan0.ToPointer(); //处理 Point size = PixelSize; for (int x = 0; x < size.X; x++) { for (int y = 0; y < size.Y; y++) { PixelData* pPixel = PixelAt(x, y); int value = (pPixel->red + pPixel->green + pPixel->blue) / 3; pPixel->red = (byte)value; pPixel->green = (byte)value; pPixel->blue = (byte)value; } } //unlock bitmap.UnlockBits(bitmapData); bitmapData = null; pBase = null; }
using System.IO; using System.Drawing; Bitmap image = new Bitmap(); MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.ToArray();
/// 指针扫描bmp by skep99
/// </summary>
unsafe void dealbmp()
{
//lock
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = bitmap.GetBounds(ref unit);
Rectangle bounds = new Rectangle((int)boundsF.X,
(int)boundsF.Y,
(int)boundsF.Width,
(int)boundsF.Height); width = (int)boundsF.Width * sizeof(PixelData);
if (width % 4 != 0)
{
width = 4 * (width / 4 + 1);
} bitmapData =
bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); pBase = (Byte*)bitmapData.Scan0.ToPointer(); //处理
Point size = PixelSize; for (int x = 0; x < size.X; x++)
{
for (int y = 0; y < size.Y; y++)
{
PixelData* pPixel = PixelAt(x, y); int value = (pPixel->red + pPixel->green + pPixel->blue) / 3;
pPixel->red = (byte)value;
pPixel->green = (byte)value;
pPixel->blue = (byte)value;
}
} //unlock
bitmap.UnlockBits(bitmapData);
bitmapData = null;
pBase = null;
}
using System.Drawing;
Bitmap image = new Bitmap();
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.ToArray();
不放用:
unsafe
{
fixed *xxx = address;
...
}
能详细说明如何使用指针直接访问吗?
我的代码下面的语句需要在循环中遍历short数组,如下:
for (int y_value = 0; y_value < BitmapHeight; y_value++)
{
xyValue = y_value * bmp_width + x_value;
if (rgbValues[xyValue] == 0)
{
TempBuffer[j] |= OrBytes[i];
if ((TempBuffer[j] == 0xfd) || (TempBuffer[j] == 0xfe) || (TempBuffer[j] == 0xff))
{
TempBuffer[j + 1] = (byte)(TempBuffer[j] ^ 0x20);
TempBuffer[j] = 0xfd;
j++;
}
}
i++;
if (i == 8)
{
j++;
i = 0;
}
}最好能直接告诉我如何换成直接内存方式。不胜感激。
for (int y_value = 0; y_value < BitmapHeight; y_value++)
{
xyValue = y_value * bmp_width + x_value;
if (rgbValues[xyValue] == 0)
{
TempBuffer[j] |= OrBytes[i];
if ((TempBuffer[j] == 0xfd) || (TempBuffer[j] == 0xfe) || (TempBuffer[j] == 0xff))
{
TempBuffer[j + 1] = (byte)(TempBuffer[j] ^ 0x20);
TempBuffer[j] = 0xfd;
j++;
}
}
i++;
if (i == 8)
{
j++;
i = 0;
}
}