我需要在紧凑的框架3.7应用程序中通过从dll Aygshell.dll中对SHCameraCapture进行pinvoking来显示相机捕获对话框。由于我正在使用的技术的限制,我不能使用托管对象CameraCaptureDialog。相反,我需要通过Pinvoking来访问它。
有关该功能的文档,请参阅http://msdn.microsoft.com/en-us/library/aa454995.aspx。该函数接受一个定义对话框参数的结构。例如保存文件的位置,使用的分辨率。
我想成像,我必须在C#中定义结构的副本,并使用StructLayout属性装饰sturct。我还想象代码会涉及[DllImport(“aygshell.dll”)]。如何调用此示例代码将非常感激。
答案 0 :(得分:2)
此代码有效....
#region Enumerations
public enum CAMERACAPTURE_STILLQUALITY
{
CAMERACAPTURE_STILLQUALITY_DEFAULT = 0,
CAMERACAPTURE_STILLQUALITY_LOW = 1,
CAMERACAPTURE_STILLQUALITY_NORMAL = 2,
CAMERACAPTURE_STILLQUALITY_HIGH = 3
}
public enum CAMERACAPTURE_VIDEOTYPES
{
CAMERACAPTURE_VIDEOTYPE_ALL = 0xFFFF,
CAMERACAPTURE_VIDEOTYPE_STANDARD = 1,
CAMERACAPTURE_VIDEOTYPE_MESSAGING = 2
}
public enum CAMERACAPTURE_MODE
{
CAMERACAPTURE_MODE_STILL = 0,
CAMERACAPTURE_MODE_VIDEOONLY = 1,
CAMERACAPTURE_MODE_VIDEOWITHAUDIO = 2
}
#endregion //Enumerations
#region Structures
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct struSHCAMERACAPTURE
{
public uint cbSize;
public IntPtr hwndOwner;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public String szFile;
[MarshalAs(UnmanagedType.LPTStr)]
public String pszInitialDir; //LPCTSTR
[MarshalAs(UnmanagedType.LPTStr)]
public String pszDefaultFileName; //LPCTSTR
[MarshalAs(UnmanagedType.LPTStr)]
public String pszTitle; //LPCTSTR
public CAMERACAPTURE_STILLQUALITY StillQuality;
public CAMERACAPTURE_VIDEOTYPES VideoTypes;
public uint nResolutionWidth;
public uint nResolutionHeight;
public uint nVideoTimeLimit;
public CAMERACAPTURE_MODE Mode;
}
#endregion //Structures
#region API
[DllImport("Aygshell.dll", SetLastError = true,CharSet=CharSet.Unicode)]
public static extern int SHCameraCapture
(
ref struSHCAMERACAPTURE pshCamCapture
);
private string StartImager(String strImgDir, String strImgFile, uint uintImgHeight,
uint uintImgWidth)
try
{
struSHCAMERACAPTURE shCamCapture = new struSHCAMERACAPTURE();
shCamCapture.cbSize = (uint)Marshal.SizeOf(shCamCapture);
shCamCapture.hwndOwner = IntPtr.Zero;
shCamCapture.szFile = "\\" + strImgFile; //strImgDir + "\\" + strImgFile;
shCamCapture.pszInitialDir = "\\"; // strImgDir;
shCamCapture.pszDefaultFileName = strImgFile;
shCamCapture.pszTitle = "PTT Image Capture";
shCamCapture.StillQuality = 0; // CAMERACAPTURE_STILLQUALITY.CAMERACAPTURE_STILLQUALITY_NORMAL;
shCamCapture.VideoTypes = CAMERACAPTURE_VIDEOTYPES.CAMERACAPTURE_VIDEOTYPE_STANDARD;
shCamCapture.nResolutionHeight = 0; // uintImgHeight;
shCamCapture.nResolutionWidth = 0; // uintImgWidth;
shCamCapture.nVideoTimeLimit = 10;
shCamCapture.Mode = 0; // CAMERACAPTURE_MODE.CAMERACAPTURE_MODE_STILL;
//IntPtr intptrCamCaptr = IntPtr.Zero;
//Marshal.StructureToPtr(shCamCapture, intptrCamCaptr, true);
int intResult = SHCameraCapture(ref shCamCapture);
if (intResult != 0)
{
Win32Exception Win32 = new Win32Exception(intResult);
MessageBox.Show("Error: " + Win32.Message);
}
return strCaptrErr;
}
catch (Exception ex)
{
MessageBox.Show("Error StartImager : " + ex.ToString() + " - " + strCaptrErr
, "Nomad Imager Test");
return "";
}
答案 1 :(得分:1)
Groky,这是一个良好的开端......感谢您的开始。我尝试连接它,但得到一个NotSupportedException。
我已粘贴下面测试应用中的文字。请注意,我尝试使用[StructLayout(LayoutKind.Sequential)]装饰结构。我还公开了所有成员,以消除对象可访问性方面的任何问题。
public partial class Form1 : Form
{
[DllImport("aygshell.dll")]
static extern int SHCameraCapture(ref SHCAMERACAPTURE pshcc);
[StructLayout(LayoutKind.Sequential)]
struct SHCAMERACAPTURE
{
public Int32 cbSize;
public IntPtr hwndOwner;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pszInitialDir;
[MarshalAs(UnmanagedType.LPStr)]
public string pszDefaultFileName;
[MarshalAs(UnmanagedType.LPStr)]
public string pszTitle;
public Int32 StillQuality;
public Int32 VideoTypes;
public Int32 nResolutionWidth;
public Int32 nResolutionHeight;
public Int32 nVideoTimeLimit;
public Int32 Mode;
}
private void ShowCamera()
{
SHCAMERACAPTURE captureData = new SHCAMERACAPTURE
{
cbSize = sizeof (Int64),
hwndOwner = (IntPtr)0,
szFile = "\\My Documents",
pszDefaultFileName = "picture.jpg",
pszTitle = "Camera Demo",
StillQuality = 0,
VideoTypes = 1,
nResolutionWidth = 480,
nResolutionHeight = 640,
nVideoTimeLimit = 0,
Mode = 0
};
SHCameraCapture(ref captureData);
}
private void button1_Click(object sender, EventArgs e)
{
ShowCamera();
}
答案 2 :(得分:0)
我无法对此进行测试,但您的结构/函数看起来应该是这样的:
struct SHCAMERACAPTURE {
public Int32 cbSize;
public IntPtr hwndOwner;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szFile;
[MarshalAs(UnmanagedType.LPStr)]
string pszInitialDir;
[MarshalAs(UnmanagedType.LPStr)]
string pszDefaultFileName;
[MarshalAs(UnmanagedType.LPStr)]
string pszTitle;
Int32 StillQuality;
Int32 VideoTypes;
Int32 nResolutionWidth;
Int32 nResolutionHeight;
Int32 nVideoTimeLimit;
Int32 Mode;
}
[DllImport("aygshell.dll")]
static extern int SHCameraCapture(ref SHCAMERACAPTURE pshcc);
AFAIK,你不需要在SHCAMERCAPTURE上显式设置StructLayout,因为它的布局没有什么异常。
一旦你开始工作,你可能想把你的发现发布到pinvoke.net上供其他人使用!