利用摄像头拍照的ActiveX控件C#net源代码Word格式文档下载.docx
《利用摄像头拍照的ActiveX控件C#net源代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《利用摄像头拍照的ActiveX控件C#net源代码Word格式文档下载.docx(18页珍藏版)》请在冰豆网上搜索。
otxt=obj.make2();
/script>
首先新建一个类库,名为:
MakePhoto
然后修改以下文件:
//-------------------------------------------------------
//文件名:
Properties/AssemblyInfo.cs
//--------------------------------------------------------
usingSystem.Reflection;
usingSystem.Runtime.CompilerServices;
usingSystem.Runtime.InteropServices;
usingSystem.Security;
//有关程序集的常规信息通过下列属性集
//控制。
更改这些属性值可修改
//与程序集关联的信息。
[assembly:
AssemblyTitle("
MakePhoto"
)]
AssemblyDescription("
"
AssemblyConfiguration("
AssemblyCompany("
AssemblyProduct("
AssemblyCopyright("
版权所有(C)2015"
AssemblyTrademark("
AssemblyCulture("
//将ComVisible设置为false使此程序集中的类型
//对COM组件不可见。
如果需要从COM访问此程序集中的类型,
//则将该类型上的ComVisible属性设置为true。
ComVisible(true)]
//如果此项目向COM公开,则下列GUID用于类型库的ID
Guid("
86d26cf4-ae4e-4599-bbf7-e0ec72ab1b76"
//程序集的版本信息由下面四个值组成:
//
//主版本
//次版本
//内部版本号
//修订号
//可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
//方法是按如下所示使用“*”:
AssemblyVersion("
1.0.*"
AssemblyFileVersion("
1.0.0.0"
AllowPartiallyTrustedCallers()]
//---------------------
//文件:
InterfaceCapture.cs
//------------------------
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceMakePhoto
{
[ComImport,GuidAttribute("
CB5BDC81-93C1-11CF-8F20-00805F2CD064"
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
interfaceInterfaceCapture
{
[PreserveSig]
intGetInterfaceSafetyOptions(refGuidriid,[MarshalAs(UnmanagedType.U4)]refintpdwSupportedOptions,[MarshalAs(UnmanagedType.U4)]refintpdwEnabledOptions);
[PreserveSig()]
intSetInterfaceSafetyOptions(refGuidriid,[MarshalAs(UnmanagedType.U4)]intdwOptionSetMask,[MarshalAs(UnmanagedType.U4)]intdwEnabledOptions);
}
}
//---------------------------
//控件设计:
ykCapture.Designer.cs
partialclassykCapture
///<
summary>
///必需的设计器变量。
/summary>
privateSystem.ComponentModel.IContainercomponents=null;
///清理所有正在使用的资源。
paramname="
disposing"
如果应释放托管资源,为true;
否则为false。
/param>
protectedoverridevoidDispose(booldisposing)
if(disposing&
&
(components!
=null))
components.Dispose();
base.Dispose(disposing);
#region组件设计器生成的代码
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
privatevoidInitializeComponent()
this.SuspendLayout();
//
//ykCapture
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.Name="
ykCapture"
;
this.Size=newSystem.Drawing.Size(320,240);
this.Load+=newSystem.EventHandler(this.ykCapture_Load);
this.ResumeLayout(false);
#endregion
//------------------------------
//控件主程序:
ykCapture.cs
//--------------------------------
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Data;
usingSystem.Windows.Forms;
usingSystem.Drawing.Imaging;
[Guid("
publicpartialclassykCapture:
UserControl,InterfaceCapture
Pickpick;
publicykCapture()
InitializeComponent();
privatevoidykCapture_Load(objectsender,EventArgse)
publicvoidstart()//启动摄像头
pick=newPick(this.Handle,0,0,this.Width,this.Height);
pick.Start();
publicvoidstop()//停止摄像头
pick.Stop();
publicstringmake2()
returnpick.Cap();
publicstringmake3()
Bitmapbmp=newBitmap(this.Width,this.Height);
Graphicsg=Graphics.FromImage(bmp);
g.CopyFromScreen(PointToScreen(newPoint(this.Left,this.Top)),newPoint(0,0),this.Size);
System.IO.MemoryStreamms=newSystem.IO.MemoryStream();
bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[]arr=newbyte[ms.Length];
ms.Position=0;
ms.Read(arr,0,(int)ms.Length);
ms.Close();
stringstrbase64=Convert.ToBase64String(arr);
returnstrbase64;
publicstringGetBase64String(System.Drawing.Imageimage)
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[]buffer=ms.ToArray();
returnConvert.ToBase64String(buffer);
publicImageGetImageFromBase64String(stringImageText)
if(ImageText.Length>
0)
Byte[]bitmapData=newByte[ImageText.Length];
//bitmapData=Convert.FromBase64String(FixBase64ForImage(ImageText));
bitmapData=Convert.FromBase64String(ImageText);
System.IO.MemoryStreamstreamBitmap=newSystem.IO.MemoryStream(bitmapData);
//BitmapbitImage=newBitmap((Bitmap)Image.FromStream(streamBitmap));
returnImage.FromStream(streamBitmap);
else
returnnull;
publicstringmake(stringpath)//拍照
pick.GrabImage(path);
returnpath;
#regionIObjectSafety成员
privateconststring_IID_IDispatch="
{00020400-0000-0000-C000-000000000046}"
privateconststring_IID_IDispatchEx="
{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"
privateconststring_IID_IPersistStorage="
{0000010A-0000-0000-C000-000000000046}"
privateconststring_IID_IPersistStream="
{00000109-0000-0000-C000-000000000046}"
privateconststring_IID_IPersistPropertyBag="
{37D84F60-42CB-11CE-8135-00AA004BB851}"
privateconstintINTERFACESAFE_FOR_UNTRUSTED_CALLER=0x00000001;
privateconstintINTERFACESAFE_FOR_UNTRUSTED_DATA=0x00000002;
privateconstintS_OK=0;
privateconstintE_FAIL=unchecked((int)0x80004005);
privateconstintE_NOINTERFACE=unchecked((int)0x80004002);
privatebool_fSafeForScripting=true;
privatebool_fSafeForInitializing=true;
publicintGetInterfaceSafetyOptions(refGuidriid,refintpdwSupportedOptions,refintpdwEnabledOptions)
intRslt=E_FAIL;
stringstrGUID=riid.ToString("
B"
pdwSupportedOptions=INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA;
switch(strGUID)
case_IID_IDispatch:
case_IID_IDispatchEx:
Rslt=S_OK;
pdwEnabledOptions=0;
if(_fSafeForScripting==true)
pdwEnabledOptions=INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case_IID_IPersistStorage:
case_IID_IPersistStream:
case_IID_IPersistPropertyBag:
if(_fSafeForInitializing==true)
pdwEnabledOptions=INTERFACESAFE_FOR_UNTRUSTED_DATA;
default:
Rslt=E_NOINTERFACE;
returnRslt;
publicintSetInterfaceSafetyOptions(refGuidriid,intdwOptionSetMask,intdwEnabledOptions)
if(((dwEnabledOptions&
dwOptionSetMask)==INTERFACESAFE_FOR_UNTRUSTED_CALLER)&
(_fSafeForScripting==true))
dwOptionSetMask)==INTERFACESAFE_FOR_UNTRUSTED_DATA)&
(_fSafeForInitializing==true))
privatevoidthis_Click(objectsender,EventArgse)
classPick
privateconstintWM_USER=0x400;
privateconstintWS_CHILD=0x40000000;
privateconstintWS_VISIBLE=0x10000000;
privateconstintWM_CAP_START=WM_USER;
privateconstintWM_CAP_STOP=WM_CAP_START+68;
privateconstintWM_CAP_DRIVER_CONNECT=WM_CAP_START+10;
privateconstintWM_CAP_DRIVER_DISCONNECT=WM_CAP_START+11;
privateconstintWM_CAP_SAVEDIB=WM_CAP_START+25;
privateconstintWM_CAP_GRAB_FRAME=WM_CAP_START+60;
privateconstintWM_CAP_SEQUENCE=WM_CAP_START+62;
privateconstintWM_CAP_FILE_SET_CAPTURE_FILEA=WM_CAP_START+20;
privateconstintWM_CAP_SEQUENCE_NOFILE=WM_CAP_START+63;
privateconstintWM_CAP_SET_OVERLAY=WM_CAP_START+51;
privateconstintWM_CAP_SET_PREVIEW=WM_CAP_START+50;
privateconstintWM_CAP_SET_CALLBACK_VIDEOSTREAM=WM_CAP_START+6;
privateconstintWM_CAP_SET_CALLBACK_ERROR=WM_CAP_START+2;
privateconstintWM_CAP_SET_CALLBACK_STATUSA=WM_CAP_START+3;
privateconstintWM_CAP_SET_CALLBACK_FRAME=WM_CAP_START+5;
privateconstintWM_CAP_SET_SCALE=WM_CAP_START+53;
privateconstintWM_CAP_SET_PREVIEWRATE=WM_CAP_START+52;
privateIntPtrhWndC;
privateboolbStat=false;
privateIntPtrmControlPtr;
privateintmWidth;
privateintmHeight;
privateintmLeft;
privateintmTop;
///
///初始化摄像头
///控件的句柄
///开始显示的左边距
///开始显示的上边距
///要显示的宽度
///要显示的长度
publicPick(IntPtrhandle,intleft,inttop,intwidth,intheight)
mControlPtr=handle;
mWidth=width;
mHeight=height;
mLeft=left;
mTop=top;
[DllImport("
avicap32.dll"
privatestaticexternIntPtrcapCreateCaptureWindowA(byte[]lpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID);
privatestaticexternintcapGetVideoFormat(IntPtrhWnd,IntPtrpsVideoFormat,intwSize);
User32.dll"
privatestaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,intl