By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Setting Pixel Value with Unsafe Code

P: n/a
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!

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Salam

Following is the Solution in C#

Bitmap b=new Bitmap();

Color c=Color.FromArgb(red,green,blue);

b.SetPixel(x,y,c);


--
ALI RAZA SHAIKH
MCAD.net

www.programmersparadise.cjb.net
alirazashaikh.blogspot.com
"TomHL" <To***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
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!

Nov 17 '05 #2

P: n/a
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.
"ALI RAZA" wrote:
Salam

Following is the Solution in C#

Bitmap b=new Bitmap();

Color c=Color.FromArgb(red,green,blue);

b.SetPixel(x,y,c);


--
ALI RAZA SHAIKH
MCAD.net

www.programmersparadise.cjb.net
alirazashaikh.blogspot.com
"TomHL" <To***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
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!


Nov 17 '05 #3

P: n/a
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" <To***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
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!

Nov 17 '05 #4

P: n/a
Refer to the LockBits 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" <To***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
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!

Nov 17 '05 #5

P: n/a
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" wrote:
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" <To***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
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!


Nov 17 '05 #6

P: n/a
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

"TomHL" <To***@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
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.
"ALI RAZA" wrote:
Salam

Following is the Solution in C#

Bitmap b=new Bitmap();

Color c=Color.FromArgb(red,green,blue);

b.SetPixel(x,y,c);


--
ALI RAZA SHAIKH
MCAD.net

www.programmersparadise.cjb.net
alirazashaikh.blogspot.com
"TomHL" <To***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
> 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!
>


Nov 17 '05 #7

P: n/a
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" <To***@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com...
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" wrote:
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" <To***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
> 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!
>


Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.