示例#1
0
        /// <summary>
        /// 打开图片
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="mode">1: 数据流 2:本地文件 3:拍照文件 4: 截屏文件</param>
        public Form3(string datas, int mode, int firsttime)
        {
            InitializeComponent();
            this.mode = mode;
            //this.pictureBox1.Size = new System.Drawing.Size(screenWidth, screenHeight);
            this.WindowState = FormWindowState.Maximized;

            this.TopMost = true;
#if DEBUG
            this.TopMost = false;//Form3 ShowView
#endif
            this.pictureBox1.Visible = false;
            this.pictureBox1.Visible = true;
            try
            {
                Bitmap bmp = null;
                if (mode == 1)
                {
                    m_base64 = datas;
                    m_bmp    = Base64StringToImage(datas);
                }
                else if (mode == 2)
                {
                    filepath = datas;

                    string filetype = Path.GetExtension(filepath).ToLower();
                    if (filetype == ".png")
                    {
                        string filepath2 = Path.GetDirectoryName(filepath) + "\\" + Path.GetFileNameWithoutExtension(filepath) + ".jpg";
                        try
                        {
                            File.Delete(filepath2);
                            System.Drawing.Bitmap b = (Bitmap)Image.FromFile(filepath);
                            b.Save(filepath2, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        catch (Exception e)
                        {
                            Log.Error(e.Message);
                        }

                        filepath = filepath2;
                    }

                    Log.Info("showPicture2 :" + filepath);
                    m_base64 = Util.ImgToBase64String(filepath);

                    m_bmp = (Bitmap)Image.FromFile(filepath);

                    imgHeight = m_bmp.Height;
                    imgWidth  = m_bmp.Width;
                    Log.Info("imgHeight=" + imgHeight + " imgWidth=" + imgWidth);
                    Log.Info("screenHeight=" + screenHeight + " screenWidth=" + screenWidth);
                    //m_bmp = new Bitmap(m_bmp, screenWidth, screenHeight);//不能图片缩放,因为破坏了图片的质量
                }
                else if (mode == 3)
                {
                    filepath = Application.StartupPath + "\\" + datas;

                    string filetype = Path.GetExtension(filepath).ToLower();
                    if (filetype == ".png")
                    {
                        string filepath2        = Path.GetDirectoryName(filepath) + "\\" + Path.GetFileNameWithoutExtension(filepath) + ".jpg";
                        System.Drawing.Bitmap b = (Bitmap)Image.FromFile(filepath);
                        b.Save(filepath2, System.Drawing.Imaging.ImageFormat.Jpeg);
                        filepath = filepath2;
                    }

                    Log.Info("showPicture3 :" + filepath);
                    m_base64 = Util.ImgToBase64String(filepath);
                    m_bmp    = (Bitmap)Image.FromFile(filepath);
                }
                else if (mode == 4)
                {
                    m_bmp    = ScreenCapture.captureScreen(0, 0);
                    m_base64 = Util.ImgToBase64String(m_bmp);
                }

                imgHeight = m_bmp.Height;
                imgWidth  = m_bmp.Width;

                Log.Info("imgHeight=" + imgHeight + " imgWidth=" + imgWidth);
                Log.Info("screenHeight=" + screenHeight + " screenWidth=" + screenWidth);

                //m_bmp = new Bitmap(m_bmp, screenWidth, screenHeight);//不能图片缩放,因为破坏了图片的质量
                this.pictureBox1.Image = m_bmp;

                this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                this.pictureBox1.Width    = screenWidth;
                this.pictureBox1.Height   = screenHeight;

                //doUpload
                if (mode == 1 && firsttime == 1)
                {
                    string imgName = DateTime.Now.ToString("yyyyMMdd") + "-" + Global.getSchoolID() + "-" + Global.getClassID() + "-" + DateTime.Now.ToString("HHmmss") + ".jpg";
                    string imgDir  = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd");
                    if (!Directory.Exists(imgDir))
                    {
                        Directory.CreateDirectory(imgDir);
                    }

                    string imgPath = imgDir + "\\" + imgName;
                    m_bmp.Save(imgPath);
                    Common.uploadPicture(imgPath);
                    Common.uploadCameraEvent(imgName);//相机拍照
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }



            ////(图片宽>屏幕宽 && 图片高>屏幕高)
            //if ((pictureBox1.Width > Screen.PrimaryScreen.WorkingArea.Width && pictureBox1.Height > Screen.PrimaryScreen.WorkingArea.Height) ||
            //    (pictureBox1.Width > Screen.PrimaryScreen.WorkingArea.Width && pictureBox1.Height < Screen.PrimaryScreen.WorkingArea.Height))
            //{
            //    MakeThumbnail(pictureBox1.Width, pictureBox1.Height, "W");
            //}
            ////(图片宽<屏幕宽 && 图片高>屏幕高) 高度压缩
            //else if (pictureBox1.Width < Screen.PrimaryScreen.WorkingArea.Width && pictureBox1.Height > Screen.PrimaryScreen.WorkingArea.Height)
            //{
            //    MakeThumbnail(pictureBox1.Width, pictureBox1.Height, "H");
            //}
            //else
            //{
            //    pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
            //    this.pictureBox1.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - pictureBox1.Width / 2;
            //    this.pictureBox1.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - pictureBox1.Height / 2;
            //}
        }