Beispiel #1
0
        public ActionResult GetImageBytes(int id = 1)
        {
            Activity_photo ap = db.Activity_photo.Find(id);

            byte[] img = ap.Activity_photo1;
            return(File(img, "image/jpeg"));
        }
        //按下修改時
        public void btn_amendact_Click(object sender, RoutedEventArgs e)
        {
            if (VM_Activity_name == null || VM_Activity_type == null || VM_Group_name == null)
            {
                System.Windows.Forms.MessageBox.Show("欄位有空格無法新增");
                return;
            }
            else if (VM_Activity_startdate == null)
            {
                System.Windows.Forms.MessageBox.Show("起始日期空白", "新增活動失敗");
                return;
            }
            else if (VM_Activity_enddate == null || Convert.ToDateTime(VM_Activity_enddate) < Convert.ToDateTime(VM_Activity_startdate))
            {
                System.Windows.Forms.MessageBox.Show("結束日期空白或短於起始日期", "新增活動失敗");
                return;
            }
            System.IO.MemoryStream MS      = new System.IO.MemoryStream(); //將圖轉成二進位
            JpegBitmapEncoder      encoder = new JpegBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(VM_Activity_photos));
            encoder.Save(MS);
            VM_Activity_photo_byte = MS.GetBuffer();
            var q = (from o in Myentity.Activities
                     where o.Activity_no == VM_Activity_no
                     select o).Single();
            var q2 = (from p in Myentity.Activity_photo
                      where p.Activity_photo_id == VM_Activity_Photo_id
                      select p).Single();

            if (VM_Activity_type_ID == 0)
            {
                VM_Activity_type_ID = 1;
            }
            if (VM_Group_no == 0)
            {
                VM_Group_no = 1;
            }
            q.Activity_name      = VM_Activity_name;
            q.lecturer           = VM_lecturer;
            q.Activity_type_ID   = VM_Activity_type_ID;
            q.Group_no           = VM_Group_no;
            q.Place              = VM_Place;
            q.Activity_startdate = DateTime.Parse(VM_Activity_startdate);
            q.Activity_enddate   = DateTime.Parse(VM_Activity_enddate);
            q.Undertaker         = VM_supervision_ID;
            q.Undertake_email    = VM_supervision_email;
            q.Undertake_phone    = VM_supervision_phone.ToString();
            q.Member             = VM_Member;
            q.Spare              = VM_Spare;
            q.Summary            = VM_Summary;
            q.Activity_Photo_id  = VM_Activity_Photo_id;
            q2.Activity_photo1   = VM_Activity_photo_byte;
            if (wrapPanelmodel.wrapPanel != null && WrapPanelModel.wrapPanel.Children.Count > 0)
            {
                for (int i = 0; i < list_photo_save.Count; i += 1)
                {
                    BitmapImage            bi       = new BitmapImage(new Uri(list_photo_save[i]));
                    System.IO.MemoryStream MS2      = new System.IO.MemoryStream(); //將圖轉成二進位
                    JpegBitmapEncoder      encoder2 = new JpegBitmapEncoder();
                    encoder2.Frames.Add(BitmapFrame.Create(bi));
                    encoder2.Save(MS2);
                    VM_Activity_photo_byte = MS2.GetBuffer();
                    Activity_photo ap = new Activity_photo()
                    {
                        Activity_id = VM_Activity_no, Activity_photo1 = VM_Activity_photo_byte
                    };
                    Myentity.Activity_photo.Add(ap);
                }
            }
            Myentity.SaveChanges();
            MessageBox.Show("修改完成");
        }
        //新增活動事件▼
        public void btn_addActivity_Click(object sender, RoutedEventArgs e)
        {//新增活動事件
            if (VM_Activity_name == null || VM_Activity_type == null || VM_Group_name == null)
            {
                System.Windows.Forms.MessageBox.Show("欄位有空格無法新增");
                return;
            }
            else if (VM_Activity_startdate == null)
            {
                System.Windows.Forms.MessageBox.Show("起始日期空白", "新增活動失敗");
                return;
            }
            else if (VM_Activity_enddate == null || Convert.ToDateTime(VM_Activity_enddate) < Convert.ToDateTime(VM_Activity_startdate))
            {
                System.Windows.Forms.MessageBox.Show("結束日期空白或短於起始日期", "新增活動失敗");
                return;
            }
            System.IO.MemoryStream MS      = new System.IO.MemoryStream(); //將圖轉成二進位
            JpegBitmapEncoder      encoder = new JpegBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(VM_Activity_photos));
            encoder.Save(MS);
            VM_Activity_photo_byte = MS.GetBuffer();
            //先新增一筆活動▼新增照片時才有活動編號可用
            if (VM_Activity_type_ID == 0)
            {
                VM_Activity_type_ID = 1;
            }
            if (VM_Group_no == 0)
            {
                VM_Group_no = 1;
            }
            Activity Ac = new Activity()
            {
                Activity_name = VM_Activity_name, lecturer = VM_lecturer, Activity_type_ID = VM_Activity_type_ID, Group_no = VM_Group_no, Undertake_unit = "社工室", Undertaker = VM_supervision_ID, Undertake_phone = VM_supervision_phone.ToString(), Undertake_email = VM_supervision_email, Member = VM_Member, Spare = VM_Spare, Place = VM_Place, Summary = VM_Summary, Activity_startdate = DateTime.Parse(VM_Activity_startdate), Activity_enddate = DateTime.Parse(VM_Activity_enddate)
            };

            Myentity.Activities.Add(Ac);
            Myentity.SaveChanges();
            //再新增照片進資料庫▼
            var q = (from A in Myentity.Activities
                     where A.Activity_name == VM_Activity_name
                     select new { ActID = A.Activity_no }).Single();
            Activity_photo Ap = new Activity_photo()
            {
                Activity_id = q.ActID, Activity_photo1 = VM_Activity_photo_byte
            };

            Myentity.Activity_photo.Add(Ap);
            Myentity.SaveChanges();
            //再回頭修改剛新增的活動-活動照片ID
            var q1 = (from p in Myentity.Activity_photo
                      where p.Activity_id == q.ActID
                      select new { P_photoID = p.Activity_photo_id, P_activityID = p.Activity_id }).Single();
            var q2 = (from A2 in Myentity.Activities
                      where A2.Activity_no == q1.P_activityID
                      select A2).Single();

            q2.Activity_Photo_id = q1.P_photoID;
            Myentity.SaveChanges();
            if (wrapPanelmodel.wrapPanel != null && WrapPanelModel.wrapPanel.Children.Count > 0)
            {
                for (int i = 0; i < list_photo_save.Count; i += 1)
                {
                    BitmapImage            bi       = new BitmapImage(new Uri(list_photo_save[i]));
                    System.IO.MemoryStream MS2      = new System.IO.MemoryStream(); //將圖轉成二進位
                    JpegBitmapEncoder      encoder2 = new JpegBitmapEncoder();
                    encoder2.Frames.Add(BitmapFrame.Create(bi));
                    encoder2.Save(MS2);
                    VM_Activity_photo_byte = MS2.GetBuffer();
                    Activity_photo ap = new Activity_photo()
                    {
                        Activity_id = q1.P_activityID, Activity_photo1 = VM_Activity_photo_byte
                    };
                    Myentity.Activity_photo.Add(ap);
                }
            }
            Myentity.SaveChanges();
            MessageBox.Show("新增成功");
            System.Windows.Forms.Application.Exit();
        }