473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 23809
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dalan | last post by:
At first I was not certain what could cause Access 97 from displaying most jpeg images, but not all. After further testing, it seemed that all original images of less than 275 pixels per inch or...
1
by: James Dean | last post by:
I was wondering if anybody knew and easy way to read pixels. Is the easiest way to read a byte in convert it to a Bitarray and then when finished convert it back to Byte......I am looking in my...
4
by: serdar c | last post by:
hi everyone, i am writing a program for my scool project and i need to count the number of black pixels in a white empty picture box.. in the old visual basic there was a really simple command...
2
by: djharrison | last post by:
Greetings, I am attempting to develop an application that will color the pixels in a PictureBox based on the x and y coordinates for individual pixels in an array. I would like to load the...
1
by: Depor | last post by:
I want to do some operations on pixels in bitmap. But method GetPixel() is slow. There is a function ScanLine() in Borland C++ Builder and it's possible to operate on all row of pixels at once. Do...
1
by: bongoo | last post by:
I'm reading pixel values (RGB) from a bitmap object using function from system.drawing namespace & vb.net (.net framework 1.1) But I would like to make it faster, so is there any another way of...
3
by: Chris Saunders | last post by:
I am fairly new to .Net and C# but am familiar with the Win32 API. I wish to set the pixels of the client area of a window one at a time with a specific color. I have so far discovered that...
4
by: Martijn Mulder | last post by:
When I zoom in on an image, GDI+ automatically smoothens the edges between the pixels. I am looking for a way to see the individual pixels as squares in the enlarged image, like in MSPaint. I...
0
by: ProtossLee | last post by:
Hi, I am make a project to draw bitmap image. Pixel intensities are calculated from data received from instruments. Problem is, sometimes I get wrong data points, such as: correct data should be...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.