469,366 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

How to access all the pixels of a bitmap with C#?

Dear ladies and gentlemen:

How to access all the pixels of a bitmap with C#? I am working with
Delphi before. In Delphi, you can use TBitmap.Scanline property to access
all the pixels of a bitmap object. How to do this with C#? Any hints? Thank
you very much.

Best regards.

Xiaoguang
Nov 17 '05 #1
4 23074
Hi Xiaoguang,

In C# you use Bitmap.LockBits and handle the returning BitmapData with
pointers in an 'unsafe' block. BitmapData.Scan0 would be the start of the
pixel data.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
> How to access all the pixels of a bitmap with C#? I am working with
Delphi before. In Delphi, you can use TBitmap.Scanline property to access
all the pixels of a bitmap object. How to do this with C#? Any hints? Thank you very much.

Something like this:

/// <summary>
/// Create a gray gradient bar to represent the colors palette.
/// </summary>
private void MakeGrayColorPaletteBitmap() {
try {
int iWidth=this.Width;
if (iWidth<=0) iWidth=256;
int iHeight=this.Height;
if (iHeight<=0) iHeight=1;
Palette=new Bitmap(iWidth,iHeight, PixelFormat.Format8bppIndexed);
// Lock a rectangular portion of the bitmap for writing so we can
fill this with a gradient value.
BitmapData bitmapData=null;
Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
try {
bitmapData =
Palette.LockBits(rect,ImageLockMode.WriteOnly,Pixe lFormat.Format8bppIndexed)
;
// Write to the temporary buffer that is provided by LockBits.
// Copy the pixels from the source image in this loop.
// Because you want an index, convert RGB to the appropriate
// palette index here.
IntPtr pixels = bitmapData.Scan0;
unsafe {
// Get the pointer to the image bits.
// This is the unsafe operation.
byte * pBits;
if (bitmapData.Stride > 0) {
pBits = (byte *)pixels.ToPointer();
} else {
// If the Stide is negative, Scan0 points to the last
// scanline in the buffer. To normalize the loop, obtain
// a pointer to the front of the buffer that is located
// (Height-1) scanlines previous.
pBits = (byte *)pixels.ToPointer() +
bitmapData.Stride*(Height-1);
}
uint stride = (uint)Math.Abs(bitmapData.Stride);
float fStep=(float)(256.0/Width);
for ( uint row = 0; row < Height; ++row ) {
for ( uint col = 0; col < Width; ++col ) {
byte * p8bppPixel = pBits + row*stride + col;
*p8bppPixel=(byte)(fStep*col);
}
}
}
} finally {
// To commit the changes, unlock the portion of the bitmap.
Palette.UnlockBits(bitmapData);
}
} catch {
}
}

--
http://www.skyscan.be
Nov 17 '05 #3
Hi Morten,

Thank you very much!

Best Regards.

Xiaoguang

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsohudqsmklbvpo@pbn_computer...
Hi Xiaoguang,

In C# you use Bitmap.LockBits and handle the returning BitmapData with
pointers in an 'unsafe' block. BitmapData.Scan0 would be the start of the
pixel data.

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #4
Hi Olaf,

Thank you very much!

Best regards.

Xiaoguang

"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:42**********************@news.skynet.be...
How to access all the pixels of a bitmap with C#? I am working with
Delphi before. In Delphi, you can use TBitmap.Scanline property to access all the pixels of a bitmap object. How to do this with C#? Any hints? Thank
you very much.

Something like this:

/// <summary>
/// Create a gray gradient bar to represent the colors palette.
/// </summary>
private void MakeGrayColorPaletteBitmap() {
try {
int iWidth=this.Width;
if (iWidth<=0) iWidth=256;
int iHeight=this.Height;
if (iHeight<=0) iHeight=1;
Palette=new Bitmap(iWidth,iHeight, PixelFormat.Format8bppIndexed);
// Lock a rectangular portion of the bitmap for writing so we can
fill this with a gradient value.
BitmapData bitmapData=null;
Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
try {
bitmapData =

Palette.LockBits(rect,ImageLockMode.WriteOnly,Pixe lFormat.Format8bppIndexed) ;
// Write to the temporary buffer that is provided by LockBits.
// Copy the pixels from the source image in this loop.
// Because you want an index, convert RGB to the appropriate
// palette index here.
IntPtr pixels = bitmapData.Scan0;
unsafe {
// Get the pointer to the image bits.
// This is the unsafe operation.
byte * pBits;
if (bitmapData.Stride > 0) {
pBits = (byte *)pixels.ToPointer();
} else {
// If the Stide is negative, Scan0 points to the last
// scanline in the buffer. To normalize the loop, obtain // a pointer to the front of the buffer that is located // (Height-1) scanlines previous.
pBits = (byte *)pixels.ToPointer() +
bitmapData.Stride*(Height-1);
}
uint stride = (uint)Math.Abs(bitmapData.Stride);
float fStep=(float)(256.0/Width);
for ( uint row = 0; row < Height; ++row ) {
for ( uint col = 0; col < Width; ++col ) {
byte * p8bppPixel = pBits + row*stride + col;
*p8bppPixel=(byte)(fStep*col);
}
}
}
} finally {
// To commit the changes, unlock the portion of the bitmap.
Palette.UnlockBits(bitmapData);
}
} catch {
}
}

--
http://www.skyscan.be

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by James Dean | last post: by
1 post views Thread by Depor | last post: by
3 posts views Thread by Chris Saunders | last post: by
reply views Thread by ProtossLee | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.