// 锁住数据.
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData bitmapData =
bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bitmap.PixelFormat);
IntPtr ptr = bitmapData.Scan0;
int bytes = bitmap.Width * bitmap.Height * 3;
byte[] rgbValues = new byte[bytes]; // 遍历数据.
//bitmap.Palette.Entries[y * bitmapData.Stride + x] //这个是灰度图像的定位方法
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
byte bColorIndex = *(bitmapData.Scan0 + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
}
bitmap.UnlockBits(bitmapData);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData bitmapData =
bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bitmap.PixelFormat);
IntPtr ptr = bitmapData.Scan0;
int bytes = bitmap.Width * bitmap.Height * 3;
byte[] rgbValues = new byte[bytes]; // 遍历数据.
//bitmap.Palette.Entries[y * bitmapData.Stride + x] //这个是灰度图像的定位方法
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
byte bColorIndex = *(bitmapData.Scan0 + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
}
bitmap.UnlockBits(bitmapData);
解决方案 »
- 一键获取所有TextBlock控件的Name属性
- AxWindowsMediaPlayer控件有FileName属性吗?
- DataGridView填充数据时候出现"输入字符串的格式不正确"
- 求解C#中类与派生类实例化后的内存结构图
- 高手请进:当asp.net接受一个请求的时候,不直接去找文件,而由程序截获下来。
- C#如何监控桌面新打开窗口!
- c#中连接数据库的问题
- 下面代码有错吗?请各位大侠来看看!!!!!
- EXCEL打开导入datagrid的问题
- 未处理的“System.ArgumentException”类型的异常.大家帮帮忙!
- xml修改存在一模一样相同元素问题,这样这样永远是第一条数据
- HTML 源代码转换成Xml格式的文档
{ // 锁住数据.
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData bitmapData =
bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr ptr = bitmapData.Scan0;
int bytes = bitmap.Width * bitmap.Height * 3;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // 遍历数据.
//bitmap.Palette.Entries[y * bitmapData.Stride + x] //这个是灰度图像的定位方法
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
byte bColorIndex = rgbValues[y * bitmapData.Stride + x];
Color currentColor = bitmap.Palette.Entries[bColorIndex]; }
}
bitmap.UnlockBits(bitmapData);
}
System.Drawing.Imaging.BitmapData bitmapData =
bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bitmap.PixelFormat);
byte* p = bitmapData.Scan0; for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
//假设图像的像素位是1个字节,8位,且使用了调色板
byte bColorIndex = *(p + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
}
bitmap.UnlockBits(bitmapData);这样写试试
private unsafe void FindLines()
{
Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
bitmap = ScreenHelper.GetGrayBmp(bitmap);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
byte* p = (byte*)bitmapData.Scan0; for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
//假设图像的像素位是1个字节,8位,且使用了调色板
byte bColorIndex = *(p + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
} bitmap.UnlockBits(bitmapData);
}
{
Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
bitmap = ScreenHelper.GetGrayBmp(bitmap);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height );
BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height];
Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length);
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color currentColor = ?? }
} bitmap.UnlockBits(bitmapData);
}
{
Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
bitmap = ScreenHelper.GetGrayBmp(bitmap);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height );
BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height];
Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length); byte* p = (byte*)bitmapData.Scan0;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
//假设图像的像素位是1个字节,8位,且使用了调色板
byte bColorIndex = *(p + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
} bitmap.UnlockBits(bitmapData);
}
{
Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
//查下看这个地方有没有问题
bitmap = ScreenHelper.GetGrayBmp(bitmap);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height );
BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat); //去掉这两句就行了
//byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height];
//Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length); byte* p = (byte*)bitmapData.Scan0;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
//假设图像的像素位是1个字节,8位,且使用了调色板
byte bColorIndex = *(p + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
} bitmap.UnlockBits(bitmapData);
}
{
Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
//看看这里有没有问题,如果PixFormat不对也会出现问题
bitmap = ScreenHelper.GetGrayBmp(bitmap);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height );
BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat); //这两句没用了,去掉去
//byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height];
//Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length); byte* p = (byte*)bitmapData.Scan0;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
//假设图像的像素位是1个字节,8位,且使用了调色板
byte bColorIndex = *(p + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
} bitmap.UnlockBits(bitmapData);
}用这个方法试试看吧
private unsafe void FindLines()
{
Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
//查下看这个地方有没有问题
bitmap = ScreenHelper.GetGrayBmp(bitmap);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle bitmapRect = new Rectangle(0, 0, rect.Width, rect.Height);
BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
byte* p = (byte*)bitmapData.Scan0;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
byte bColorIndex = *(p + y * bitmapData.Stride + x);
Color currentColor = bitmap.Palette.Entries[bColorIndex];
}
} bitmap.UnlockBits(bitmapData); }
{
Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
bitmap = ScreenHelper.GetGrayBmp(bitmap);
Color[] colors = bitmap.Palette.Entries;
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle bitmapRect = new Rectangle(0, 0, rect.Width, rect.Height);
BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat); byte* p = (byte*)bitmapData.Scan0;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
byte bColorIndex = *(p + y * bitmapData.Stride + x);
Color currentColor = colors[bColorIndex];
}
} bitmap.UnlockBits(bitmapData);
bitmap.Dispose();
}这样写经调试正常运行