WPF界面新控件的编写的例子
1.TakePhotosWithMediaKit.xaml
<UserControl.Resources>
</UserControl.Resources>
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
2.TakePhotosWithMediaKit.xaml.cs
///
/// TakePhotosWithMediaKit.xaml 的交互逻辑
///
public partial class TakePhotosWithMediaKit : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private bool isTakePhoto = false;
public bool IsTakePhoto
{
get { return isTakePhoto; }
set
{
isTakePhoto = value;
this.NotifyPropertyChanged("IsTakePhoto");
}
}
public string m_photographPath = "Resource\\Common\\HTML\\Print\\CN\\" + "3333.jpg";
public static readonly DependencyProperty PhotographPathProperty = DependencyProperty.Register("PhotographPath", typeof(string),
typeof(TakePhotosWithMediaKit), new UIPropertyMetadata("", new PropertyChangedCallback(SetPhotographPathValue)));
public string m_cameraNamePath = "Resource\\Common\\HTML\\Print\\CN\\" + "a.txt";
public static readonly DependencyProperty CameraNamePathProperty = DependencyProperty.Register("CameraName", typeof(string),
typeof(TakePhotosWithMediaKit), new UIPropertyMetadata("", null));
public string PhotographPath
{
get { return (string)GetValue(PhotographPathProperty); }
set { SetValue(PhotographPathProperty, value); }
}
public static void SetPhotographPathValue(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (obj != null && e.NewValue != null)
{
TakePhotosWithMediaKit con = obj as TakePhotosWithMediaKit;
con.m_photographPath = e.NewValue.ToString();
}
}
public string CameraName
{
get { return (string)GetValue(CameraNamePathProperty); }
set { SetValue(CameraNamePathProperty, value); }
}
public static void SetCameraNamePathValue(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (obj != null && e.NewValue != null)
{
TakePhotosWithMediaKit con = obj as TakePhotosWithMediaKit;
con.m_cameraNamePath = e.NewValue.ToString();
}
}
public TakePhotosWithMediaKit()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window_Loaded);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (MultimediaUtil.VideoInputNames.Length > 0)
{
//vce.VideoCaptureSource = MultimediaUtil.VideoInputNames[0];
if (string.IsNullOrEmpty(CameraName))
{
vce.VideoCaptureSource = MultimediaUtil.VideoInputNames[0];
Log.Action.LogDebug("相机名字" + CameraName);
}
else
{
for (var i = 0; i < MultimediaUtil.VideoInputNames.Length; i++)
{
if (String.Compare(MultimediaUtil.VideoInputNames[i], CameraName) == 0)
vce.VideoCaptureSource = CameraName;
}
if (string.IsNullOrEmpty(vce.VideoCaptureSource))
{
vce.VideoCaptureSource = MultimediaUtil.VideoInputNames[0];
}
}
string[] names = MultimediaUtil.VideoInputNames;
Log.Action.LogDebug("开始遍历相机名字");
foreach (string name in names)
{
Log.Action.LogDebug("相机名字" + name);
}
Log.Action.LogDebug("结束遍历相机名字");
btnCapture.IsEnabled = true;
}
else
{
btnCapture.IsEnabled = false;
TextTip.Text = "设备故障,拍照按钮不可使用,请退出";
}
}
private int takePhotoCount = 0;
//拍照并保存图片
private void btnCapture_Click(object sender, RoutedEventArgs e)
{
try
{
RenderTargetBitmap bmp = new RenderTargetBitmap((int)vce.ActualWidth, (int)vce.ActualHeight, 96, 96, PixelFormats.Default);
bmp.Render(vce);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
string filePath = string.Empty;
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Temp"))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Temp");
}
string fileNameShow = AppDomain.CurrentDomain.BaseDirectory + "Temp\\" + System.Guid.NewGuid().ToString() + ".jpg";
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
byte[] captureData = ms.ToArray();
if (string.IsNullOrWhiteSpace(m_photographPath))
{
filePath = System.AppDomain.CurrentDomain.BaseDirectory + "Resource\\Common\\HTML\\Print\\CN\\" + "3333.jpg"; ;
}
else
{
filePath = System.AppDomain.CurrentDomain.BaseDirectory + m_photographPath;
}
File.WriteAllBytes(filePath, captureData);
string filePath1 = System.AppDomain.CurrentDomain.BaseDirectory + "Resource\\Common\\HTML\\Print\\CN\\" + "3333.png"; ;
File.WriteAllBytes(filePath1, captureData);
File.WriteAllBytes(fileNameShow, captureData);
}
if (System.IO.File.Exists(fileNameShow))
{
Bitmap bit = (Bitmap)Bitmap.FromFile(filePath);
imgPreview.Source = BitmapToBitmapImage(bit);
IsTakePhoto = true;
}
}
catch (Exception ex)
{
Log.Project.LogError(ex.Message, ex);
IsTakePhoto = false;
if (takePhotoCount > 2)
{
btnCapture.IsEnabled = false;
TextTip.Text = "设备故障,拍照按钮不可使用,请退出";
}
}
if (btnCapture.Content.ToString() == "拍照")
{
btnCapture.Content = "重新拍照";
}
takePhotoCount++;
}
public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
BitmapImage bitmapImage = null;
using (Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height))
{
int i, j;
for (i = 0; i < bitmap.Width; i++)
for (j = 0; j < bitmap.Height; j++)
{
System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
bitmapSource.SetPixel(i, j, newColor);
}
MemoryStream ms = new MemoryStream();
bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
bitmapImage.EndInit();
}
return bitmapImage;
}
public void Dispose()
{
try
{
vce.Close();
}
catch (Exception ex)
{
}
}
}