求一个C#读取BMP图片像素的完整代码,或者可以下载到的网址也可以

如题所述

using System.Runtime.InteropServices;
[DllImport("gdi32.dll")]
public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount,
ref byte lpRgnData);

public static int RGN_AND = 1;
public static int RGN_OR = 2;
public static int RGN_XOR = 3;
public static int RGN_DIFF = 4;
public static int RGN_COPY = 5;
public static int RGN_MIN = RGN_AND;
public static int RGN_MAX = RGN_COPY;

[DllImport("gdi32.dll")]
public static extern int CombineRgn(IntPtr hrgnDest, IntPtr hrgnSrc1, IntPtr hrgnSrc2,
int fnCombineMode);

[DllImport("gdi32.dll")]public static extern bool DeleteObject(IntPtr hObject);
public Region ImageToRegion(Image AImage, Color ATransparent)
{
//转贴请注明出处ZswangY37(wjhu111#21cn.com) 时间2007-05-25
if (AImage == null) return null;
Bitmap vBitmap = new Bitmap(AImage);
BitmapData vBitmapData = vBitmap.LockBits(
new Rectangle(0, 0, vBitmap.Width, vBitmap.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
int vAddress = (int)vBitmapData.Scan0;
int vOffset = vBitmapData.Stride - vBitmap.Width * 4; // 每行多出的字节数
int h = vBitmap.Height, w = vBitmap.Width;
int vTransparent = ColorTranslator.ToWin32(ATransparent); // 透明色
int vAllocRect = (0x1000 - sizeof(uint) * 8) / sizeof(int); // 预分配的矩形数
if (h * w < vAllocRect) vAllocRect = h * w;
Byte[] vBuffer = new byte[sizeof(uint) * 8 + sizeof(int) * 4 * vAllocRect];
//头信息dwSize\iType\nCount\nRegSize
uint vCount = 0;
vBuffer[0] = sizeof(uint) * 8; //dwSize//头信息大小
vBuffer[4] = 1; //iType//int RDH_RECTANGLES = 1;//数据类型
IntPtr vResult = IntPtr.Zero;

uint vPointer = sizeof(uint) * 8;
bool vWriteRect = false; bool vWriteAlways = false;
for (int y = 0; y < h; y++)
{
int vBlockStart = 0;
bool vLastMaskBit = false;

for (int x = 0; x < w; x++)
{
int i = Marshal.ReadInt32((IntPtr)vAddress) & 0x00FFFFFF;
if (vTransparent == i) // 透明色
{
if (vLastMaskBit)
vWriteRect = true;
}
else
{
if (!vLastMaskBit)
{
vBlockStart = x;
vLastMaskBit = true;
}
}
if (x == w - 1)
{
if (y == h - 1)
{
vWriteRect = true;
vWriteAlways = true;
}
else if (vLastMaskBit)
{
vWriteRect = true;
}
x++;
}
if (vWriteRect)
{
if (vLastMaskBit)
{
vCount++;
WriteRect(vBuffer, ref vPointer,
new Rectangle(vBlockStart, y, x - vBlockStart, 1));
}
if (vCount == vAllocRect || vWriteAlways)
{

vBuffer[8] = (byte)vCount;
vBuffer[9] = (byte)(vCount >> 8);
vBuffer[10] = (byte)(vCount >> 16);
vBuffer[11] = (byte)(vCount >> 24);
IntPtr hTemp = ExtCreateRegion(IntPtr.Zero,
sizeof(uint) * 8 + sizeof(int) * 4 * vCount,
ref vBuffer[0]);
if (vResult == IntPtr.Zero)
vResult = hTemp;
else
{
CombineRgn(vResult, vResult, hTemp, RGN_OR);
DeleteObject(hTemp);
}
vCount = 0;
vPointer = sizeof(uint) * 4;
vWriteAlways = false;
}
vWriteRect = false;
vLastMaskBit = false;
}
vAddress += 4;
}
vAddress += vOffset;
}

vBitmap.UnlockBits(vBitmapData);
return Region.FromHrgn(vResult);
}

private void WriteRect(byte[] ARGNData, ref uint ptr, Rectangle r)
{
ARGNData[ptr] = (byte)r.X;
ARGNData[ptr + 1] = (byte)(r.X >> 8);
ARGNData[ptr + 2] = (byte)(r.X >> 16);
ARGNData[ptr + 3] = (byte)(r.X >> 24);
ARGNData[ptr + 4] = (byte)r.Y;
ARGNData[ptr + 5] = (byte)(r.Y >> 8);
ARGNData[ptr + 6] = (byte)(r.Y >> 16);
ARGNData[ptr + 7] = (byte)(r.Y >> 24);
ARGNData[ptr + 8] = (byte)r.Right;
ARGNData[ptr + 9] = (byte)(r.Right >> 8);
ARGNData[ptr + 10] = (byte)(r.Right >> 16);
ARGNData[ptr + 11] = (byte)(r.Right >> 24);
ARGNData[ptr + 12] = (byte)r.Bottom;
ARGNData[ptr + 13] = (byte)(r.Bottom >> 8);
ARGNData[ptr + 14] = (byte)(r.Bottom >> 16);
ARGNData[ptr + 15] = (byte)(r.Bottom >> 24);
ptr += 16;
}

private void button1_Click(object sender, EventArgs e)
{
Region = ImageToRegion(pictureBox1.Image, Color.FromArgb(216, 233, 236));
}

来源:http://bbs.csdn.net/topics/230025357
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-03-12
以下是我的测试代码,希望对你有所帮助
private void ScreenPicture()
{
Bitmap bitMap = new Bitmap("XXXX.bmp");
BitmapData bitMapData = bitMap.LockBits(new Rectangle(0, 0, bitMap.Width, bitMap.Height)
, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //以24位像素读取
IntPtr nFirstAdress = bitMapData.Scan0;
byte[] bytePicturePixels = new byte[bitMap.Width * bitMap.Height * 3];
Marshal.Copy(nFirstAdress, bytePicturePixels, 0, bitMap.Width * bitMap.Height * 3);
for (int i = 0; i < bytePicturePixels.Length; i += 3)
{
//遍历所有的像素
}
Marshal.Copy(bytePicturePixels, 0, nFirstAdress, bitMap.Width * bitMap.Height * 3);
bitMap.UnlockBits(bitMapData);
}
最后两句是如果有修改的话,重新Copy到BitmapData中,如果是读取的话,可以使用只读的方式取得BitmapData对象本回答被提问者采纳

用c#编写一个程序读取一张bmp图片的数据并转化为灰度图保存到文件中
Bitmap bmpobj = (Bitmap)img;转换为灰度图片(传入刚才转换为 Bitmap 的对象,返回值为转换后的灰度图片对象):public Bitmap GrayByPixels(Bitmap bmpobj){ for (int i = 0; i < bmpobj.Height; i++){ for (int j = 0; j < bmpobj.Width; j++){ int tmpValue = GetGrayNumCo...

如何用c语言printf输出bmp图片的像素信息。
1,fp); \/\/获取图像宽、高、每像素所占位数等信息 bmpWidth = head.biWidth; bmpHeight = head.biHeight; biBitCount = head.biBitCount;\/\/定义变量,计算图像每行像素所占的字节数(

c语言,怎样读取一个BMP图片?
fseek(file,0L,0); \/\/读取图像文件类型 fread(&bmp,sizeof(BMP),1,file); printf("\\n bmp tpye: %u",bmp.bfType); printf("\\n bmp size: %u",bmp.bfSize); printf("\\n bmp reserved1: %u",bmp.bfReserved1); printf("\\n bmp reserved2: %u",bmp.bfReserved2); printf("\\n bmp offBits:...

C#编程 来处理BMP图像
(sourceInfo->biWidth)*24+31)\/32\/\/保证宽度位数是32的整数倍(这时位图的四字节对齐要求)sourceInfo->biWidth)*24+31)\/32*4\/\/获取每行共有字节的总数 ((sourceInfo->biWidth)*24+31)\/32*4*(sourceInfo->biHeight))\/\/图片数据区的数据的大小 ...

用C++ 读取bmp信息,求代码
void MazeToArray(bmpMaze maze){ \/\/将图片像素信息读取到Ma数组中 int status,i=0,x,y,n=8,j=0,line;line=maze.datasize\/maze.height;for(x=maze.height-1;x>=0;x--){ for(y=0;y<maze.width;y++){ \/\/获取这个点是黑的还是白的 status=maze.bmp[i]&(int)pow(2,n-1);if(!

用c++编写的读取bmp图中任意一像素点的RGB值的代码
控制台 读取BMP图片文件 输出指定点的颜色分量 include <stdio.h> include <stdlib.h> include <malloc.h> typedef unsigned long DWORD;typedef int BOOL;typedef unsigned char BYTE;typedef unsigned short WORD;typedef struct tagBITMAPINFOHEADER{ DWORD biSize;long biWidth;long ...

c#中 调用大漠插件,谁能给个找图的代码(如何使用Findpic()),谢谢
那个好像有说明文档吧,你应该会实例化大漠吧? 绑定窗口也会吧?那我就不多少了,直接给你FindPic的代码吧。Dm.dmsoft dm = new Dm.dmsoft();object intX = 0;object intY = 0;int res = dm.FindPic(0, 0, 2000, 2000, "C:\/Pic.bmp", "000000", 0.9, 0, out intX, out ...

asp.net(C#)如何搜集远程图片并传到服务器
回答:只能能你思路,代码是没空写的 webclient类,链接远程网站,遍历每个链接,分析每个页面的源码,抓取img标签,用正则获取src属性,下载回来保存到服务器上就可以了,也可以自动分析图片的大小信息以决定去留问题

用C语言读取16位bmp图片的每个像素的信息~
biBitCount 等于 16 就是16位,等于24 就是24位,等于32 就是32位。颜色在哪,要考虑 biCompression 压缩方法,若等于 BI_RGB, bmiColors 就等于 NULL. 在 bitmap 数组里 每个WORD 就是 一个像素点. 5个 bits 兰,5个 bits 绿,再5个 bits 红,最高位不用。其它方法自己看资料吧。

关于怎么用C++读取bmp图片
可以看下如下的操作:1、图片文件是有固定格式的,像BMP图片是文件头+位图的颜色数据。文件头一般在读取的时候是使用下面的代码:BITMAPFILEHEADER fileheader={0}; fread(&fileheader,sizeof(fileheader),1,fp); if(fileheader.bfType!=0x4D42) \/\/ 判断是否为BMP图片 { fclose(fp); ...

相似回答