http://zip.nvp.com.tw/forum.php?mod=viewthread&tid=1104&extra=page%3D29
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace Bitmap_Pixel_CS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.Image = Image.FromFile(@"c:windowsOPEN-chan_no33_1280.jpg");
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = (Bitmap)Image.FromFile(@"c:windowsOPEN-chan_no33_1280.jpg");
BitmapPlus bmpP = new BitmapPlus(bmp);
bmpP.BeginAccess();
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
Color bmpCol = bmpP.GetPixel(i, j);
if (bmpCol.R == (byte)0)
{
bmpP.SetPixel(i, j, Color.FromArgb(255, 255, 0, 0));
}
else
{
bmpP.SetPixel(i, j, Color.FromArgb(255, 0, 0, 0));
}
}
}
bmpP.EndAccess();
pictureBox1.Image = bmp;
}
class BitmapPlus
{
private Bitmap _bmp = null;
private BitmapData _img = null;
private int _BytesPerPixel;
public BitmapPlus(Bitmap original)
{
_bmp = original;
_BytesPerPixel = Bitmap.GetPixelFormatSize(_bmp.PixelFormat) / 8;
}
public void BeginAccess()
{
_img = _bmp.LockBits(new Rectangle(0, 0, _bmp.Width, _bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, _bmp.PixelFormat);
}
public void EndAccess()
{
if (_img != null)
{
_bmp.UnlockBits(_img);
_img = null;
}
}
public Color GetPixel(int x, int y)
{
if (_img == null)
{
return _bmp.GetPixel(x, y);
}
IntPtr adr = _img.Scan0;
int pos = x * _BytesPerPixel + _img.Stride * y;
byte b = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 0);
byte g = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 1);
byte r = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 2);
return Color.FromArgb(r, g, b);
}
public void SetPixel(int x, int y, Color col)
{
if (_img == null)
{
_bmp.SetPixel(x, y, col);
return;
}
IntPtr adr = _img.Scan0;
int pos = x * _BytesPerPixel + _img.Stride * y;
System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 0, col.B);
System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 1, col.G);
System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 2, col.R);
}
}
}
}