private void cheEditFile_CheckedChanged(object sender, EventArgs e) { try { if (this.cheEditFile.Checked == true) { string path = this.txtPicturePath.Text.Trim(); if (File.Exists(path) == true) { using (Image image = this.picboxQRCodePicture.Image) { ImageFormat imageFormat = QRCodeCommon.GetImageFormat(path); image.Save(path, imageFormat); } } else { throw new FileNotFoundException("需要修改的文件不存在,请先保存。"); } } } catch (Exception ee) { MessageBox.Show(ee.Message, "错误"); } }
private void btnScreen_Click(object sender, EventArgs e) { try { Rectangle rectangle = Screen.PrimaryScreen.Bounds; rectangle.Width = 1920; rectangle.Height = 1080; Image image = new Bitmap(rectangle.Width, rectangle.Height); using (Graphics graphics = Graphics.FromImage(image)) { graphics.CopyFromScreen(0, 0, 0, 0, rectangle.Size); } using (ScreenForm screenForm = new ScreenForm(image)) { screenForm.ShowDialog(); if (screenForm.bSucceed == true) { this.txtQRCodeText.Text = QRCodeCommon.ImageToText(Clipboard.GetImage()); } } } catch (Exception ee) { MessageBox.Show(ee.Message, "错误"); } }
private void txtPicturePath_TextChanged(object sender, EventArgs e) { try { string path = this.txtPicturePath.Text.Trim(); if (path == "") { return; } if (File.Exists(path)) { using (Stream stream = File.Open(path, FileMode.Open)) { if (stream.Length == 0) { return; } this.picboxQRCodePicture.Image = Image.FromStream(stream); } this.txtQRCodeText.Text = QRCodeCommon.ImageToText(this.picboxQRCodePicture.Image); } else { this.picboxQRCodePicture.Image = this.picboxQRCodePicture.ErrorImage; this.txtQRCodeText.Text = ""; } } catch (Exception ee) { MessageBox.Show(ee.Message, "错误"); } }
/// <summary> /// 保存图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSavePicture_Click(object sender, EventArgs e) { try { this.saveFileDialog1.Filter = @" 所有图片| *.bmp; *.ico; *.gif; *.jpeg; *.jpg; *.png; *.tif; *.tiff | Windows Bitmap(*.bmp)|*.bmp| Windows Icon(*.ico)|*.ico| Graphics Interchange Format (*.gif)|(*.gif)| JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg| Portable Network Graphics (*.png)|*.png| Tag Image File Format (*.tif)|*.tif;*.tiff"; DialogResult result = this.saveFileDialog1.ShowDialog(); if (result == DialogResult.OK) { string path = this.saveFileDialog1.FileName; using (Image image = this.picboxQRCodePicture.Image) { ImageFormat imageFormat = QRCodeCommon.GetImageFormat(path); image.Save(path, imageFormat); } this.txtPicturePath.Text = path; } } catch (Exception ee) { MessageBox.Show(ee.Message, "错误"); } }
private void txtQRCodeText_TextChanged(object sender, EventArgs e) { try { Size size; string path = this.txtPicturePath.Text.Trim(); string content = this.txtQRCodeText.Text; if (content == "") { this.picboxQRCodePicture.Image = this.picboxQRCodePicture.ErrorImage; return; } size = new Size() { Width = int.Parse(this.txtWidth.Text), Height = int.Parse(this.txtHeight.Text), }; Image image = QRCodeCommon.TextToBitmap(content, size, this.barcodeFormat); this.picboxQRCodePicture.Image = image; this.labWidth.Text = "宽:" + image.Size.Width.ToString(); this.labHeight.Text = "高:" + image.Size.Height.ToString(); this.labTextLength.Text = "内容长度:" + content.Length; //using (Stream stream = new MemoryStream()) //{ // image.Save(stream, image.RawFormat); // this.labPictureSIze.Text = "图片大小:" + (stream.Length / 1024 + "K"); //} string strPath = DateTime.Now.ToFileTimeUtc().ToString() + ".png"; image.Save(strPath); using (Stream stream = File.OpenRead(strPath)) { this.labPictureSIze.Text = "图片大小:" + (stream.Length / 1024 + "K"); } File.Delete(strPath); if (this.cheEditFile.Checked == true) { if (File.Exists(path) == true) { ImageFormat imageFormat = QRCodeCommon.GetImageFormat(path); image.Save(path, imageFormat); //image.Dispose(); } else { throw new FileNotFoundException("需要修改的文件不存在,请先保存。"); } } } catch (Exception ee) { MessageBox.Show(ee.Message, "错误"); } }