Click here to Skip to main content
13,504,202 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to write a text or a label on Preview Window that is showing the webcam preview using directshow.
-> Text will be on window when there will be preview or frames on screen.
Is here anyone having idea about this, how to do it?

Posted 12-Feb-13 1:28am
Sergey Alexandrovich Kryukov 12-Feb-13 11:25am
Are you using GDI, GDI+? It's is not compatible with DirectShow. You would need to do it with DirectX, layout manager, etc. Not a very pleasant work.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

What you normally do is overlay text on a video - you don't need to add a label.
There are some filters that would allow you to do this for e.g. Pure .NET DirectShow Filters in C#[^] describes a filter that adds text to a video.
Sergey Alexandrovich Kryukov 12-Feb-13 12:47pm
Of course, a 5. I explained my opinion in my comment under my answer.
Abhinav S 12-Feb-13 12:57pm
Thanks for the 5.
Read your comment.
Espen Harlinn 12-Feb-13 14:48pm
Very nice, Abhinav :-D
Abhinav S 12-Feb-13 22:02pm
Thank you.
toms from newdelhi 12-Feb-13 23:57pm
Thanks Abhinav!
for your answer, I really appreciate it.
But the thing is I'm implementing my application in VC++.
Its a webcam preview over which I want a text.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Another option is to render bot the video and the text to a texture, and an example on how to render the video to a texture is included with the SDK:
Source: (SDK root)\Samples\C++\DirectShow\Players\Texture3D[^]

To render the text you do something like this:

Enable transparency:
device.RenderState.AlphaTestEnable = true;
device.RenderState.AlphaFunction = Compare.Greater;
device.RenderState.ReferenceAlpha = 0;

Create font and sprite:
D3DX10CreateFont(device, 16, 0, FW_BOLD, 0, FALSE, 
           DEFAULT_PITCH | FF_DONTCARE, L"Verdana", &font);

D3DX10CreateSprite(device, 256, &sprite);

hr = sprite->Begin(0);
std::wstring left = L"Hurray, text on a sprite!";

RECT r; = 0;
r.bottom = window->height;
r.left = 0;
r.right = window->width;

hr = font->DrawTextW(sprite, left.c_str(), -1, &r, 
              DT_LEFT | DT_TOP, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f));
hr = sprite->End();

Best regards
Espen Harlinn
Sergey Alexandrovich Kryukov 13-Feb-13 11:49am
Sure, a 5.
But note that OP successfully implemented my idea based on my quite stupid answer :-)
Because I expected that it could be just enough, and everything else might be too difficult to OP...
Espen Harlinn 13-Feb-13 14:38pm
Thanks, Sergey - I did notice both yours and Abhinavs replies yestrday, and thought this would be an interesting alternative :-D
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question.

Could you simply place a label control over the video? Not fancy, but simple.

Otherwise, dig in DirectX; but it will need time and nerve…

Abhinav S 12-Feb-13 11:49am
I would add an overlay filter instead of a label.
Sergey Alexandrovich Kryukov 12-Feb-13 12:47pm
Of course you would do it; and it would make a neat powerful way of doing things. I just suggest a simplest alternative.
You see, it depends on requirements and what OP is doing. If OP is already into DirectShow, of course it can be done, but if OP is just using some ready to use DirectShow component, which is the most usual case, the label is simpler.
toms from newdelhi 12-Feb-13 23:59pm
Its an app that is being done in VC++ win32 environment.
So put_Caption is failed,
Textout is failed and childwindow is also failed.
That's why the thing is how to write a simple text over preview.
This and that failed is... not informative...
toms from newdelhi 13-Feb-13 1:03am
in directshow its a IVIDEOWINDOW and after render we can put caption there as I have seen from few examples and something like this, but its not working.
And Textout is to call some text on paint event of window but that's not working too.
I tried to explain: this won't work, because this is GDI. You need to follow Solution 1 or Solution 2. What's not clear?
In my method, you don't touch DirectShow at all, use a separate (Windowed) control on top. Or dig into DirectX...
toms from newdelhi 13-Feb-13 1:19am
I got your point and I'm agree with this.
But for webcam preview how can I switch to Directx only whether preview is only seen in directshow that's a part of MS-SDK.
What do you mean "switch"? It looks like you already use DirectX. If does not matter what happens in this window. You create some windows with valid HWND, and then you "give it out" to DirectX. You cannot control what happens in it by Windows API. When you try render some GDI image, you see it for a while, but next video frame removes it, right? This is the whole problem: these two APIs fight against each other. As video is always DirectX (because this is DirectShow), you can only use DirectX to do something else. But my method is a total work-around: you ignore the problem and don't touch the windows with video at all. Preview or not, it does not matter.
By the way, to best of my knowledge, video other than DirectX is not practically used in Windows.
toms from newdelhi 13-Feb-13 1:47am
Now I got it...
I'm trying this and after this I'll return on discussion.
toms from newdelhi 13-Feb-13 4:36am
@Sergey, Thanks for your solution it worked.
But there is a problem when i resize the window the child window disappears.

Have you any Idea?
Sergey Alexandrovich Kryukov 13-Feb-13 11:28am
Could be different problem, might be as simple as layout. First thing is to run it under the debugger and check up the coordinates/size of the hidden window. If you cannot sort it out, you will have to reproduce the problem on some minimal code sample. It might be not so easy, but chances are, you might detect the problem while simplifying code. In not, you can show this sample. It should be a separate question.
toms from newdelhi 13-Feb-13 22:52pm
Its done finally....
Sergey Alexandrovich Kryukov 13-Feb-13 23:00pm
Good luck, call again.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Additionally, to all answers:

1. In my articles there is an example to drawing text over the video, yes it is in C# but in old DirectX SDK you can find similar examples in C++.

2. Another way you can capture frames with Sample Grabber and draw text this way.

3. Easy way is to use VMR and makes bitmap from text and draw it as overlay Ticker Sample[^].

4. You can use RenderToTexture example but easy is to make Custom Allocator presenter for VMR[^] and draw text in there. The example of that you can see on MSDN or in C# in my articles.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100