C sharp - ERROR PUNTERO

 
Vista:

ERROR PUNTERO

Publicado por kelvin (11 intervenciones) el 22/06/2007 11:34:18
Buenas a todos, estoy tratando de extraer los valores R,G,B de una imagen, esto lo hago con la ayuda de "System.Runtime.InteropServices.Marshal.Copy" en primer lugar creo un puntero que apunta a la primera línea del archivo de la imagen, y copio el contenido de esa primera línea dentro de un array, hasta ahí todo funciona perfectamente, pero cuando trato de saltar a la siguiente fila me sale el siguiente error:

"Intento de leer o escribir en la memoria protegida. A menudo, esto indica que hay otra memoria dañada."

Supongo que este error me sale por que estoy accediendo a memoria no controlada, y necesito privilegios para poder escribir en ella... pero no se bien como hacerlo, ni tampoco se si es este el error..

A continuación os pongo todo mi código por si alguien le encuentra el fallo, muchas gracias por la ayuda. Un saludo.

static private void RGBextract(Bitmap bmp, out int rojo, out int verde, out int azul)
{
int r = 0, g = 0, b = 0;
int totales = 0;

// Lock the bitmap's bits.
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);

int y = 0;

while (y < bmp.Height)
{

// Get the address of the first line.
int offset = (y * bmpData.Stride);

int ptr = bmpData.Scan0.ToInt32();

// Get address of next row
IntPtr realByteAddr = new IntPtr(ptr +System.Convert.ToInt32(y * bmpData.Stride));

// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 24 bits per pixels.
int bytes = bmp.Width * bmp.Height * 3;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(realByteAddr, rgbValues, 0, bytes); // Copy (source, destination,inicio,longitud)
// Set every red value to 255.
int counter = 0;
while (counter < bmp.Width)
{
b = b + rgbValues[counter];
counter++;
g = g + rgbValues[counter];
counter++;
r = r + rgbValues[counter];
counter++;
}
y++;
}
totales = bmp.Height * bmp.Width;
rojo = r/totales;
verde = g/totales;
azul = b/totales;
// Copy the RGB values back to the bitmap
//System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, realByteAddr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Se me olvidaba!

Publicado por Kelvin (11 intervenciones) el 22/06/2007 11:53:11
Se me olvidaba comentar que el error salta en esta linea:

System.Runtime.InteropServices.Marshal.Copy(realByteAddr, rgbValues, 0, bytes);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:ERROR PUNTERO

Publicado por Jose Clemente (1 intervención) el 17/12/2010 03:28:28
Hola kelvin, tengo un problema con el mismo error, solo que a mi se me genera cuando intento guardar una imagen con ayuda de una libreria llamada OpenCv, pudiste resolver el problema? Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar