• Jueves 28 de Marzo de 2024, 23:25

Autor Tema:  Ayuda Con Bitmap  (Leído 2177 veces)

AiTdX

  • Nuevo Miembro
  • *
  • Mensajes: 18
    • Ver Perfil
Ayuda Con Bitmap
« en: Miércoles 25 de Octubre de 2006, 13:18 »
0
Estoy intentando capturar frames de un video que se halla en reproduccion.
Para ello en primer lugar vuelco el buffer a un array.
Y luego uso la siguiente secuencia para obtener la imagen

GCHandle handle = GCHandle.Alloc(arrayRegistro,
GCHandleType.Pinned);
int scan0 = (int)handle.AddrOfPinnedObject();
scan0 += (altoVideo - 1) * stride;


captura = new Bitmap(anchoVideo, altoVideo, -stride,
PixelFormat.Format24bppRgb, (IntPtr)scan0);

handle.Free();

Lo que sucede es que luego cuando realiza una nueva captura mi anterio
bitmap tambien se modifica puesto que el puntero apunta a la nueva direccion.
He probado con new IntPtr(scan0) a la hora de pasarselo al bitmap y nada.

Por favor si alguien puede ayudar se lo agradeceria

JuanK

  • Miembro de ORO
  • ******
  • Mensajes: 5393
  • Nacionalidad: co
    • Ver Perfil
    • http://juank.io
Re: Ayuda Con Bitmap
« Respuesta #1 en: Miércoles 25 de Octubre de 2006, 16:02 »
0
claro, eso debe de suceder asi.

Lo que debes hacer es crear el bitmap usando ifromacion independiente para cada bitmap.

Asi que usas el puntero a la imagen para crear un nuevo byte array, usa System.Runtime.Interoperability.Marshall.CopyTo para copiar la informacion a un byte [] y una vez copiada al nuevo byte array debes crear el bitmaop tansolo usando las dimensiones, luego obtener el array de bytes del bitmap ( usando lockbytes) y le copias los bytes del array que hiciste aarriba.  :rolleyes:

bueno mas o menos seria asi: ( mas o menos porque puede cambiar un poco)

Código: Text
  1. // bitmapPaddedDataSize es ( stride * alto * bytesPorPixel)
  2. byte[] bmpBytes= new byte[bitmapPaddedDataSize];
  3.  
  4. //Copiar el puntero a un array de bytes
  5. System.Runtime.InteropServices.Marshal.Copy((IntPtr)scan0, 0, bmpBytes, bmpBytes.Length);
  6.  
  7. //Obtener memoria para los nuevos datos de bitmap,
  8. IntPtr punteroDatos = System.Runtime.InteropServices.Marshal.AllocHGlobal(bitmapPaddedDataSize);
  9.  
  10. //Copiar el array de bytes creado al puntero
  11. System.Runtime.InteropServices.Marshal.Copy(bmpBytes, 0, punteroDatos, bmpBytes.Length);
  12.  
  13. //Crear bmp
  14. captura = new Bitmap(anchoVideo, altoVideo, -stride,
  15. PixelFormat.Format24bppRgb, punteroDatos );
  16.  
  17. handle.Free();
  18.  

No se como lo tengas pero el problema puede tambien ser que la imagen la estas mostrando haciendo referencia puntualmente al objeto captura , pues seguiras teniendo el mismo problema, asi que debes hacer referencia es a un  nuevo objeto y a este objeto copiarle el valor de captura, parao cual puedes usar la misma tecnica que te mostré arriba.
[size=109]Juan Carlos Ruiz Pacheco
[/size]
Microsoft Technical Evangelist
@JuanKRuiz
http://juank.io

AiTdX

  • Nuevo Miembro
  • *
  • Mensajes: 18
    • Ver Perfil
Re: Ayuda Con Bitmap
« Respuesta #2 en: Jueves 26 de Octubre de 2006, 10:05 »
0
Gracias lo que tu me has respondido era lo mismo en lo que estaba trabajando.
Sin embargo ahora me hallo ante un nuevo error.

