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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



> 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,PixelFormat.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


.



Relevant Pages

  • Re: How to access all the pixels of a bitmap with C#?
    ... Xiaoguang ... In Delphi, you can use TBitmap.Scanline property to ... >> all the pixels of a bitmap object. ...
    (microsoft.public.dotnet.languages.csharp)
  • How to access all the pixels of a bitmap with C#?
    ... How to access all the pixels of a bitmap with C#? ... In Delphi, you can use TBitmap.Scanline property to access ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with SavePicture, and a question on new versions
    ... > mode does not have to be in pixels. ... 500 pixels then the picture box will also be 700 x 500 pixels. ... and the bitmap shows ... to be *smaller* than the scale units that the BitBlt API is using ...
    (comp.lang.basic.visual.misc)
  • Re: How long Win32 viable?
    ... > time Windows outgrows the old widget world. ... screen pixels per inch is doubled. ... Only in the last few years have PCs had the power to run Avalon type display ... Bog standard Win API compatibility via Delphi is amazingly good across ...
    (borland.public.delphi.non-technical)
  • Re: How long Win32 viable?
    ... >> monitor pixels. ... the pixel based world we have with the Win32 API approach to graphics. ... would translate your comment as meaning that your apps are designed for one ... expecially when the bitmap has a lot more pixels than the form it is ...
    (borland.public.delphi.non-technical)