C# Bitmap GetPixel 效率太低,太慢的替代方法
生活随笔
收集整理的這篇文章主要介紹了
C# Bitmap GetPixel 效率太低,太慢的替代方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新建類
public class DirectBitmap : IDisposable
{
public Bitmap Bitmap { get; private set; }
int[] Bits = null;
public int Height { get; private set; }
public int Width { get; private set; }
protected GCHandle BitsHandle { get; private set; }
public DirectBitmap(int width, int height)
{
Width = width;
Height = height;
Bits = new int[width * height];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
public void SetPixel(int x, int y, Color colour)
{
SetPixelRGB(x, y, colour.ToArgb());
}
public void SetPixelRGB(int x, int y, int colour)
{
Bits[x + (y * Width)] = colour;
}
public Color GetPixel(int x, int y)
{
return Color.FromArgb(GetPixelRGB(x, y));
}
public int GetPixelRGB(int x, int y)
{
return Bits[x + (y * Width)];
}
private bool Disposed = false;
public void Dispose()
{
if (Disposed)
return;
Disposed = true;
Bitmap.Dispose();
BitsHandle.Free();
}
}
使用時:
聲明DirectBitmap實例,訪問DirectBitmap的Bitmap即可。 使用DirectBitmap的GetPixel方法來獲取顏色
DirectBitmap image = new DirectBitmap(rc.Right - rc.Left, rc.Bottom - rc.Top);
using (Graphics gp = Graphics.FromImage(image.Bitmap))
{
IntPtr dc = gp.GetHdc();
FormHelper.PrintWindow(handle, dc, 0);
gp.ReleaseHdc();
}
總結
以上是生活随笔為你收集整理的C# Bitmap GetPixel 效率太低,太慢的替代方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Angular开发模式下的编译器和运行时
- 下一篇: Angular应用的入口