Solved

BitmapData pointer in C#

Posted on 2007-03-30
2
821 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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