Im building an Add-In for Outlook where I copy all exchange users from the global address list to the local contacts.
The problem is I want transfer the picture of the exchange user too, but exchUser.GetPicture() returns a stdole.stdPicture and I have not yet found a working solution to download or convert it into an image/jpg/...
Here the code to get the exchange User from the global address list:
private void EnumerateGAL()
{
Outlook.AddressList gal = Application.Session.GetGlobalAddressList();
if (gal != null)
{
for (int i = 1; i <= gal.AddressEntries.Count - 1; i++)
{
Outlook.AddressEntry addrEntry = gal.AddressEntries[i];
Outlook.ExchangeUser exchUser = addrEntry.GetExchangeUser();
if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
&& exchUser.CompanyName == "")
{
CreateContact(exchUser);
//exchUser.GetPicture() returns stdole.stdPicture
}
}
}
return;
}
The closest solution I found, was a conversion of stdole.IPictureDisp which returns a bitmap but IPuctureDisp and stdPicture isn´t the same as I read somewhere.
public static System.Drawing.Image ConvertPicture(stdole.IPictureDisp image)
{
int type = image.Type;
if (type == 1)
{
IntPtr hPal = (IntPtr)image.hPal;
return Image.FromHbitmap((IntPtr)image.Handle, hPal);
}
return null;
}
In the end I need to download the picture because I can only upload a picture to a contact with a path.
So, is there a way to download a stdPicture or convert it to be able to download it?
There are four main ways to get the job done.
The "traditional" approach is to use the GetIPictureDispFromPicture and GetPictureFromIPicture methods in the System.Windows.Forms.AxHost class. These are both protected members of the class, so you can't use them externally. For this reason, it is common to subclass the AxHost class and expose public methods that internally call the base class protected methods. This approach allows you to convert in both directions:
internal class AxHostConverter : AxHost
{
private AxHostConverter() : base("") { }
static public stdole.IPictureDisp ImageToPictureDisp(Image image)
{
return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
}
static public Image PictureDispToImage(stdole.IPictureDisp pictureDisp)
{
return GetPictureFromIPicture(pictureDisp);
}
}
Your second option is to use OleLoadPicture or OleCreatePictureIndirect. There's an old support article on the topic here. OleLoadPicture creates a new picture object and initializes it from the contents of a stream.
internal class OleCreateConverter
{
[DllImport("oleaut32.dll", EntryPoint = "OleCreatePictureIndirect",
CharSet = CharSet.Ansi, ExactSpelling = true, PreserveSig = true)]
private static extern int OleCreatePictureIndirect(
[In] PictDescBitmap pictdesc, ref Guid iid, bool fOwn,
[MarshalAs(UnmanagedType.Interface)] out object ppVoid);
const short _PictureTypeBitmap = 1;
[StructLayout(LayoutKind.Sequential)]
internal class PictDescBitmap
{
internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PictDescBitmap));
internal int pictureType = _PictureTypeBitmap;
internal IntPtr hBitmap = IntPtr.Zero;
internal IntPtr hPalette = IntPtr.Zero;
internal int unused = 0;
internal PictDescBitmap(Bitmap bitmap)
{
this.hBitmap = bitmap.GetHbitmap();
}
}
public static stdole.IPictureDisp ImageToPictureDisp(Image image)
{
if (image == null || !(image is Bitmap))
{
return null;
}
PictDescBitmap pictDescBitmap = new PictDescBitmap((Bitmap)image);
object ppVoid = null;
Guid iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;
OleCreatePictureIndirect(pictDescBitmap, ref iPictureDispGuid, true, out ppVoid);
stdole.IPictureDisp picture = (stdole.IPictureDisp)ppVoid;
return picture;
}
public static Image PictureDispToImage(stdole.IPictureDisp pictureDisp)
{
Image image = null;
if (pictureDisp != null && pictureDisp.Type == _PictureTypeBitmap)
{
IntPtr paletteHandle = new IntPtr(pictureDisp.hPal);
IntPtr bitmapHandle = new IntPtr(pictureDisp.Handle);
image = Image.FromHbitmap(bitmapHandle, paletteHandle);
}
return image;
}
}
Your third option is to use the VB6 compatibility library, documented here. To use this, you'll need to add a reference to Microsoft.VisualBasic.Compatibility.dll, which is listed on the .NET tab of the Add References dialog (it resides in the GAC). Then, you can use the ImageToIPictureDisp and IPictureDispToImage methods in the Support class. This is obviously by far the simplest approach, although it does pull in the VB6 compatibility DLL. Internally, the VB6 compatibility code looks a lot like the second option above – using OleCreatePictureIndirect.
using Microsoft.VisualBasic.Compatibility.VB6;
internal class VB6CompatibilityConverter
{
public static stdole.IPictureDisp ImageToPictureDisp(Image image)
{
return (stdole.IPictureDisp)Support.ImageToIPictureDisp(image);
}
public static Image PictureDispToImage(stdole.IPictureDisp pictureDisp)
{
return Support.IPictureDispToImage(pictureDisp);
}
}
Finally, you can implement IPictureDisp and IPicture yourself. This is fine if you just want to convert from an Image to an IPictureDisp, but doesn't help you converting in the other direction. The implementation below relies on the Image actually being a derived Bitmap type, because we call Bitmap.GetHbitmap internally. If you want to keep the support to the generic Image type, you'll have to do a lot more work to p/invoke to a bunch of undocumented GDI methods instead
internal class PictureDispConverter
{
public static stdole.IPictureDisp BitmapToPictureDisp(Bitmap bitmap)
{
return new PictureDispImpl(bitmap);
}
public static Image PictureDispToBitmap(stdole.IPictureDisp pictureDisp)
{
// TODO
return null;
}
}
internal class PictureDispImpl : stdole.IPictureDisp, stdole.IPicture
{
#region Init
[DllImport("gdi32.dll")]
static extern void DeleteObject(IntPtr handle);
private Bitmap _image;
private IntPtr _handle;
public PictureDispImpl(Bitmap image)
{
_image = image;
}
~PictureDispImpl()
{
if (_handle != IntPtr.Zero)
{
DeleteObject(_handle);
}
}
#endregion
#region IPictureDisp Members
public int Width
{
get { return _image.Width; }
}
public int Height
{
get { return _image.Height; }
}
public short Type
{
get { return 1; }
}
public int Handle
{
get
{
if (_handle == IntPtr.Zero)
{
_handle = _image.GetHbitmap();
}
return _handle.ToInt32();
}
}
public int hPal
{
get { return 0; }
set { }
}
public void Render(
int hdc, int x, int y, int cx, int cy, int xSrc, int ySrc, int cxSrc, int cySrc, IntPtr prcWBounds)
{
Graphics graphics = Graphics.FromHdc(new IntPtr(hdc));
graphics.DrawImage(
_image, new Rectangle(x, y, cx, cy), xSrc, ySrc, cxSrc, cySrc, GraphicsUnit.Pixel);
}
#endregion
#region IPicture Members
public int Attributes
{
get { return 0; }
}
public int CurDC
{
get { return 0; }
}
public bool KeepOriginalFormat
{
get { return false; }
set { }
}
public void PictureChanged()
{
}
public void SaveAsFile(IntPtr pstm, bool fSaveMemCopy, out int pcbSize)
{
pcbSize = 0;
}
public void SelectPicture(int hdcIn, out int phdcOut, out int phbmpOut)
{
phdcOut = 0;
phbmpOut = 0;
}
public void SetHdc(int hdc)
{
}
#endregion
}
Related
I'been developing a library on C# for the last days, and now, I need to build that library into a dll, and use that dll from Qt.
The library includes three classes I need to access, and my original idea was to simply build the dll, use dumpcpp to get the headers and use them from Qt along with activeqt, which I have used before.
The problem is that I cannot manage to get the headers from dumpcpp, it fails, returning:
dumpcpp: loading '.\Wrapper.dll' as a type library failed
dumpcpp: error processing type library '.\Wrapper.dll'
The Visual Studio project I'm using is a Class library (.Net Standard), and I'm using building it for x86 architecture. I've seen this link and tried using
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
with the three classes I need in order to get a COM dll which I suppose must be usable by dumpcpp, but it fails with the same error I mentioned before. Almost every documentation I have found is about using C++ dlls on C#, and there's nothing about using dumpcpp with C# dlls.
Do somebody has any ideas on how to get this dll working with dumpcpp, so that I can use it with Qt? I'd be really grateful.
Thanks a lot!
Edit: In case it helps, here it's the basic structure (without the complex funcionality) of the unique main .cs file the dll includes:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Wrapper
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Device
{
public string host;
public string username;
public string password;
public uint port;
private bool _serverConnected;
public uint serverGroupId { get; private set; }
public uint serverId { get; private set; }
private object[] _cameraIds;
public Device(string host, string username, string password, uint port)
{
this.host = host;
this.username = username;
this.password = password;
this.port = port;
this._serverConnected = false;
Debug.WriteLine("Device Initialized");
}
public bool isConnected() => _serverConnected;
public void onServerConnected(uint serverGroupId, uint serverId)
{
Debug.WriteLine("ServerConnectedEventHandler"); // TODO: Remove, just for testing
_serverConnected = true;
}
public void onServerGroupDisconnected(uint serverGroupId)
{
Debug.WriteLine("ServerGroupDisconnectedEventHandler"); // TODO: Remove, just for testing
_serverConnected = false;
}
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
class Player
{
private Device _device;
private static readonly int NUM_FRAMES = 10;
private Frame[] _lastFrames = new Frame[NUM_FRAMES];
private int _toReadFrame, _toReceivedFrame;
public Player(Device device)
{
this._device = device;
_toReadFrame = 0;
_toReceivedFrame = 0;
}
public Frame Image()
{
Frame tmp = _lastFrames[_toReadFrame];
_toReadFrame = (++_toReadFrame) % NUM_FRAMES;
return tmp;
}
void FrameAvailableEventHandler(int width, int height, byte[] data)
{
_lastFrames[_toReceivedFrame] = new Frame(width, height, data);
_toReceivedFrame = (++_toReceivedFrame) % NUM_FRAMES;
}
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
class Frame
{
public int width { get; private set; }
public int height { get; private set; }
public byte[] data { get; private set; }
public Frame(int width, int height, byte[] data)
{
this.data = data;
this.width = width;
this.height = height;
}
}
}
I am reading a TiledExr file as follows using C++ and OpenEXR
std::vector<double> TiledExrDepthMapExtractor::extractDepthMap(const char* filePath,
int& width, int& height, ProgressFunc& progressCallback, ErrorFunc& errorCallback)
{
// Do some stuff then...
return pixelVector;
}
This gets called from C#
public class ExrDepthMapExtractor : IDepthMapExtractor
{
public RawDepthMap GetDepthMap(string filePath)
{
return ExtractRawDepthMap(filePath, Callbacks.ProgressCallback, Callbacks.ErrorCallback);
}
private unsafe RawDepthMap ExtractRawDepthMap(string filePath,
Callbacks.ProgressFunc progressCallback, Callbacks.ErrorFunc errorCallback)
{
using (InternalExtractRawDepthMapWrapper(filePath, out RawDepthMap rdm, progressCallback, errorCallback))
{
rdm.Source = filePath;
return rdm;
}
}
private unsafe PixelVectorSafeHandle InternalExtractRawDepthMapWrapper(
string filePath, out RawDepthMap rdm, Callbacks.ProgressFunc progressCallback, Callbacks.ErrorFunc errorCallback)
{
if (!NativeMethods.ExtractDepthMapAs1DArray(out PixelVectorSafeHandle pixelVectorHandle,
out double* pixels, out int width, out int height, filePath, progressCallback))
throw new FormatException($"Depth map could not be extracted from \"{filePath}\"");
var pixelList = new List<double>();
for (int i = 0; i < width * height; i++)
pixelList.Add(pixels[i]);
rdm = new RawDepthMap()
{
Source = filePath,
DepthMapArray = pixelList,
Height = height,
Width = width
};
return pixelVectorHandle;
}
}
Via an API
[SuppressUnmanagedCodeSecurity()]
internal static class NativeMethods
{
[DllImport("Blundergat.OpenExr.Adapter.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
internal static unsafe extern bool ExtractDepthMapAs1DArray(
out PixelVectorSafeHandle vectorHandle,
out double* points,
out int width,
out int height,
string filePath,
Callbacks.ProgressFunc progressCallback);
[DllImport("Blundergat.OpenExr.Adapter.dll", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern bool Release(IntPtr itemsHandle);
}
and converted to a RawDepthMap
public class RawDepthMap
{
public string Source { get; set; }
public List<double> DepthMapArray { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public override string ToString()
{
string source = !String.IsNullOrEmpty(Source) ? Source : "N/A";
StringBuilder builder = new StringBuilder($"RawDepthMap: Source \"{source}\"");
if (DepthMapArray != null)
builder.Append($", Array size {DepthMapArray:N0}");
builder.Append($", Width {Width:N0}, Height {Height:N0}");
return builder.ToString();
}
}
This is a spherical depth map (it is a spherical panorama image), essentially an 1D array of depth mesurements. From this, I convert to a Cartesian PointCloud using
public class DepthMapToPointCloudAdapter : IDepthMapToPointCloudAdapter
{
public DepthMapToPointCloudAdapter() { }
public IPointCloud GetPointCloudFromDepthMap(RawDepthMap rdm)
{
// Do some transformations.
return new PointCloud(dataPoints.ToArray(), rdm.Source);
}
}
This give the following point cloud
Now, I need to read an image EXR file. I do this again like above using C++
std::vector<double> ImageExrDepthMapExtractor::extractDepthMap(const char* filePath,
int& width, int& height, ProgressFunc& progressCallback, ErrorFunc& errorCallback)
{
// Do some reading...
return pixelVector;
}
Same conversion routine to Cartesian PointCloud but this time I get a spherical geometry.
This is clearly down to the way I am reading the image (RGBA) .exr file, but what exactly is causing this?
I want to address Port 0xE020 (Hex -> Dec = 57.376) but it is out of bounds for a short. Now, I need this to test PCI-E Extension Parallel Ports.
Thanks to the answer to this question (https://stackoverflow.com/a/4618383/3391678) I use the following working code for 32-bit environments:
using System;
using System.Runtime.InteropServices;
namespace LPTx86
{
public class LPT
{
//inpout.dll
[DllImport("inpout32.dll")]
private static extern void Out32(short PortAddress, short Data);
[DllImport("inpout32.dll")]
private static extern char Inp32(short PortAddress);
private short _PortAddress;
public LPT(short PortAddress)
{
_PortAddress = PortAddress;
}
public void Write(short Data)
{
Out32(_PortAddress, Data);
}
public byte Read()
{
return (byte)Inp32(_PortAddress);
}
}
}
And for 64-bit environments:
using System;
using System.Runtime.InteropServices;
namespace LPTx64
{
class LPT
{
[DllImport("inpoutx64.dll", EntryPoint = "Out32")]
private static extern void Out32_x64(short PortAddress, short Data);
[DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
private static extern char Inp32_x64(short PortAddress);
private short _PortAddress;
public LPT64(short PortAddress)
{
_PortAddress = PortAddress;
}
public void Write(short Data)
{
Out32_x64(_PortAddress, Data);
}
public byte Read()
{
return (byte)Inp32_x64(_PortAddress);
}
}
}
(Yes, I removed every piece of safeties and sanity checking, my second name is "Danger" ;) )
Is it possible and how can I address ports that do not fit into a short?
My header looks like this
void _stdcall Out32(short PortAddress, short data);
short _stdcall Inp32(short PortAddress);
The accepted answer works, but I think a more idiomatic way of using longer addresses is keeping the signatures consistent and casting to short in an unchecked context
[DllImport("inpout32.dll")]
private static extern void Out32(short PortAddress, short data);
[DllImport("inpout32.dll")]
private static extern short Inp32(short PortAddress);
public void Write(int address, short data) {
short portAddress;
unchecked {
portAddress = (short)address;
}
Out32(portAddress, data);
}
public short Read(int address) {
short portAddress;
unchecked {
portAddress = (short)address;
}
return Inp32(portAddress);
}
It's possible:
I tried and changed the type of the port variable from short to int, i tested it and it seems to work:
Edit: Refactored Code to combine the data/control/status ports into one Package
using System;
using System.Runtime.InteropServices;
namespace LPTtestdrive
{
class LPT_X
{
//inpout.dll
[DllImport("inpout32.dll")]
private static extern UInt32 IsInpOutDriverOpen();
[DllImport("inpout32.dll")]
private static extern void Out32(int PortAddress, short Data);
[DllImport("inpout32.dll")]
private static extern char Inp32(int PortAddress);
//inpoutx64.dll
[DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
private static extern UInt32 IsInpOutDriverOpen_x64();
[DllImport("inpoutx64.dll", EntryPoint = "Out32")]
private static extern void Out32_x64(int PortAddress, short Data);
[DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
private static extern char Inp32_x64(int PortAddress);
private bool _X64;
private int DataAddress;
private int StatusAddress;
private int ControlAddress;
public LPT_X(int PortAddress)
{
DataAddress = PortAddress;
StatusAddress = (int)(DataAddress + 1);
ControlAddress = (int)(DataAddress + 2);
//now the code tries to determine if it should use inpout32.dll or inpoutx64.dll
uint nResult = 0;
try
{
Console.WriteLine("trying 32 bits");
nResult = IsInpOutDriverOpen();
if (nResult != 0)
{
Console.WriteLine("using 32 bits");
return;
}
}
catch (BadImageFormatException)
{
Console.WriteLine("32 bits failed");
}
catch (DllNotFoundException)
{
throw new ArgumentException("Unable to find InpOut32.dll");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
try
{
Console.WriteLine("trying 64 bits");
nResult = IsInpOutDriverOpen_x64();
if (nResult != 0)
{
Console.WriteLine("using 64 bits");
_X64 = true;
return;
}
}
catch (BadImageFormatException)
{
Console.WriteLine("64 bits failed");
}
catch (DllNotFoundException)
{
throw new ArgumentException("Unable to find InpOutx64.dll");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
throw new ArgumentException("Unable to open either inpout32 and inpoutx64");
}
public void WriteData(short Data)
{
if (_X64)
{
Out32_x64(DataAddress, Data);
}
else
{
Out32(DataAddress, Data);
}
}
public void WriteControl(short Data)
{
if (_X64)
{
Out32_x64(ControlAddress, Data);
}
else
{
Out32(ControlAddress, Data);
}
}
public byte ReadData()
{
if (_X64)
{
return (byte)Inp32_x64(DataAddress);
}
else
{
return (byte)Inp32(DataAddress);
}
}
public byte ReadControl()
{
if (_X64)
{
return (byte)Inp32_x64(ControlAddress);
}
else
{
return (byte)Inp32(ControlAddress);
}
}
public byte ReadStatus()
{
if (_X64)
{
return (byte)Inp32_x64(StatusAddress);
}
else
{
return (byte)Inp32(StatusAddress);
}
}
}
}
Use it like this:
LPT_X LPT;
int read;
int Adress = 0xE020;
try
{
LPT = new LPT_X(Address);
LPT.WriteData(251);
LPT.WriteControl(0);
read = LPT.ReadStatus();
...
}
I am currently working on a third party dll, and randomly when the delegate OnReceive is invoked I get the following error:
CallbackOnCollectedDelegate was detected
I read that GC.Collect() can solve the problem using static but also not as, I have hours and trying every way CallbackOnCollectedDelegate get the error, please help...
namespace Interfaz
{
class TMPDlg:
{
public CTMIF m_objTMPInterface;
public uint m_dwLocalIP;
public ushort m_nPort;
public byte m_nSubNet;
public uint m_nRadioID;
public uint m_nIndex;
public uint m_dwMobileID;
public int nLength;
public string mensaje_destino;
public string mensaje_recibido;
public TMPDlg()
{
m_objTMPInterface = null;
}
unsafe public void OnReceive(ushort wOpcode, System.IntPtr pbPayload, uint dwSize, uint dwLocalIP)
{
TMReceive(wOpcode, (byte*)pbPayload, dwSize, dwLocalIP);
}
unsafe public void TMReceive(ushort wOpcode, byte * pbPayload, uint dwSize, uint dwLocalIP)
{
// Some Work....
}
public void Send_PrivateMsg(string textBoxMensaje, string labelID)
{
m_nRadioID = uint.Parse(labelID);
mensaje_destino = textBoxMensaje;
nLength = textBoxMensaje.Length;
m_objTMPInterface.SendPrivateMsg(m_nRadioID, mensaje_destino, nLength, 0);
}
public void conect_master(ushort port, string ip)
{
m_objTMPInterface = new CTMIF();
m_dwLocalIP = (uint)IPAddressToNumber(ip);
ADKCALLBACK myOnReceive = new ADKCALLBACK(OnReceive);
m_objTMPInterface.SetCallBackFn(myOnReceive);
//m_objTMPInterface.SetCallBackFn(OnReceive);
m_objTMPInterface.OpenSocket(m_dwLocalIP, port, m_dwMobileID, 10)<
}
Presumably this part is the problem?
ADKCALLBACK myOnReceive = new ADKCALLBACK(OnReceive);
m_objTMPInterface.SetCallBackFn(myOnReceive);
If you have an instance variable of type ADKCALLBACK, then so long as your instance isn't garbage collected before (or while) the callback function is executing, you should be okay. What controls your instance's lifetime?
class TMPDlg
{
// Instance variable to protect from garbage collection
private readonly ADKCALLBACK receiveCallback;
public TMPDlg()
{
receiveCallback = myOnReceive;
}
...
public void ConnectMaster(ushort port, string ip)
{
...
m_objTMPInterface.SetCallBackFn(receiveCallback);
...
}
}
(As an aside, your naming could be improved significantly, and you should avoid having public fields.)
I have an IP Camera that receives a char buffer containing an image over the network. I cant access it until i setup the connection to it in a program. I am trying to dissect windows source filter code and im not going very fast so i thought i'd ask if it was possible to just take a buffer like that and cast it to something that could then connect a pin to AVISplitter or such in Directshow/.net
(video buffer from IP Cam) -> (???) -> (AVI Splitter) -> (Profit)
Update
I have my program capturing video in a namespace, and i have this code from the GSSF in its own namespace. I pass a ptr with an image from the cam namespace to the GSSF namespace. This only occurs once, but the graph streams from this one image, and the camera streams from the network. is there a way to continually pass the buffer from cam to GSSF or should i combine the namespaces somehow? I tried sending the main camera pointer to the GSSF but it crashed because its accessing the pointer and its being written. maybe if i grabbed an image, passed the pointer, waited to grab a new one?
*Update*
I shrunk my code and I don't believe im doing the namespace correctly either now that i look at it.
namespace Cam_Controller
{
static byte[] mainbyte = new byte[1280*720*2];
static IntPtr main_ptr = new IntPtr();
//(this function is threaded)
static void Trial(NPvBuffer mBuffer, NPvDisplayWnd mDisplayWnd, VideoCompression compressor)
{
Functions function = new Functions();
Defines define = new Defines();
NPvResult operationalResult = new NPvResult();
VideoCompression mcompressor = new VideoCompression();
int framecount = 0;
while (!Stopping && AcquiringImages)
{
Mutex lock_video = new Mutex();
NPvResult result = mDevice.RetrieveNextBuffer(mBuffer, operationalResult);
if(result.isOK())
{
framecount++;
wer = (int)mDisplayWnd.Display(mBuffer, wer);
main_ptr = (IntPtr)mBuffer.GetMarshalledBuffer();
Marshal.Copy(main_ptr, mainbyte, 0, 720 * 2560);
}
}
}
private void button7_Click(object sender, EventArgs e)
{
IntPtr dd = (IntPtr)mBuffer.GetMarshalledBuffer();
Marshal.Copy(dd, main_byte1, 0, 720 * 2560);
play = new VisiCam_Controller.DxPlay.DxPlay("", panel9, main_byte1);
play.Start();
}
namespace DxPlay
{
public class DxPlay
{
public DxPlay(string sPath, Control hWin, byte[] color)
{
try
{
// pick one of our image providers
//m_ImageHandler = new ImageFromFiles(sPath, 24);
m_ImageHandler = new ImageFromPixels(20, color);
//m_ImageHandler = new ImageFromMpg(#"c:\c1.mpg");
//m_ImageHandler = new ImageFromMpg(sPath);
//m_ImageHandler = new ImageFromMP3(#"c:\vss\media\track3.mp3");
// Set up the graph
SetupGraph(hWin);
}
catch
{
Dispose();
throw;
}
}
}
abstract internal class imagehandler
internal class imagefrompixels
{
private int[] mainint = new int[720 * 1280];
unsafe public ImageFromPixels(long FPS, byte[] x)
{
long fff = 720 * 1280 * 3;
mainptr = new IntPtr(fff);
for (int p = 0; p < 720 * 640; p++)
{
U = (x[ p * 4 + 0]);
Y = (x[p * 4 + 1]);
V = (x[p * 4 + 2]);
Y2 = (x[p * 4 + 3]);
int one = V << 16 | Y << 8 | U;
int two = V << 16 | Y2 << 8 | U;
mainint[p * 2 + 0] = one;
mainint[p * 2 + 1] = two;
}
m_FPS = UNIT / FPS;
m_b = 211;
m_g = 197;
}
}
}
}
Theres also GetImage but thats relatively the same, copy the buffer into the pointer. What happens is i grab a buffer of the image and send it to the DxPlay class. it is able to process it and put it on the directshow line no problems; but it never updates nor gets updated because its just a single buffer. If i instead send DxPlay a IntPtr holding the address of the image buffer, the code crashes for accessing memory because i assume ImageFromPixels code ( which isn't there now ( change
(x[p * 4 + #])
to
(IntPtr)((x-passed as an IntPtr).toInt64()+p*4 + #)
))
is accessing the memory of the pointer as the Cam_Controller class is editing it. I make and pass copies of the IntPtrs, and new IntPtrs but they fail halfway through the conversion.
If you want to do this in .NET, the following steps are needed:
Use the DirectShow.NET Generic Sample Source Filter (GSSF.AX) from the Misc/GSSF directory within the sample package. A source filter is always a COM module, so you need to register it too using "RegSvr32 GSSF.ax".
Implement a bitmap provider in .NET
Setup a graph, and connect the pin from the GSSF to the implementation of the bitmap provider.
Pray.
I am using the following within a project, and made it reusable for future usage.
The code (not the best, and not finished, but a working start) (this takes a IVideoSource, which is bellow):
public class VideoSourceToVideo : IDisposable
{
object locker = new object();
public event EventHandler<EventArgs> Starting;
public event EventHandler<EventArgs> Stopping;
public event EventHandler<EventArgs> Completed;
/// <summary> graph builder interface. </summary>
private DirectShowLib.ICaptureGraphBuilder2 captureGraphBuilder = null;
DirectShowLib.IMediaControl mediaCtrl = null;
IMediaEvent mediaEvent = null;
bool stopMediaEventLoop = false;
Thread mediaEventThread;
/// <summary> Dimensions of the image, calculated once in constructor. </summary>
private readonly VideoInfoHeader videoInfoHeader;
IVideoSource source;
public VideoSourceToVideo(IVideoSource source, string destFilename, string encoderName)
{
try
{
this.source = source;
// Set up the capture graph
SetupGraph(destFilename, encoderName);
}
catch
{
Dispose();
throw;
}
}
/// <summary> release everything. </summary>
public void Dispose()
{
StopMediaEventLoop();
CloseInterfaces();
}
/// <summary> build the capture graph for grabber. </summary>
private void SetupGraph(string destFilename, string encoderName)
{
int hr;
// Get the graphbuilder object
captureGraphBuilder = new DirectShowLib.CaptureGraphBuilder2() as DirectShowLib.ICaptureGraphBuilder2;
IFilterGraph2 filterGraph = new DirectShowLib.FilterGraph() as DirectShowLib.IFilterGraph2;
mediaCtrl = filterGraph as DirectShowLib.IMediaControl;
IMediaFilter mediaFilt = filterGraph as IMediaFilter;
mediaEvent = filterGraph as IMediaEvent;
captureGraphBuilder.SetFiltergraph(filterGraph);
IBaseFilter aviMux;
IFileSinkFilter fileSink = null;
hr = captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, destFilename, out aviMux, out fileSink);
DsError.ThrowExceptionForHR(hr);
DirectShowLib.IBaseFilter compressor = DirectShowUtils.GetVideoCompressor(encoderName);
if (compressor == null)
{
throw new InvalidCodecException(encoderName);
}
hr = filterGraph.AddFilter(compressor, "compressor");
DsError.ThrowExceptionForHR(hr);
// Our data source
IBaseFilter source = (IBaseFilter)new GenericSampleSourceFilter();
// Get the pin from the filter so we can configure it
IPin ipin = DsFindPin.ByDirection(source, PinDirection.Output, 0);
try
{
// Configure the pin using the provided BitmapInfo
ConfigurePusher((IGenericSampleConfig)ipin);
}
finally
{
Marshal.ReleaseComObject(ipin);
}
// Add the filter to the graph
hr = filterGraph.AddFilter(source, "GenericSampleSourceFilter");
Marshal.ThrowExceptionForHR(hr);
hr = filterGraph.AddFilter(source, "source");
DsError.ThrowExceptionForHR(hr);
hr = captureGraphBuilder.RenderStream(null, null, source, compressor, aviMux);
DsError.ThrowExceptionForHR(hr);
IMediaPosition mediaPos = filterGraph as IMediaPosition;
hr = mediaCtrl.Run();
DsError.ThrowExceptionForHR(hr);
}
private void ConfigurePusher(IGenericSampleConfig ips)
{
int hr;
source.SetMediaType(ips);
// Specify the callback routine to call with each sample
hr = ips.SetBitmapCB(source);
DsError.ThrowExceptionForHR(hr);
}
private void StartMediaEventLoop()
{
mediaEventThread = new Thread(MediaEventLoop)
{
Name = "Offscreen Vid Player Medialoop",
IsBackground = false
};
mediaEventThread.Start();
}
private void StopMediaEventLoop()
{
stopMediaEventLoop = true;
if (mediaEventThread != null)
{
mediaEventThread.Join();
}
}
public void MediaEventLoop()
{
MediaEventLoop(x => PercentageCompleted = x);
}
public double PercentageCompleted
{
get;
private set;
}
// FIXME this needs some work, to be completely in-tune with needs.
public void MediaEventLoop(Action<double> UpdateProgress)
{
mediaEvent.CancelDefaultHandling(EventCode.StateChange);
//mediaEvent.CancelDefaultHandling(EventCode.Starvation);
while (stopMediaEventLoop == false)
{
try
{
EventCode ev;
IntPtr p1, p2;
if (mediaEvent.GetEvent(out ev, out p1, out p2, 0) == 0)
{
switch (ev)
{
case EventCode.Complete:
Stopping.Fire(this, null);
if (UpdateProgress != null)
{
UpdateProgress(source.PercentageCompleted);
}
return;
case EventCode.StateChange:
FilterState state = (FilterState)p1.ToInt32();
if (state == FilterState.Stopped || state == FilterState.Paused)
{
Stopping.Fire(this, null);
}
else if (state == FilterState.Running)
{
Starting.Fire(this, null);
}
break;
// FIXME add abort and stuff, and propagate this.
}
// Trace.WriteLine(ev.ToString() + " " + p1.ToInt32());
mediaEvent.FreeEventParams(ev, p1, p2);
}
else
{
if (UpdateProgress != null)
{
UpdateProgress(source.PercentageCompleted);
}
// FiXME use AutoResetEvent
Thread.Sleep(100);
}
}
catch (Exception e)
{
Trace.WriteLine("MediaEventLoop: " + e);
}
}
}
/// <summary> Shut down capture </summary>
private void CloseInterfaces()
{
int hr;
try
{
if (mediaCtrl != null)
{
// Stop the graph
hr = mediaCtrl.Stop();
mediaCtrl = null;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
if (captureGraphBuilder != null)
{
Marshal.ReleaseComObject(captureGraphBuilder);
captureGraphBuilder = null;
}
GC.Collect();
}
public void Start()
{
StartMediaEventLoop();
}
}
IVideoSource:
public interface IVideoSource : IGenericSampleCB
{
double PercentageCompleted { get; }
int GetImage(int iFrameNumber, IntPtr ip, int iSize, out int iRead);
void SetMediaType(global::IPerform.Video.Conversion.Interops.IGenericSampleConfig psc);
int SetTimeStamps(global::DirectShowLib.IMediaSample pSample, int iFrameNumber);
}
ImageVideoSource (mostly taken from DirectShow.NET examples):
// A generic class to support easily changing between my different sources of data.
// Note: You DON'T have to use this class, or anything like it. The key is the SampleCallback
// routine. How/where you get your bitmaps is ENTIRELY up to you. Having SampleCallback call
// members of this class was just the approach I used to isolate the data handling.
public abstract class ImageVideoSource : IDisposable, IVideoSource
{
#region Definitions
/// <summary>
/// 100 ns - used by a number of DS methods
/// </summary>
private const long UNIT = 10000000;
#endregion
/// <summary>
/// Number of callbacks that returned a positive result
/// </summary>
private int m_iFrameNumber = 0;
virtual public void Dispose()
{
}
public abstract double PercentageCompleted { get; protected set; }
abstract public void SetMediaType(IGenericSampleConfig psc);
abstract public int GetImage(int iFrameNumber, IntPtr ip, int iSize, out int iRead);
virtual public int SetTimeStamps(IMediaSample pSample, int iFrameNumber)
{
return 0;
}
/// <summary>
/// Called by the GenericSampleSourceFilter. This routine populates the MediaSample.
/// </summary>
/// <param name="pSample">Pointer to a sample</param>
/// <returns>0 = success, 1 = end of stream, negative values for errors</returns>
virtual public int SampleCallback(IMediaSample pSample)
{
int hr;
IntPtr pData;
try
{
// Get the buffer into which we will copy the data
hr = pSample.GetPointer(out pData);
if (hr >= 0)
{
// Set TRUE on every sample for uncompressed frames
hr = pSample.SetSyncPoint(true);
if (hr >= 0)
{
// Find out the amount of space in the buffer
int cbData = pSample.GetSize();
hr = SetTimeStamps(pSample, m_iFrameNumber);
if (hr >= 0)
{
int iRead;
// Get copy the data into the sample
hr = GetImage(m_iFrameNumber, pData, cbData, out iRead);
if (hr == 0) // 1 == End of stream
{
pSample.SetActualDataLength(iRead);
// increment the frame number for next time
m_iFrameNumber++;
}
}
}
}
}
finally
{
// Release our pointer the the media sample. THIS IS ESSENTIAL! If
// you don't do this, the graph will stop after about 2 samples.
Marshal.ReleaseComObject(pSample);
}
return hr;
}
}
RawVideoSource (an example of a concrete managed source generator for a DirectShow pipeline):
internal class RawVideoSource : ImageVideoSource
{
private byte[] buffer;
private byte[] demosaicBuffer;
private RawVideoReader reader;
public override double PercentageCompleted
{
get;
protected set;
}
public RawVideoSource(string sourceFile)
{
reader = new RawVideoReader(sourceFile);
}
override public void SetMediaType(IGenericSampleConfig psc)
{
BitmapInfoHeader bmi = new BitmapInfoHeader();
bmi.Size = Marshal.SizeOf(typeof(BitmapInfoHeader));
bmi.Width = reader.Header.VideoSize.Width;
bmi.Height = reader.Header.VideoSize.Height;
bmi.Planes = 1;
bmi.BitCount = 24;
bmi.Compression = 0;
bmi.ImageSize = (bmi.BitCount / 8) * bmi.Width * bmi.Height;
bmi.XPelsPerMeter = 0;
bmi.YPelsPerMeter = 0;
bmi.ClrUsed = 0;
bmi.ClrImportant = 0;
int hr = psc.SetMediaTypeFromBitmap(bmi, 0);
buffer = new byte[reader.Header.FrameSize];
demosaicBuffer = new byte[reader.Header.FrameSize * 3];
DsError.ThrowExceptionForHR(hr);
}
long startFrameTime;
long endFrameTime;
unsafe override public int GetImage(int iFrameNumber, IntPtr ip, int iSize, out int iRead)
{
int hr = 0;
if (iFrameNumber < reader.Header.NumberOfFrames)
{
reader.ReadFrame(buffer, iFrameNumber, out startFrameTime, out endFrameTime);
Demosaic.DemosaicGBGR24Bilinear(buffer, demosaicBuffer, reader.Header.VideoSize);
Marshal.Copy(demosaicBuffer, 0, ip, reader.Header.FrameSize * 3);
PercentageCompleted = ((double)iFrameNumber / reader.Header.NumberOfFrames) * 100.0;
}
else
{
PercentageCompleted = 100;
hr = 1; // End of stream
}
iRead = iSize;
return hr;
}
override public int SetTimeStamps(IMediaSample pSample, int iFrameNumber)
{
reader.ReadTimeStamps(iFrameNumber, out startFrameTime, out endFrameTime);
DsLong rtStart = new DsLong(startFrameTime);
DsLong rtStop = new DsLong(endFrameTime);
int hr = pSample.SetTime(rtStart, rtStop);
return hr;
}
}
And the interops to the GSSF.AX COM:
namespace IPerform.Video.Conversion.Interops
{
[ComImport, Guid("6F7BCF72-D0C2-4449-BE0E-B12F580D056D")]
public class GenericSampleSourceFilter
{
}
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("33B9EE57-1067-45fa-B12D-C37517F09FC0")]
public interface IGenericSampleCB
{
[PreserveSig]
int SampleCallback(IMediaSample pSample);
}
[Guid("CE50FFF9-1BA8-4788-8131-BDE7D4FFC27F"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IGenericSampleConfig
{
[PreserveSig]
int SetMediaTypeFromBitmap(BitmapInfoHeader bmi, long lFPS);
[PreserveSig]
int SetMediaType([MarshalAs(UnmanagedType.LPStruct)] AMMediaType amt);
[PreserveSig]
int SetMediaTypeEx([MarshalAs(UnmanagedType.LPStruct)] AMMediaType amt, int lBufferSize);
[PreserveSig]
int SetBitmapCB(IGenericSampleCB pfn);
}
}
Good luck, try to get it working using this. Or comment with further questions so we can iron out other issues.