|
|
|
มัน error ว่า Object reference not set to an instance of an object. อ่ะครับ |
|
|
|
|
|
|
|
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using WIA;
namespace BlogWebcam
{
public partial class Main : Form
{
private int currentFileNumber = 0;
private string currentFileName = String.Empty;
private bool tooManyFiles = false;
private Device device = null;
private string jpegGuid = string.Empty;
BlogWebcam.Properties.Settings settings = BlogWebcam.Properties.Settings.Default;
public Main()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
SaveSettings();
if (btnStart.Text == "Start")
{
DeviceManager manager = new DeviceManagerClass();
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == settings.DeviceID)
{
device = info.Connect();
break;
}
}
timerWebcam.Enabled = true;
btnStart.Text = "Stop";
}
else
{
timerWebcam.Enabled = false;
device = null;
btnStart.Text = "Start";
}
}
private string GetFreeFileName()
{
while (true)
{
currentFileName = currentFileNumber.ToString() + ".jpg";
if (tooManyFiles == true)
{
System.IO.File.Delete(currentFileName);
}
if (System.IO.File.Exists(currentFileName) == false)
{
return currentFileName;
}
currentFileNumber++;
if (currentFileNumber > 50)
{
currentFileNumber = 0;
tooManyFiles = true;
}
}
}
public void SaveSettings()
{
settings.TimeInMinutes = Convert.ToInt32(numTimer.Value);
timerWebcam.Interval = settings.TimeInMinutes * 60000;
settings.FTPServerURL = txtFTPURL.Text;
settings.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey (@"CLSID\{D2923B86-15F1-46FF-A19A-DE825F919576}\SupportedExtension\.jpg");
jpegGuid = jpegKey.GetValue("FormatGUID") as string; <<<< error ตรงนี้อ่ะครับ
//reasonable default
if (settings.TimeInMinutes <= 0)
{
settings.TimeInMinutes = 15;
}
timerWebcam.Interval = settings.TimeInMinutes * 60000;
numTimer.Value = settings.TimeInMinutes;
txtFTPURL.Text = settings.FTPServerURL;
}
private void btnConfigure_Click(object sender, EventArgs e)
{
CommonDialogClass class1 = new CommonDialogClass();
Device d = class1.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true,false);
if (d != null)
{
settings.DeviceID = d.DeviceID;
settings.Save();
}
}
private void timerWebcam_Tick(object sender, EventArgs e)
{
timerWebcam.Enabled = false;
string filename = TakePicture();
if (string.IsNullOrEmpty(filename) == false)
{
FTPUploadFile(filename);
}
timerWebcam.Enabled = true;
}
private void FTPUploadFile(string filename)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(settings.FTPServerURL);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
FileInfo fileInfo = new FileInfo(filename);
byte[] fileContents = new byte[fileInfo.Length];
using (FileStream fr = fileInfo.OpenRead())
{
fr.Read(fileContents, 0, Convert.ToInt32(fileInfo.Length));
}
using (Stream writer = request.GetRequestStream())
{
writer.Write(fileContents, 0, fileContents.Length);
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
}
}
catch (WebException)
{
throw;
}
}
private string TakePicture()
{
string filename = String.Empty;
if (device != null)
{
Item item = device.ExecuteCommand(CommandID.wiaCommandTakePicture);
foreach (string format in item.Formats)
{
if (format == jpegGuid)
{
WIA.ImageFile imagefile = item.Transfer(format) as WIA.ImageFile;
filename = GetFreeFileName();
if (string.IsNullOrEmpty(filename) == false)
{
imagefile.SaveFile(filename);
}
this.picLastImage.Load(filename);
return filename;
}
}
}
else
{
btnConfigure_Click(null, null);
}
return filename;
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
timerWebcam.Enabled = false;
SaveSettings();
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.Show();
}
}
}
เหมือนมันจะไม่รู้จักบรรทันนี้ เพราะอ่ะไรอ่ะครับ..
Tag : C#
|
|
|
|
|
|
Date :
2013-05-22 18:07:46 |
By :
ธีระวุฒิ |
View :
1104 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้า C# น่าจะ \\ น่ะครับ
Code (C#)
Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey (@"CLSID\\{D2923B86-15F1-46FF-A19A-DE825F919576}\\SupportedExtension\\.jpg");
|
|
|
|
|
Date :
2013-05-23 09:29:14 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใส่ \\ เข้าไปแล้ว มันก็ ยัง error เหมือนเดิมอ่ะ ครับ..
|
|
|
|
|
Date :
2013-05-23 11:10:46 |
By :
ธีระวุฒิ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|