473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Unmanaged buffer problem?

I've got an app that plays video using the ActiveMovie COM stuff.

I'm using the FilgraphManagerClass to play the movie and I'm trying to get
an image from the currently displayed scene.

The call to do it is FilgraphManagerClass.GetCurrentImage(ref int
bufferSize, out int dibImage)

Now, I'm aware that my code, as-is, might not free what it's allocating.
That's okay for the moment. I just want to get it working first, then I'll
get it working cleanly.

unsafe private byte[] GetImageData()
{
byte[] buffer = null;
try
{
int buffSize = 0;
int zero = 0;
fmc.GetCurrentImage(ref buffSize, out zero);

IntPtr tempBuffer = Marshal.AllocHGlobal(buffSize);
int tempBufferInt = tempBuffer.ToInt32();
fmc.GetCurrentImage(ref buffSize, out tempBufferInt);
buffer = new byte[buffSize];
Marshal.Copy((IntPtr) tempBufferInt, buffer, 0, buffSize);
Marshal.FreeHGlobal((IntPtr) tempBufferInt);
}
catch(System.Exception ex)
{
Debug.WriteLine(ex.Message);
}
return buffer;
}

Calling GetCurrentImage with zero for the dibImage, should set the buffSize.
This does not happen, however.

If I skip that first call to GetCurrentImage and instead set buffSize to
say, 10*height*width (which should be more than enough room to store the
image), then on the call to GetCurrentImage causes my application to exit.
No exceptions, no messages in the debug output, the app just ends.

Any ideas?

Pete
Nov 17 '05 #1
2 6577
Pete,

A few things. First, I don't know why you are using unsafe code,
because everything that you are using is safe. Rather, you could declare
the array in unsafe code, fix the pointer to it, and then pass it to the
GetCurrentImage method.

Doing some research, it seems that GetCurrentImage method on the
IBasicVideo2 interface is not supported, so I don't think it is a good idea
to go about using it.

Rather, what you should do is use the DirectShow.NET library to
construct your filter graph (you can render it from a file, which is what I
think you want). You can find the managed direct show library at:

http://directshownet.sourceforge.net/

On top of that, there is a blog entry that I have found which describes
how to get the bitmap bits in C# (it uses an interop library to DirectX, but
I think the sourceforge project is better, and you can easily convert the
code to use that):

http://www.a2ii.com/DasBlog/CommentV...c44c7883c.aspx

I do think that the IMediaDet interface is a little out of date though
(as is the ActiveMovie stuff, btw, it's all DirectShow now). You should be
using the ISampleGrabber interface instead.

There is an example out there that shows how to do this. You can see it
at:

http://www.a2ii.com/tech/directx/tutorialDS6.htm

The link for the code for that tutorial is hard to find, so here it is:

http://www.a2ii.com/tech/directx/tutorialDS6.zip

Note, in that project you will have to remove the reference to
DirectShowLib and replace it with a reference to DirectShowLib that you
downloaded (it's not included in the project).

In the ISampleGrabber.BufferCB method, you can create an instance of a
Bitmap class, using the overload that takes the scan0 parameter. You can
pass the pBuffer parameter into that, and use the video header info (which
has the bitmap header info) information to get the dimensions of the video.

If you are going to save the image, or draw on it, you ^might^ have to
rotate it. If you are going to draw on the image before you save it, then
you should apply a Matrix to the Transform property of the Graphics instance
for the bitmap. Once done drawing, you can call the RotateFlip method to
flip the image (if needed).

Of course, you can always just flip the image before you get the
graphics instance and draw on that. I believe the height property in the
bitmap info header is negative if you need to flip the image.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:aK********************@giganews.com...
I've got an app that plays video using the ActiveMovie COM stuff.

I'm using the FilgraphManagerClass to play the movie and I'm trying to get
an image from the currently displayed scene.

The call to do it is FilgraphManagerClass.GetCurrentImage(ref int
bufferSize, out int dibImage)

Now, I'm aware that my code, as-is, might not free what it's allocating.
That's okay for the moment. I just want to get it working first, then I'll
get it working cleanly.

unsafe private byte[] GetImageData()
{
byte[] buffer = null;
try
{
int buffSize = 0;
int zero = 0;
fmc.GetCurrentImage(ref buffSize, out zero);

IntPtr tempBuffer = Marshal.AllocHGlobal(buffSize);
int tempBufferInt = tempBuffer.ToInt32();
fmc.GetCurrentImage(ref buffSize, out tempBufferInt);
buffer = new byte[buffSize];
Marshal.Copy((IntPtr) tempBufferInt, buffer, 0, buffSize);
Marshal.FreeHGlobal((IntPtr) tempBufferInt);
}
catch(System.Exception ex)
{
Debug.WriteLine(ex.Message);
}
return buffer;
}

Calling GetCurrentImage with zero for the dibImage, should set the
buffSize. This does not happen, however.

If I skip that first call to GetCurrentImage and instead set buffSize to
say, 10*height*width (which should be more than enough room to store the
image), then on the call to GetCurrentImage causes my application to exit.
No exceptions, no messages in the debug output, the app just ends.

Any ideas?

Pete

Nov 17 '05 #2
Nicholas,

GetCurrentImage() is in IBasicVideo, not IBasicVideo2. IBasicVideo2 simply
adds a getter of PrefferedAspectRatio, which I'm not using.

Where do you see that GetCurrentImage is not supported? There's nothing
about it here:
http://tinyurl.com/b46ho

I have DirectShowNet and have used it before. I was just trying to throw
together a quick and easy image capture without wanting to go through all
the hassles that come with DirectShow.

I was just planning on using it for myself, so I don't really care if it's
using out-of-date interfaces.

From what I've seen on the newsgroups, it appears to function (at least for
some people).

But if it's one of those things where it works for some people and not for
others and I'm one of those "others", then I guess I'm stuck with using
DirectShow.

Pete

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:e3**************@TK2MSFTNGP12.phx.gbl...
Pete,

A few things. First, I don't know why you are using unsafe code,
because everything that you are using is safe. Rather, you could declare
the array in unsafe code, fix the pointer to it, and then pass it to the
GetCurrentImage method.

Doing some research, it seems that GetCurrentImage method on the
IBasicVideo2 interface is not supported, so I don't think it is a good
idea to go about using it.

Rather, what you should do is use the DirectShow.NET library to
construct your filter graph (you can render it from a file, which is what
I think you want). You can find the managed direct show library at:

http://directshownet.sourceforge.net/

On top of that, there is a blog entry that I have found which describes
how to get the bitmap bits in C# (it uses an interop library to DirectX,
but I think the sourceforge project is better, and you can easily convert
the code to use that):

