Im trying to pass a variable from Form1 to a new class but it dosent work good how can i repair it

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
IN Form1 i did:

<pre class="prettyprint public string test
{
get { return label2.Text; }
set { label2.Text = value; }
}
[/code]
I want to update in form1 designer the variable label2.Text with a variable in the new class.
In the new class i did in the top of the class:

<pre class="prettyprint private AnimationEditor.Form1 f1;[/code]

And then in the event i need i did:

<pre class="prettyprint int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
if (SaveToDisc)
{
String tempFile = _outFolder + _frameId + ".bmp";
if (File.Exists(tempFile))
{
f1.test = _frameId.ToString();
}
else
{
bitmap.Save(Path.Combine(_outFolder, _frameId + ".bmp"));
}
_frameId++;
}
else
{
if (Images == null)
Images = new List<Bitmap>();
Images.Add((Bitmap)bitmap.Clone());
}
}
return 0;
}[/code]
This is the line i added: f1.test = _frameId.ToString();
The problem is that f1 never assigned so its all the time null. I tried to make an instance for the Form1 in the top of the new class when i click in Form1 on a button and its calling to the new class its doing the Form1 instance but then its jumping to
the Form1 and do all the Form1 over again it dosent continue with the new class.

Thats in Form1 on the button click where i start the new class:

<pre class="prettyprint private void button5_Click(object sender, EventArgs e)
{
wmv = new Polkan.DataSource.WmvAdapter(@"d:VIDEO0040.3gp", sf); wmv.SaveToDisc = true;
wmv.Start();
wmv.WaitUntilDone();
} [/code]
So i tried to make an instance in the new class for the Form1 in the wmv.Start() event but then its going and making the Form1 again and dosent continue with the new class.

This is the code of the new class:

<pre class="prettyprint using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using DirectShowLib;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Polkan.DataSource
{
internal class WmvAdapter : ISampleGrabberCB, IDisposable
{
#region Fields
//public Image img;
private IFilterGraph2 _filterGraph;
private IMediaControl _mediaCtrl;
private IMediaEvent _mediaEvent;
private int _width;
private int _height;
private readonly string _outFolder;
private int _frameId;
private AnimationEditor.Form1 f1;

#endregion

#region Constructors and Destructors

public WmvAdapter(string file, string outFolder)
{
_outFolder = outFolder;
try
{
SetupGraph(file);
}
catch
{
Dispose();
throw;
}
}

~WmvAdapter()
{
CloseInterfaces();
}

#endregion

public void Dispose()
{
CloseInterfaces();
}

public void Start()
{
int hr = _mediaCtrl.Run();
DsError.ThrowExceptionForHR(hr);
}

public void WaitUntilDone()
{
int hr;
//const int eAbort = unchecked((int)0x80004004);


EventCode evCode;
hr = _mediaEvent.WaitForCompletion(100, out evCode);
}

/// <summary> build the capture graph for grabber. </summary>
private void SetupGraph(string file)
{
ISampleGrabber sampGrabber = null;
IBaseFilter capFilter = null;
IBaseFilter nullrenderer = null;

_filterGraph = (IFilterGraph2)new FilterGraph();
_mediaCtrl = (IMediaControl)_filterGraph;
_mediaEvent = (IMediaEvent)_filterGraph;

var mediaFilt = (IMediaFilter)_filterGraph;

try
{
// Add the video source
int hr = _filterGraph.AddSourceFilter(file, "Ds.NET FileFilter", out capFilter);
DsError.ThrowExceptionForHR(hr);

// Get the SampleGrabber interface
sampGrabber = new SampleGrabber() as ISampleGrabber;
var baseGrabFlt = sampGrabber as IBaseFilter;

ConfigureSampleGrabber(sampGrabber);

// Add the frame grabber to the graph
hr = _filterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
DsError.ThrowExceptionForHR(hr);

// ---------------------------------
// Connect the file filter to the sample grabber

// Hopefully this will be the video pin, we could check by reading its mediatype
IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0);

// Get the input pin from the sample grabber
IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

hr = _filterGraph.Connect(iPinOut, iPinIn);
DsError.ThrowExceptionForHR(hr);

// Add the null renderer to the graph
nullrenderer = new NullRenderer() as IBaseFilter;
hr = _filterGraph.AddFilter(nullrenderer, "Null renderer");
DsError.ThrowExceptionForHR(hr);

// ---------------------------------
// Connect the sample grabber to the null renderer

iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);
iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0);

hr = _filterGraph.Connect(iPinOut, iPinIn);
DsError.ThrowExceptionForHR(hr);

// Turn off the clock. This causes the frames to be sent
// thru the graph as fast as possible
hr = mediaFilt.SetSyncSource(null);
DsError.ThrowExceptionForHR(hr);

// Read and cache the image sizes
SaveSizeInfo(sampGrabber);
}
finally
{
if (capFilter != null)
{
Marshal.ReleaseComObject(capFilter);
}
if (sampGrabber != null)
{
Marshal.ReleaseComObject(sampGrabber);
}
if (nullrenderer != null)
{
Marshal.ReleaseComObject(nullrenderer);
}
GC.Collect();
}
}

private void SaveSizeInfo(ISampleGrabber sampGrabber)
{
// Get the media type from the SampleGrabber
var media = new AMMediaType();
int hr = sampGrabber.GetConnectedMediaType(media);
DsError.ThrowExceptionForHR(hr);

if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
{
throw new NotSupportedException("Unknown Grabber Media Format");
}

// Grab the size info
var videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
_width = videoInfoHeader.BmiHeader.Width;
_height = videoInfoHeader.BmiHeader.Height;

DsUtils.FreeAMMediaType(media);
GC.Collect();
}

private void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
{
var media = new AMMediaType
{
majorType = MediaType.Video,
subType = MediaSubType.RGB24,
formatType = FormatType.VideoInfo
};
int hr = sampGrabber.SetMediaType(media);
DsError.ThrowExceptionForHR(hr);

DsUtils.FreeAMMediaType(media);
GC.Collect();
hr = sampGrabber.SetCallback(this, 1);
DsError.ThrowExceptionForHR(hr);
}

private void CloseInterfaces()
{
try
{
if (_mediaCtrl != null)
{
_mediaCtrl.Stop();
_mediaCtrl = null;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}

if (_filterGraph != null)
{
Marshal.ReleaseComObject(_filterGraph);
_filterGraph = null;
}
GC.Collect();
}

int ISampleGrabberCB.SampleCB(double sampleTime, IMediaSample pSample)
{
Marshal.ReleaseComObject(pSample);
return 0;
}

//add a boolean property to indicate the save-mode
public bool SaveToDisc { get; set; }
//the list for the bitmaps
public List<Bitmap> Images { get; set; }

int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
if (SaveToDisc)
{
String tempFile = _outFolder + _frameId + ".bmp";
if (File.Exists(tempFile))
{
f1.test = _frameId.ToString();
}
else
{
bitmap.Save(Path.Combine(_outFolder, _frameId + ".bmp"));
}
_frameId++;
}
else
{
if (Images == null)
Images = new List<Bitmap>();
Images.Add((Bitmap)bitmap.Clone());
}
}
return 0;
}


/*int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
MemoryStream myStream = new MemoryStream(1000000);
bitmap.Save(myStream, ImageFormat.Bmp);
//bitmap.Save(Path.Combine(_outFolder, _frameId + ".bmp"));
img = new Bitmap(myStream);

}
return 0;
}*/
}
}
[/code]
<br/>
All i want is to update label2 in Form1 with the progress of _frameD int variable in the new class.

View the full article
 
Back
Top