WPF界面新控件的编写的例子

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)									
            {									
									
            }									
        }									
    }																			
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ysgs129

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值