Primero asi ha quedado el metodo

            GCHandle handle = GCHandle.Alloc(arrayRegistro, GCHandleType.Pinned);
            int scan0 = (int)handle.AddrOfPinnedObject();
            scan0 += (altoVideo - 1) * stride;
           
            int  bitmapPaddedDataSize=( stride * altoVideo * 3);
            byte[] bmpBytes = new byte[bitmapPaddedDataSize];

            //Copiar el puntero a un array de bytes
            Marshal.Copy((IntPtr)scan0, bmpBytes, 0, bmpBytes.Length);

            //Obtener memoria para los nuevos datos de bitmap,
            IntPtr punteroDatos = Marshal.AllocHGlobal(bitmapPaddedDataSize);

            //Copiar el array de bytes creado al puntero
            Marshal.Copy(bmpBytes, 0, punteroDatos, bmpBytes.Length);

            //Crear bmp
            captura = new Bitmap(anchoVideo, altoVideo, -stride,
            PixelFormat.Format24bppRgb, punteroDatos);

            handle.Free();


Lo llamo para asignarlo al atributo imagen de un objeto secuencia

Bitmap imgGol = video.Captura();


Y al tratar de mostrarlo

                e.Graphics.DrawImage(secuencia.Imagen, posImagen);

En la siguiente sentencia me da el error

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Con esta stackTrace

   at System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI(HandleRef graphics, HandleRef image, Int32 x, Int32 y, Int32 width, Int32 height)
   at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
   at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
   at OrganizerINEF.componentes.SecuenciasEventosC.listBox1_DrawItem(Object sender, DrawItemEventArgs e) in C:\Documents and Settings\lfcia\Mis documentos\Visual Studio 2005\Projects\OrganizerINEF\OrganizerINEF\es\aitdx\vista\componentes\segundoNivel\SecuenciasEventosC.cs:line 87
   at System.Windows.Forms.ListBox.OnDrawItem(DrawItemEventArgs e)
   at System.Windows.Forms.ListBox.WmReflectDrawItem(Message& m)
   at System.Windows.Forms.ListBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
   at System.Windows.Forms.Control.WmOwnerDraw(Message& m)
   at System.Windows.Forms.Control.WmDrawItem(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ListBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at OrganizerINEF.Program.Main() in C:\Documents and Settings\lfcia\Mis documentos\Visual Studio 2005\Projects\OrganizerINEF\OrganizerINEF\Program.cs:line 17
   at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Gracias por todo

JuanK

  • Miembro de ORO
  • ******
  • Mensajes: 5393
  • Nacionalidad: co
    • Ver Perfil
    • http://juank.io
Re: Ayuda Con Bitmap
« Respuesta #3 en: Jueves 26 de Octubre de 2006, 15:11 »
0
Bueno en este momento no se como resolverlo, pero una recomendacion inicial: usa un PictureBox para dibujar la imagen, es mucho mas sencillo y no necesitas liarte con los eventos.
[size=109]Juan Carlos Ruiz Pacheco
[/size]
Microsoft Technical Evangelist
@JuanKRuiz
http://juank.io

AiTdX

  • Nuevo Miembro
  • *
  • Mensajes: 18
    • Ver Perfil
Re: Ayuda Con Bitmap
« Respuesta #4 en: Jueves 26 de Octubre de 2006, 18:43 »
0
He probado con el picture box y sigo con el mismo problema

En teoria a este nuevo bitmap si que podría acceder ¿no?

JuanK

  • Miembro de ORO
  • ******
  • Mensajes: 5393
  • Nacionalidad: co
    • Ver Perfil
    • http://juank.io
Re: Ayuda Con Bitmap
« Respuesta #5 en: Jueves 26 de Octubre de 2006, 18:59 »
0
bueno eso es algo de revisar mejor...

prueba dejando de hacer esto: handle.Free();

Y revisa muy bien las copas de memoria que hiciste...
[size=109]Juan Carlos Ruiz Pacheco
[/size]
Microsoft Technical Evangelist
@JuanKRuiz
http://juank.io