Show only small area of the video with directshow - c#

I have a video graph in c# with DirectShow.
Now I want to show all video sources with its preview. But it should not adjust the video area to the size of the panel.
Currently it shows me the video on the panel, but it adjust the size of the video proportional into the panel.
I want to show only a single area of the video in this panel. For example this picture: http://www.cnet.de/i/story_media/41557373/weitwinkel.jpg
If this would be my video and the smallest area on it would be the size of my panel. I don't want to fit the whole video in my panel size, it should only show a small part of the video.
My code is:
//get the video window from the graph
IVideoWindow videoWindow2 = (IVideoWindow)_graph;
//Set the owner of the videoWindow to an IntPtr of some sort (the Handle of any control - could be a form / button etc.)
int hr = videoWindow2.put_Owner(panel.Handle);
panel is of type Panel.

The solution is to use SetWindowPosition of IVideoWindow.
//get the real video width
hr1 = videoWindow2.get_Width(out videoWidth);
DsError.ThrowExceptionForHR(hr1);
//get the real video height
hr1 = videoWindow2.get_Height(out videoHeight);
DsError.ThrowExceptionForHR(hr1);
//calculate the width when setting the height to the panel height
videoWidthF = (float)videoWidth;
videoHeightF = (float)videoHeight;
panelWidthF = (float)panelWidth;
panelHeightF = (float)panelHeight;
// calculate the margins
int margin = (int)(((panelHeightF / videoHeightF*videoWidthF) - panelWidthF) / 2);
// Position video window in client rect of main application window
hr1 = videoWindow2.SetWindowPosition(-margin, 0, (int)(panelHeightF / videoHeightF * videoWidthF), panel.Height);

Take a look at using windowless mode of the VMR.
IVMRWindowlessControl9::SetVideoPosition is what you are looking for. A quick google search would provide samples.

Related

How to change the aspect ratio of an Windows media player by using C#

Watching a video on windows media player will produce black bars on the left and right.
this.MPlayer.stretchToFit = true;
AxWindowsMediaPlayer mPlayer = this.MPlayer;
mPlayer.URL = "Location of a file";
Below attached file output of the code.
How to change the aspect ratio of an Windows media player?
With C# images you define width and height as double. When you now display your image you can choose between the following stretch options:
Stretch="Fill" - depending on your width and height the image can be
distortedor Stretch="None" - depending on your width and height the
image may not fit into the display area Stretch="Uniform" - the image
stretches into the display area and completely fills the display area
you might have some empty space at upper/lower or left/right border. The aspect ratio is not changed Stretch = "UniformToFill" -
the image stretches and completely fills the display area, the aspect
ratio remains intact however to fit into the display area the lower
or right part of the imace is cut off.
For the MediaElement first try Stretch="None", this leaves the display undistorted.
If your video absolutely does not fit into the display area replace the Stretch argument
by SizeToContent="WidthAndHeight".
Regards Martin

how to calculate the coordinates of the free space on the screen?

I would like to post notification forms but I do not want the forms to overlap. How do I determine the free space on the screen to display the lab form?
Sorry for the bad english!
You can get screen size for primary screen like this:
System.Drawing.Rectangle workingRectangle =
Screen.PrimaryScreen.WorkingArea;
The working area is the desktop area of the display, excluding taskbars, docked windows, and docked tool bars.
For multi-monitor configurations you can enumerate screens using Screen.AllScreens
After you have screen space you can find out free space for each side of your form:
int availableSpaceOnLeft = yourForm.Left - workingRectangle.Left;
int availableSpaceOnTop = yourForm.Top- workingRectangle.Top;
int availableSpaceOnRight = workingRectangle.Right - yourForm.Right;
int availableSpaceOnBottom = workingRectangle.Bottom - yourForm.Bottom;

Getting "image" coordinates of picturebox in zoom mode

I am using a zoomed picturebox. I want to retrieve the image Top-Left and Bottom-Right coordinates. But it is different from that of picturebox when the aspect ratio of the image doesn't match the picturebox. I wonder how I can get the image coordinates on the form.
Minus the Image size divided by 2 from the PictureBoxsize plus the Image size again.
This uses the Size.Subtract Method (Size, Size). MSDN
Size sizestep1 = Size.Subtract(new Size(PictureBox1.Image.Size.Width / 2, PictureBox1.Image.Size.Height / 2), PictureBox1.Size);
Size finalsize = Size.Add(sizestep1, PictureBox1.Image.Size);
// Convert to point.
Point BottomRightCoords = new Point(finalsize.Width, finalsize.Height);
And if you want to get the BottomRightCoords on the form, you have to add the PictureBox Location to it.
A little bit of mathematics suggested above + the code in the following link did the trick:
How to retrieve zoom factor of a WinForms PictureBox?

Window form size adjustment

