473,698 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshal.Copy throws Exception in Multithreaded Webcam capture

1 New Member
I have a server listening for connections. Client connects and both start streaming webcam images. This works fine as long as there is only one client connected(runni ng as a Thread). When another client connects, the new thread starts streaming as well. The camera is accessed in a mutual exclusive way by locking which fails some times and I get AccessViolation Exception. Below is the code executing in the thread.

The scenario is like this : One person sitting at a help desk and doing video conferencinf with multiple clients. Server hosted at the helpdesk itself.

Expand|Select|Wrap|Line Numbers
  1. try {
  2.       bytes = new byte[(ConfConsole.CAP.Stride * ConfConsole.CAP.Height)];
  3.       lock (lok) {
  4.           ptr = ConfConsole.CAP.GetBitMap(); //static object accessed by threads
  5.           Marshal.Copy(ptr, bytes, 0, bytes.Length); //error here sometimes
  6.           if (ptr != IntPtr.Zero) {
  7.               Marshal.FreeCoTaskMem(ptr);//error here sometimes
  8.               ptr = IntPtr.Zero;
  9.           }
  10.       }
  11.       bmp = (Bitmap)Utils.CopyDataToBitmap(
  12.             bytes, ConfConsole.CAP.Width, ConfConsole.CAP.Height);
  13.       bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
  14.       jpg = Utils.ResizeBitmap(bmp, 320, 240);
  15.       bmp.Dispose();
  16. } catch (Exception e) {                            
  17.       l.log("error in capturing image + data : " + e.Message);
  18. }
  19.  
  20. //code ripped from net
  21. public static Bitmap CopyDataToBitmap(byte[] data, int width, int height) {            
  22.             Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
  23.             //Create a BitmapData and Lock all pixels to be written
  24.             BitmapData bmpData = bmp.LockBits(
  25.                                  new Rectangle(0, 0, bmp.Width, bmp.Height),
  26.                                  ImageLockMode.WriteOnly, bmp.PixelFormat);
  27.             //Copy the data from the byte array into BitmapData.Scan0
  28.             Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
  29.             //Unlock the pixels
  30.             bmp.UnlockBits(bmpData);
  31.             //Return the bitmap
  32.             return bmp;
  33.         }
  34.  
  35.  
May 21 '09 #1
0 2220

Sign in to post your reply or Sign up for a free account.

Similar topics

4
11992
by: Joakim Rylander | last post by:
Hi all, I need to create a public booth where people can look at themselves in a webcam and snap a picture which will be seen on a webpage. The backend is easy, it's the webcam part that I have problems with... Could you recommend the best way to capture a Webcam picture from .NET? Do I need device specific drivers or is there a standardized way to do it? Any recommended brand of Webcam?
3
4909
by: Christy | last post by:
Does anyone have any idea how i can begin to start programming an automatic image capture from a webcam by just using .NET? Thanks!
0
3205
by: CodeMayhem | last post by:
Hey fellow coders, I need help in integrating webcam capturing into a program written i C#. the camera will face the user and when he or she clicks th "capture" button, the camera should take the picture and display it o the program. before clicking the capture button, the user should b able to see the camera's view so that the user can adjust the positio of the camera in order to get a good picture. Hope someone can help me on this.
1
3581
by: Nikolay Petrov | last post by:
I need to capture frames from my webcam using a windows service. I've found some sample code on how to use thw VfW API, but I have two major problems: 1. The sample code uses picturebox to preview and grab images. I want to do it in formless windows service, so I need to capture frames without displaying anything. 2. All samples I've found are using Windows Clipboard. They call the API function which grabs as frame and put it in...
12
1557
by: tony obrien | last post by:
Hi, I have written a Class Library in VB.net which provides Props/Meths to a caller to hide the uglinesses of a TCP message protocol between a Client and a Server. All this works just fine. Now I am trying to develop a LOAD TESTER app which spawns lotso' threads each with this capability to bang the server.
0
3413
by: indhu | last post by:
Hi all, am new to the VB forum. Anyway, i need a help from u all. i am developing project.where i am using some kind of webcam. capture image thru webcam by cliking capture button in the form. i ve to pick a color from the image using color picker tool. then this tool will drop the color in the next image (predefined image)other side of the form. then i will choose the lip area alone to change color using some predefined colors. any...
2
31649
debasisdas
by: debasisdas | last post by:
This program checks if webcam is available, if available then capture video and displays in picture box. Add this code to the general module (.BAS file) ---------------------------------------------------------------------- Public Const ws_child As Long = &H40000000 Public Const ws_visible As Long = &H10000000 Global Const WM_USER = 1024 Global Const wm_cap_driver_connect = WM_USER + 10 Global Const wm_cap_set_preview = WM_USER + 50
1
3132
by: Steve | last post by:
Hi All I have a VB.net 2005 windows forms application which uses webcam to take photos of staff members etc for use within the application All works fine if an external webcam is used The problem I have is when clients have a laptop with built in webcam The first time I connect to the capture drivers all works well. If I try to
2
7201
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
8668
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9014
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8855
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.