http://www.a2ii.com/DasBlog/CommentV...c44c7883c.aspx

I do think that the IMediaDet interface is a little out of date though
(as is the ActiveMovie stuff, btw, it's all DirectShow now). You should
be using the ISampleGrabber interface instead.

There is an example out there that shows how to do this. You can see
it at:

http://www.a2ii.com/tech/directx/tutorialDS6.htm

The link for the code for that tutorial is hard to find, so here it is:

http://www.a2ii.com/tech/directx/tutorialDS6.zip

Note, in that project you will have to remove the reference to
DirectShowLib and replace it with a reference to DirectShowLib that you
downloaded (it's not included in the project).

In the ISampleGrabber.BufferCB method, you can create an instance of a
Bitmap class, using the overload that takes the scan0 parameter. You can
pass the pBuffer parameter into that, and use the video header info (which
has the bitmap header info) information to get the dimensions of the
video.

If you are going to save the image, or draw on it, you ^might^ have to
rotate it. If you are going to draw on the image before you save it, then
you should apply a Matrix to the Transform property of the Graphics
instance for the bitmap. Once done drawing, you can call the RotateFlip
method to flip the image (if needed).

Of course, you can always just flip the image before you get the
graphics instance and draw on that. I believe the height property in the
bitmap info header is negative if you need to flip the image.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:aK********************@giganews.com...
I've got an app that plays video using the ActiveMovie COM stuff.

I'm using the FilgraphManagerClass to play the movie and I'm trying to
get an image from the currently displayed scene.

The call to do it is FilgraphManagerClass.GetCurrentImage(ref int
bufferSize, out int dibImage)

Now, I'm aware that my code, as-is, might not free what it's allocating.
That's okay for the moment. I just want to get it working first, then
I'll get it working cleanly.

unsafe private byte[] GetImageData()
{
byte[] buffer = null;
try
{
int buffSize = 0;
int zero = 0;
fmc.GetCurrentImage(ref buffSize, out zero);

IntPtr tempBuffer = Marshal.AllocHGlobal(buffSize);
int tempBufferInt = tempBuffer.ToInt32();
fmc.GetCurrentImage(ref buffSize, out tempBufferInt);
buffer = new byte[buffSize];
Marshal.Copy((IntPtr) tempBufferInt, buffer, 0, buffSize);
Marshal.FreeHGlobal((IntPtr) tempBufferInt);
}
catch(System.Exception ex)
{
Debug.WriteLine(ex.Message);
}
return buffer;
}

Calling GetCurrentImage with zero for the dibImage, should set the
buffSize. This does not happen, however.

If I skip that first call to GetCurrentImage and instead set buffSize to
say, 10*height*width (which should be more than enough room to store the
image), then on the call to GetCurrentImage causes my application to
exit. No exceptions, no messages in the debug output, the app just ends.

Any ideas?

Pete


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
0
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
0
by: avl | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
2
by: Weston Fryatt | last post by:
(Sorry for spamming multiple groups, But I need a solution to this problem) I think this should be a simple question on Memory Allocation in a managed DLL and passing a memory pointer over to an...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
1
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
5
by: Saya | last post by:
Hi, Please help with the following: How to implement a C++ code like this: MediaFunction( char* filename, void* info, int* size, void** buffer, int first, int* last);
11
by: Joe Martin | last post by:
Has anyone made use of PKWare's PKCDL.DLL for the Implode and Explode functions? I have been able to successfully define everything correctly and even get the invoked function (Explode) to run...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: =?Utf-8?B?QmFydE1hbg==?= | last post by:
Greetings, I am working on a project where the interface from the UI application (done in C#) requires that an image buffer be passed back as a byte array as defined by an interface. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.