I need to adjust my form according some size..I want that the form always will be half size of the screen, no matter what the size of the screen.
Always when I put some controller into the form and maximize/change the size of the form all the controllers get crazy and they are not in the order I put..
How can I know what the screen size..?
To get the screen size:
Screen.FromControl(yourForm).Bounds;
or, if you only want the working area only (minus taskbar, docked toolbars, docked windows, etc.):
Screen.FromControl(yourForm).WorkingArea;
There is a class Screen which you can use to get certain display information from. If you want to get the screen you can use:
Rectangle screen = Screen.PrimaryScreen.Bounds;
and then from that you could get the height and width by doing:
int height = screen.Height;
int width = screen.Width;

Going fullscreen without stretching in an XNA game

I've got a 2D game that I'm working on that is in 4:3 aspect ratio. When I switch it to fullscreen mode on my widescreen monitor it stretches. I tried using two viewports to give a black background to where the game shouldn't stretch to, but that left the game in the same size as before. I couldn't get it to fill the viewport that was supposed to hold the whole game.
How can I get it to go fullscreen without stretching and without me needing to modify every position and draw statement in the game? The code I'm using for the viewports is below.
// set the viewport to the whole screen
GraphicsDevice.Viewport = new Viewport
{
X = 0,
Y = 0,
Width = GraphicsDevice.PresentationParameters.BackBufferWidth,
Height = GraphicsDevice.PresentationParameters.BackBufferHeight,
MinDepth = 0,
MaxDepth = 1
};
// clear whole screen to black
GraphicsDevice.Clear(Color.Black);
// figure out the largest area that fits in this resolution at the desired aspect ratio
int width = GraphicsDevice.PresentationParameters.BackBufferWidth;
int height = (int)(width / targetAspectRatio + .5f);
if (height > GraphicsDevice.PresentationParameters.BackBufferHeight)
{
height = GraphicsDevice.PresentationParameters.BackBufferHeight;
width = (int)(height * targetAspectRatio + .5f);
}
//Console.WriteLine("Back: Width: {0}, Height: {0}", GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight);
//Console.WriteLine("Front: Width: {0}, Height: {1}", width, height);
// set up the new viewport centered in the backbuffer
GraphicsDevice.Viewport = new Viewport
{
X = GraphicsDevice.PresentationParameters.BackBufferWidth / 2 - width / 2,
Y = GraphicsDevice.PresentationParameters.BackBufferHeight / 2 - height / 2,
Width = width,
Height = height,
MinDepth = 0,
MaxDepth = 1
};
GraphicsDevice.Clear(Color.CornflowerBlue);
The image below shows what the screen looks like. The black on the sides is what I want (and is from the first viewport) and the second viewport is the game and the cornflower blue area. What I want is to get the game to scale to fill the cornflower blue area.
Use a viewport http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport_members.aspx
As is also the case in commercial games, you should provide an option to the user that allows them to switch between 4:3 aspect and 16:9 aspect. You should be able to just modify the camera viewing ratio accordingly.
EDIT:
As far as I have seen, there are no games that 'auto-detect' the proper aspect ratio to use.
As has been pointed out, there are ways to make a good guess as to what the proper aspect ratio is. If XNA allows you to get at the current Windows user's screen settings data, you can determine an aspect ratio based off of the monitor resolution.
Once you have determined the monitor resolution of the user, you can best decide how to deal with it. At first, the best bet may be to just put black bars on the left/right side of the screen to allow full-screen with a 16:9 aspect ratio that is essentially still using the 4:3 artwork.
Eventually you could modify the game so that it changes the viewing port size when the aspect ratio is 16:9. This wouldn't require changing any art assets, just how they are being rendered.
First of all I'm assuming you're talking about XNA 4.0, which AFAIK there are breaking changes between XNA 3.x and XNA 4.0.
I'm relatively new at XNA, however it seems to me that your assets does not fit the size of the window. Let's say that your game are is 320x240 and your window is bigger e.g. 640x480.
Thus you can specify PreferredBuffer in order to scale up your application. So, tell to XNA you are going to use 320x240 by setting the following values;
_graphics.PreferredBackBufferWidth = 320;
_graphics.PreferredBackBufferHeight = 240;
Additionally you can start fullscreen mode by setting:
_graphics.IsFullScreen = true;
Also, you have to handle manually the how the items should change their size once the Window has changed their size.
Checkout my sample at.
https://github.com/hmadrigal/xnawp7/tree/master/XNASample02
(BTW, you can press F11 to switch between fullscreen and normal view)
Best regards,
Herber
I'm not sure if you can actually scale your view port like that. I understand what you're trying to do, but to do it you'd have to do the following.
Set your screen backbuffer width and height to the 16:9 resolution.
Program in the displacement so that objects didn't draw in those borders.
The thing is, all major games these days, if you play them on a 16:9 monitor and select a 4:3 resolution, will stretch to fit the screen. This isn't something you usually want to overcome. You either support many resolutions in your game, or you will get stretching when a user uses the wrong resolution for his or her screen type.
Usually, one sets up their game, and their textures to work based on the relative dimensions of the current viewport or backbuffer width and height. This way, regardless of the resolution inputted, the game scales to work with that width/height ratio.
It's a bit more work, but in the end, makes your game far more polished and compatible with a wide array of systems.
The only time this may not be done is if the app runs in a window (NOT fullscreen).

Categories