I tried to recreate the BitmapMixer sample from Directshow.NET in VS 2010 (The original sample works fine) and got the following errors in the Form1.cs
Error 1 The name 'MainForm_ResizeMove' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 177 13 myBitmapMixer
Error 2 The name 'MainForm_Paint' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 289 49 myBitmapMixer
Error 3 The name 'MainForm_ResizeMove' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 290 45 myBitmapMixer
Error 4 The name 'MainForm_ResizeMove' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 291 43 myBitmapMixer
Error 5 The name 'SystemEvents_DisplaySettingsChanged' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 292 69 myBitmapMixer
Error 6 The name 'MainForm_Paint' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 300 49 myBitmapMixer
Error 7 The name 'MainForm_ResizeMove' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 301 45 myBitmapMixer
Error 8 The name 'MainForm_ResizeMove' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 302 43 myBitmapMixer
Error 9 The name 'SystemEvents_DisplaySettingsChanged' does not exist in the current context C:\Documents and Settings\TLNA\my documents\visual studio 2010\Projects\mybitmapMixer\myBitmapMixer\Form1.cs 303 69 myBitmapMixer
Here is the Form1.cs code :-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;
using DirectShowLib;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.Win32;
namespace DirectShowLib.Sample
{
public partial class MainForm : Form
{
//private System.ComponentModel.Container components = null;
private Color colorKey = Color.Violet; // The color use as ColorKey for GDI operations
private Bitmap colorKeyBitmap; // A RGB bitmap used for GDI operations.
private Bitmap alphaBitmap; // A ARGB bitmap used for Direct3D operations
// Managed Direct3D magic number to retrieve unmanaged Direct3D interfaces
private const int DxMagicNumber = -759872593;
private Device device = null; // A Managed Direct3D device
private PresentParameters presentParams;
private Surface surface = null; // A Direct3D suface filled with alphaBitmap
private IntPtr unmanagedSurface; // A pointer on the unmanaged surface
private IFilterGraph2 graphBuilder = null;
private IMediaControl mediaContr开发者_如何学Gool = null;
private IBaseFilter vmr9 = null;
private IVMRMixerBitmap9 mixerBitmap = null;
private IVMRWindowlessControl9 windowlessCtrl = null;
private bool handlersAdded = false; // Needed to remove delegates
// Menus stuff
private bool mixerEnabled = true;
private bool usingGDI = true;
public MainForm()
{
InitializeComponent();
// We paint the windows ourself
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
// Get the colorkeyed bitmap without antialiasing
colorKeyBitmap = BitmapGenerator.GenerateColorKeyBitmap(colorKey, false);
// Get the bitmap with alpha transparency
alphaBitmap = BitmapGenerator.GenerateAlphaBitmap();
// Init Managed Direct3D
InitializeDirect3D();
}
private void InitializeDirect3D()
{
Device.IsUsingEventHandlers = false;
// Basic Presentation Parameters...
presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
// Assume a hardware Direct3D device is available
// Add MultiThreaded to be safe. Each DirectShow filter runs in a separate thread...
device = new Device(
0,
DeviceType.Hardware,
this,
CreateFlags.SoftwareVertexProcessing | CreateFlags.MultiThreaded,
presentParams
);
// Create a surface from our alpha bitmap
surface = new Surface(device, alphaBitmap, Pool.SystemMemory);
// Get the unmanaged pointer
unmanagedSurface = surface.GetObjectByValue(DxMagicNumber);
}
private void CloseInterfaces()
{
if (mediaControl != null)
mediaControl.Stop();
if (handlersAdded)
RemoveHandlers();
if (vmr9 != null)
{
Marshal.ReleaseComObject(vmr9);
vmr9 = null;
windowlessCtrl = null;
mixerBitmap = null;
}
if (graphBuilder != null)
{
Marshal.ReleaseComObject(graphBuilder);
graphBuilder = null;
mediaControl = null;
}
menuMixer.Enabled = false;
}
private void BuildGraph(string fileName)
{
int hr = 0;
try
{
graphBuilder = (IFilterGraph2)new FilterGraph();
mediaControl = (IMediaControl)graphBuilder;
vmr9 = (IBaseFilter)new VideoMixingRenderer9();
ConfigureVMR9InWindowlessMode();
hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9");
DsError.ThrowExceptionForHR(hr);
hr = graphBuilder.RenderFile(fileName, null);
DsError.ThrowExceptionForHR(hr);
mixerBitmap = (IVMRMixerBitmap9)vmr9;
menuMixer.Enabled = true;
mixerEnabled = true;
usingGDI = false;
UpdateMixerMenu();
SetMixerSettings();
}
catch (Exception e)
{
CloseInterfaces();
MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ConfigureVMR9InWindowlessMode()
{
int hr = 0;
IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9)vmr9;
// Not really needed for VMR9 but don't forget calling it with VMR7
hr = filterConfig.SetNumberOfStreams(1);
DsError.ThrowExceptionForHR(hr);
// Change VMR9 mode to Windowless
hr = filterConfig.SetRenderingMode(VMR9Mode.Windowless);
DsError.ThrowExceptionForHR(hr);
windowlessCtrl = (IVMRWindowlessControl9)vmr9;
// Set "Parent" window
hr = windowlessCtrl.SetVideoClippingWindow(this.Handle);
DsError.ThrowExceptionForHR(hr);
// Set Aspect-Ratio
hr = windowlessCtrl.SetAspectRatioMode(VMR9AspectRatioMode.LetterBox);
DsError.ThrowExceptionForHR(hr);
// Add delegates for Windowless operations
AddHandlers();
// Call the resize handler to configure the output size
MainForm_ResizeMove(null, null);
}
private void SetMixerSettings()
{
int hr = 0;
VMR9AlphaBitmap alphaBmp;
if (!mixerEnabled) // Did the user disable the bitmap ?
{
// Get current Alpha Bitmap Parameters
hr = mixerBitmap.GetAlphaBitmapParameters(out alphaBmp);
DsError.ThrowExceptionForHR(hr);
// Disable them
alphaBmp.dwFlags = VMR9AlphaBitmapFlags.Disable;
// Update the Alpha Bitmap Parameters
hr = mixerBitmap.UpdateAlphaBitmapParameters(ref alphaBmp);
DsError.ThrowExceptionForHR(hr);
return;
}
if (usingGDI)
{
// Old school GDI stuff...
Graphics g = Graphics.FromImage(colorKeyBitmap);
IntPtr hdc = g.GetHdc();
IntPtr memDC = NativeMethodes.CreateCompatibleDC(hdc);
IntPtr hBitmap = colorKeyBitmap.GetHbitmap();
NativeMethodes.SelectObject(memDC, hBitmap);
// Set Alpha Bitmap Parameters for using a GDI DC
alphaBmp = new VMR9AlphaBitmap();
alphaBmp.dwFlags = VMR9AlphaBitmapFlags.hDC | VMR9AlphaBitmapFlags.SrcColorKey | VMR9AlphaBitmapFlags.FilterMode;
alphaBmp.hdc = memDC;
alphaBmp.rSrc = new DsRect(0, 0, colorKeyBitmap.Size.Width, colorKeyBitmap.Size.Height);
alphaBmp.rDest = GetDestRectangle();
alphaBmp.clrSrcKey = ColorTranslator.ToWin32(colorKey);
alphaBmp.dwFilterMode = VMRMixerPrefs.PointFiltering;
alphaBmp.fAlpha = 0.75f;
// Set Alpha Bitmap Parameters
hr = mixerBitmap.SetAlphaBitmap(ref alphaBmp);
DsError.ThrowExceptionForHR(hr);
// Release GDI handles
NativeMethodes.DeleteObject(hBitmap);
NativeMethodes.DeleteDC(memDC);
g.ReleaseHdc(hdc);
g.Dispose();
}
else // Using a Direct3D surface
{
// Set Alpha Bitmap Parameters for using a Direct3D surface
alphaBmp = new VMR9AlphaBitmap();
alphaBmp.dwFlags = VMR9AlphaBitmapFlags.EntireDDS;
alphaBmp.pDDS = unmanagedSurface;
alphaBmp.rDest = GetDestRectangle();
alphaBmp.fAlpha = 1.0f;
// Note : Alpha values from the bitmap are cumulative with the fAlpha parameter.
// Example : texel alpha = 128 (50%) & fAlpha = 0.5f (50%) = effective alpha : 64 (25%)
// Set Alpha Bitmap Parameters
hr = mixerBitmap.SetAlphaBitmap(ref alphaBmp);
DsError.ThrowExceptionForHR(hr);
}
}
private NormalizedRect GetDestRectangle()
{
int hr = 0;
int width, height, arW, arH;
NormalizedRect rect = new NormalizedRect();
hr = windowlessCtrl.GetNativeVideoSize(out width, out height, out arW, out arH);
DsError.ThrowExceptionForHR(hr);
// Position the bitmap in the middle of the video stream.
if (width >= height)
{
rect.top = 0.0f;
rect.left = (1.0f - ((float)height / (float)width)) / 2;
rect.bottom = 1.0f;
rect.right = rect.left + (float)height / (float)width;
}
else
{
rect.top = (1.0f - ((float)width / (float)height)) / 2;
rect.left = 0.0f;
rect.right = rect.top + (float)width / (float)height;
rect.bottom = 1.0f;
}
return rect;
}
private void UpdateMixerMenu()
{
// Just manage the Mixer menu...
menuMixerEnable.Checked = mixerEnabled;
menuMixerGDI.Enabled = mixerEnabled;
menuMixerGDI.Checked = usingGDI;
menuMixerD3D.Enabled = mixerEnabled;
menuMixerD3D.Checked = !usingGDI;
}
private void AddHandlers()
{
// Add handlers for VMR purpose
this.Paint += new PaintEventHandler(MainForm_Paint); // for WM_PAINT
this.Resize += new EventHandler(MainForm_ResizeMove); // for WM_SIZE
this.Move += new EventHandler(MainForm_ResizeMove); // for WM_MOVE
SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged); // for WM_DISPLAYCHANGE
handlersAdded = true;
}
private void RemoveHandlers()
{
// remove handlers when they are no more needed
handlersAdded = false;
this.Paint -= new PaintEventHandler(MainForm_Paint);
this.Resize -= new EventHandler(MainForm_ResizeMove);
this.Move -= new EventHandler(MainForm_ResizeMove);
SystemEvents.DisplaySettingsChanged -= new EventHandler(SystemEvents_DisplaySettingsChanged);
}
private void RunGraph()
{
if (mediaControl != null)
{
int hr = mediaControl.Run();
DsError.ThrowExceptionForHR(hr);
}
}
private void StopGraph()
{
if (mediaControl != null)
{
int hr = mediaControl.Stop();
DsError.ThrowExceptionForHR(hr);
}
}
// Menus click handlers
private void menuFileOpen_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
CloseInterfaces();
BuildGraph(openFileDialog.FileName);
RunGraph();
}
}
private void menuFileClose_Click(object sender, EventArgs e)
{
StopGraph();
CloseInterfaces();
this.Invalidate();
}
private void menuFileExit_Click(object sender, EventArgs e)
{
StopGraph();
CloseInterfaces();
this.Dispose();
}
private void menuMixerEnable_Click(object sender, EventArgs e)
{
mixerEnabled = !mixerEnabled;
UpdateMixerMenu();
SetMixerSettings();
}
private void menuMixerGDI_Click(object sender, EventArgs e)
{
usingGDI = true;
UpdateMixerMenu();
SetMixerSettings();
}
private void menuMixerD3D_Click(object sender, EventArgs e)
{
usingGDI = false;
UpdateMixerMenu();
SetMixerSettings();
}
private void menuHelpAbout_Click(object sender, EventArgs e)
{
string title = "About Bitmap mixer";
string text = "DirectShow.Net VMR9 Bitmap mixer\r\nMake use of IVMRBitmapMixer9";
AboutBox.Show(title, text);
}
}
}
Any help to resolve this is appreciated.
Many Thanks
It looks like your form doesn't have the event handler functions declared and implemented.
When you do:
this.Paint += new PaintEventHandler(MainForm_Paint);
You are attaching an event handler using MainForm_Paint to the Paint event.
So, when the paint event is fired every time your form needs to paint itself, the function MainForm_Paint should be fired as well.
You need to put in your form a definition for MainForm_Paint, for instance:
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
// Make a big red pen.
Pen p= new Pen(Color.Red, 7);
g.DrawLine(p, 1, 1, 100, 100);
}
And the same goes for the other methods (MainForm_ResizeMove, etc.).
精彩评论