图像8位数据读取使用.NET
来源:互联网 发布:cacti的linux镜像 编辑:程序博客网 时间:2024/06/06 07:35
This article will cover some of the basics on how to access 8 bit greyscale or indexed images, by accessing the bitmapdata directly in memory. This also has the benefit of being much faster than the Set/GetPixel methods provided by the .NET Framework.
Before we can access the memory directly, we must lock its place in memory. We can do this by calling the Bitmap.LockBits() method:
BitmapData bmd = myBitmap.LockBits(new Rectangle(0, 0, myBitmap.Width, myBitmap.Height), ImageLockMode.ReadWrite, myBitmap.PixelFormat);
Likewise when we are done using the BitmapData, remember to unlock the data:
myBitmap.UnlockBits(bmd);
Now we need a method that can access the BitmapData. Lets make our own SetPixel and GetPixel method. Here we assume that we are dealing with8bit pixels. We also add the 'unsafe' keyword since direct memory access isn't thread safe. I won't cover the Stride and Scan0 values. Bob Powell has a nice article on this.
public unsafe void SetPixel(int x, int y, byte c){byte* p = (byte *)bmd.Scan0.ToPointer();int offset=y*bmd.Stride+(x);p[offset] = c;}public unsafe Byte GetPixel(int x, int y){byte* p = (byte *)bmd.Scan0.ToPointer();int offset=y*bmd.Stride+x;return p[offset];}
It is worth noting that GetPixel only returns a byte and not a color. The byte represents a number between 0 and 255. Each of the values is actually an index to a color palette. The palette could specify that for instance index 0 is black, index 1 is red, index 3 is blue etc. If you want a greyscale image, we can override the color palette. Let's set index 0 to black, index 255 to white, and linearly distribute the grayscale in between.
public static void SetGrayscalePalette(Bitmap b){ColorPalette pal = b.Palette;for(int i = 0; i < 256; i++)pal.Entries[i] = Color.FromArgb( 255, i, i, i );b.Palette = pal;}
You can easily override this palette to specify other thangrayscale images.
We can likewise create a function that can convert an index to a System.Drawing.Color. If you are working with agrayscale image, there is probably no need for this.
public System.Drawing.Color GetColorFromIndex(byte c){return = myBitmap.Palette.Entries[c];}
Now let's put it all together into an easy-to-use8bit image access class. Remember to allow unsafe code blocks before compiling.
using System;using System.Drawing;using System.Drawing.Imaging;using System.Runtime.InteropServices;namespace ImageProc{ /// /// Class used for direct memory access to 8bit grayscale images /// public class Image8Bit { private BitmapData bmd; private Bitmap b; /// /// Locks an 8bit image in memory for fast get/set pixel functions. /// Remember to Dispose object to release memory. /// /// Bitmap reference public Image8Bit (Bitmap bitmap) { if(bitmap.PixelFormat!=System.Drawing.Imaging.PixelFormat.Format8bppIndexed) throw(new System.Exception("Invalid PixelFormat. 8 bit indexed required")); b = bitmap; //Store a private reference to the bitmap bmd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat); } /// /// Releases memory /// public void Dispose() { b.UnlockBits(bmd); } /// /// Gets color of an 8bit-pixel /// /// Row /// Column /// Color of pixel public unsafe System.Drawing.Color GetPixel(int x, int y) { byte* p = (byte *)bmd.Scan0.ToPointer(); //always assumes 8 bit per pixels int offset=y*bmd.Stride+x; return GetColorFromIndex(p[offset]); } /// /// Sets color of an 8bit-pixel /// /// Row /// Column /// Color index public unsafe void SetPixel(int x, int y, byte c) { byte* p = (byte *)bmd.Scan0.ToPointer(); //always assumes 8 bit per pixels int offset=y*bmd.Stride+(x); p[offset] = c; } /// /// Sets the palette for the referenced image to Grayscale /// public void MakeGrayscale() { SetGrayscalePalette(this.b); } /// /// Sets the palette of an image to grayscales (0=black, 255=white) /// /// Bitmap to set palette on public static void SetGrayscalePalette(Bitmap b) { ColorPalette pal = b.Palette; for(int i = 0; i < 256; i++) pal.Entries[i] = Color.FromArgb( 255, i, i, i ); b.Palette = pal; } private System.Drawing.Color GetColorFromIndex(byte c) { return = b.Palette.Entries[c]; } }}
- 图像8位数据读取使用.NET
- CImage读取8位灰度图像数据
- 字节对齐在24位bmp图像读取中的使用
- 使用OpenCV 读取图片 ,再用OpenGL显示,图像有错位
- 读取jpeg图像数据
- python使用h5py读取mat文件数据,并保存图像
- Cximage 库使用,直接读取图像数据到内存。
- 使用Linux的V4L2读取摄像头数据+Opencv图像处理
- 使用C++读取8位BMP位图
- 图像数据读取LockBits理解
- 使用GDI+将24位真彩色图像转换为8位灰度图像
- .net读取exsel数据
- 【图像处理】使用CImage类将彩色图像转换成8位灰色图像
- 图像处理 (第一次优化)增加了 二值图像 4位 8位 图像的读取 增加霓虹 浮雕效果
- ASP.NET数据库使用精典-----读取数据库中数据
- 使用 ADO.NET 来创建、读取、更新和删除数据
- c#中使用ADO.NET读取数据库中的数据
- 使用asp.net读取并显示excel数据
- 关于SIGPIPE导致的程序退出
- IOS消息推送------推送证书的安装与调试
- 使用BeanUtils方法拷贝不上问题
- iframe使用说明
- 有些access语句在数据库中可以执行,但是在C#上又不能执行,比如模糊查询-like
- 图像8位数据读取使用.NET
- ArcGIS系列软件学习参考书收藏
- SharePoint2010 内容类型剖析
- [mpeg4]mpeg4码流分析
- 内存对齐.结构体对齐
- Linux下定时任务的查看及取消
- WIN7虚拟网卡安装【图解】
- 底部菜单栏
- ArcGIS Engine二次开发中有关内存泄漏异常的解决方法