Setting Pixel Value with Unsafe Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello,

- I have a Bitmap Object named: bmp1

- I already have the numerical values of:red,blue and green colors + I have
the X and Y cordinates.

How do I set the pixel value via safe code on the bmp1 object?
(the pixel location based on the X & Y values and it's color based on the
red,blue and green values the I already have)

thx!
 
hi ali,
thx for your answer but I want to do it with unsafe code.
your sample is slow, unsafe make it faster.

thx anyway.
 
Not a problem. Here's an example:

using System.Drawing;
using System.Drawing.Imaging;

public someFunction(Bitmap b)
{
BitmapData bmData = b.LockBits(
new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte * p = (byte *)(void *)Scan0;

int nOffset = stride - b.Width*3;
int nWidth = b.Width * 3;

for(int y=0;y<b.Height;++y)
{
for(int x=0; x < nWidth; ++x )
{
// Set the colors to whatever
p[0] = (byte)(255);
p[1] = (byte)(255);
p[2] = (byte)(255);

// increment to next pixel
p += 3;
}
p += nOffset; // Go to end of line
}
}
//... whatever else you want to do
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
thx! , but I have 2 questions:
1. How can I optimize that to 256 colot bitmap?
2. Whre do I enter my values? (X & Y + Blue,Red and Green values)?
I just need to set one pixel, not the whole image!

thx!!!

Kevin Spencer said:
Not a problem. Here's an example:

using System.Drawing;
using System.Drawing.Imaging;

public someFunction(Bitmap b)
{
BitmapData bmData = b.LockBits(
new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte * p = (byte *)(void *)Scan0;

int nOffset = stride - b.Width*3;
int nWidth = b.Width * 3;

for(int y=0;y<b.Height;++y)
{
for(int x=0; x < nWidth; ++x )
{
// Set the colors to whatever
p[0] = (byte)(255);
p[1] = (byte)(255);
p[2] = (byte)(255);

// increment to next pixel
p += 3;
}
p += nOffset; // Go to end of line
}
}
//... whatever else you want to do
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

TomHL said:
hello,

- I have a Bitmap Object named: bmp1

- I already have the numerical values of:red,blue and green colors + I
have
the X and Y cordinates.

How do I set the pixel value via safe code on the bmp1 object?
(the pixel location based on the X & Y values and it's color based on the
red,blue and green values the I already have)

thx!
 
PMJI, but if you're only setting one pixel, as you say elsewhere, I don't
see where using the Bitmap's SetPixel method as suggested by Ali Raza would
cause you any performance problems. If you were setting EVERY pixel in the
image, or even a lot of them, I can see why you might be looking for a
higher-performance solution, but for a single call I doubt if it makes any
measurable difference.

Unless you just WANT to use unsafe code for some reason. Then of course you
can just do whatever you want.

Tom Dacon
Dacon Software Consulting
 
SetPixel and GetPixel are slow because they set a single pixel using
LockBits to perform a single pixel operation. The only advantage to using
LockBits is if you are setting many pixels at once. If you're only setting
one, use SetPixel.

For 8 bit image operations you will need to understand the format of the
BitmapData buffer for that pixels format. I refer you once again to the
article in the GDI+ FAQ.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





TomHL said:
thx! , but I have 2 questions:
1. How can I optimize that to 256 colot bitmap?
2. Whre do I enter my values? (X & Y + Blue,Red and Green values)?
I just need to set one pixel, not the whole image!

thx!!!

Kevin Spencer said:
Not a problem. Here's an example:

using System.Drawing;
using System.Drawing.Imaging;

public someFunction(Bitmap b)
{
BitmapData bmData = b.LockBits(
new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte * p = (byte *)(void *)Scan0;

int nOffset = stride - b.Width*3;
int nWidth = b.Width * 3;

for(int y=0;y<b.Height;++y)
{
for(int x=0; x < nWidth; ++x )
{
// Set the colors to whatever
p[0] = (byte)(255);
p[1] = (byte)(255);
p[2] = (byte)(255);

// increment to next pixel
p += 3;
}
p += nOffset; // Go to end of line
}
}
//... whatever else you want to do
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

TomHL said:
hello,

- I have a Bitmap Object named: bmp1

- I already have the numerical values of:red,blue and green colors + I
have
the X and Y cordinates.

How do I set the pixel value via safe code on the bmp1 object?
(the pixel location based on the X & Y values and it's color based on
the
red,blue and green values the I already have)

thx!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top