Solved

BitmapData pointer in C#

Posted on 2007-03-30
2
801 Views
Last Modified: 2013-12-17
In code I had in a Visual Studio C++ .NET Windows Form Application, I had a portion of code that looked like this:
bmd is a BitmapData object.

for(int y = 0; y < bmd->Height; y++)
{
      Byte* row = (Byte*)(void*)bmd->Scan0 +(y * bmd->Stride);
      for(int x = 0; x < bmd->Width*4; x++)
      {
              ...
        }
}

Now, I'm writing a function to do the same thing in C#.  However, I got a few errors.  I've been trying to find a C# equivalent that would give me the equivalent of the "Byte* row" line, but so far, I've got no luck.  This is what I have now, but its still not working. I've replaced that one entire line with this:

int rowPtr = y * bmd.Stride;
IntPtr scanPoint = bmd.Scan0;
Byte row;
unsafe
{
      void* bmdRow = scanPoint.ToPointer();
      //bmdRow+=rowPtr;   <- Gives error.
      row = (Byte)(bmdRow);
}

The code is basically supposed to give me a pointer to each row so I can set each pixel individually.  In that nested for loop was code that contained stuff like "row[x] = pixelvalue;"    If anyone can help me out with this, that'd be great.  Any questions to get a more detailed explanation of the problem, just ask.
0
Comment
Question by:raw_enha
  • 2
2 Comments
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18828275
Hi,

what error are you getting?
0
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 500 total points
ID: 18828305
If you want to walk over the image, pixel by pixel i'd do something like this (i've assumed it's all in an unsafe block):

Byte bPP = (Byte)(aData.Stride / aWidth); //determine how many bytes per pixel there are  
Byte rowOffset = (Byte)(aData.Stride - (aBPP * aWidth)); // determine whether there is a row offset

Byte* p = (Byte*)aData.Scan0; //pointer to beginning of image

for (Int32 y = 0; y < height; y++)
{
    for (Int32 x = 0; x < width; x++)
    {
        //do something to this pixel
      //e.g. p[0] = 255 etc

      //update position of pointer
        p += bPP;    
    }
    p += rowOffset;
}
 
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now